问题描述
出于某种原因,即使我在关卡和关卡编辑器中定义了所有内容,我也没有收到回复。我真正想做的是在我的 pygame 屏幕上显示地图。我正在使用 cv2、sys、cv2_imread、pygame 和 pygame_locals 库。没有出现错误消息,所以我没有在互联网上找到任何答案。好像也是opencv端的代码有问题。
代码:
pygame.init()
screen = pygame.display.set_mode((226,318))
player_pos = [95,292]
player = pygame.image.load('Player.png').convert()
player.set_colorkey((255,255,255))
level1 = list(imread('Map1.png')) # the image
moving_right = False
moving_left = False
moving_up = False
moving_down = False
clock = pygame.time.Clock()
while True:
screen.fill((15,15,15))
player_rect = screen.blit(player,player_pos)
if moving_right == True:
player_pos[0] += 2
if moving_left == True:
player_pos[0] -= 2
if moving_up == True:
player_pos[1] -= 2
if moving_down == True:
player_pos[1] += 2
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moving_right = True
if event.key == K_LEFT:
moving_left = True
if event.key == K_UP:
moving_up = True
if event.key == K_DOWN:
moving_down = True
if event.type == KEYUP:
if event.key == K_RIGHT:
player_pos[0] += 24
moving_right = False
if event.key == K_LEFT:
player_pos[0] -= 24
moving_left = False
if event.key == K_UP:
player_pos[1] -= 24
moving_up = False
if event.key == K_DOWN:
player_pos[1] += 24
moving_down = False
if player_pos[1] > 290:
moving_down = False #This will be changed later so you die
if player_pos[1] < 4:
moving_up = False
if player_pos[0] < 4:
moving_left = False
if player_pos[0] > 212:
moving_right = False
a = screen.get_width()/32
b = screen.get_height()/24
for i in range(len(level1)):
for j in range(len(level1[i])):
blue = list(level1[i][j])[0]
green = list(level1[i][j])[1]
red = list(level1[i][j])[2]
if blue == 255 and green == 255 and red == 255:
pygame.draw.rect(screen,(255,255),pygame.Rect(j*a,i *b,32,32))
else:
pygame.draw.rect(screen,(0,0),pygame.Rect(j * a,i * b,32))
pygame.display.update()
clock.tick(120)
print(clock.get_fps())
pygame.quit()
播放器.png:
Map1.png:
解决方法
我无法解决整个问题,但出现黑屏的主要原因之一是因为您将 print(df)
type symbol desc
0 pet A 1
1 toy 2 B
2 toy 3 C
3 car D 4
和 j*a
相乘。
例如,如果您尝试:
b*i
如果你将坐标设置为 100,你也会看到玩家:
if blue == 255 and green == 255 and red == 255:
pygame.draw.rect(screen,(255,255,255),pygame.Rect(j,i,32,32))
else:
pygame.draw.rect(screen,(0,32))
因此,请仔细检查 if blue == 255 and green == 255 and red == 255:
pygame.draw.rect(screen,pygame.Rect(100,100,32))
。我不知道你到底想做什么。但我认为这会对你有所帮助。
j,i 的完整代码;
pygame.Rect(j*a,b*i,32)
j = 100,i = 100 的完整代码;
import sys
import pygame
from pygame import *
import cv2
pygame.init()
screen = pygame.display.set_mode((226,318))
player_pos = [95,292]
player = pygame.image.load('Player.png').convert()
player.set_colorkey((255,255))
level1 = list(cv2.imread('Map1.png')) # the image
moving_right = False
moving_left = False
moving_up = False
moving_down = False
clock = pygame.time.Clock()
while True:
screen.fill((15,15,15))
player_rect = screen.blit(player,player_pos)
if moving_right == True:
player_pos[0] += 2
if moving_left == True:
player_pos[0] -= 2
if moving_up == True:
player_pos[1] -= 2
if moving_down == True:
player_pos[1] += 2
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moving_right = True
if event.key == K_LEFT:
moving_left = True
if event.key == K_UP:
moving_up = True
if event.key == K_DOWN:
moving_down = True
if event.type == KEYUP:
if event.key == K_RIGHT:
player_pos[0] += 24
moving_right = False
if event.key == K_LEFT:
player_pos[0] -= 24
moving_left = False
if event.key == K_UP:
player_pos[1] -= 24
moving_up = False
if event.key == K_DOWN:
player_pos[1] += 24
moving_down = False
if player_pos[1] > 290:
moving_down = False #This will be changed later so you die
if player_pos[1] < 4:
moving_up = False
if player_pos[0] < 4:
moving_left = False
if player_pos[0] > 212:
moving_right = False
a = screen.get_width()/32
b = screen.get_height()/24
print(a,b)
for i in range(len(level1)):
for j in range(len(level1[i])):
blue = list(level1[i][j])[0]
green = list(level1[i][j])[1]
red = list(level1[i][j])[2]
if blue == 255 and green == 255 and red == 255:
pygame.draw.rect(screen,32))
else:
pygame.draw.rect(screen,0),32))
pygame.display.update()
clock.tick(120)
print(clock.get_fps())
pygame.quit()