我试图在Pygame中制作一个简单的平台游戏,并创建了一个我想要的级别的基本轮廓。然而,为了移动周围的字符,我需要不断地用颜色填充屏幕,这与地图重叠。我该怎么办?你知道吗
我的代码:
import pygame
from pygame import *
import sys
WIN_WIDTH = 680
WIN_HEIGHT = 500
DISPLAY = (WIN_WIDTH, WIN_HEIGHT) #variable for screen display
DEPTH = 32 #standard
FLAGS = 0 #standard
RED = (0, 0, 255)
class Hero():
def __init__(self, x, y):
self.x = x
self.y = y
def appearance(self):
return pygame.image.load('C:\\Users\\admin\\Desktop\\Player1.png')
def move_right(self):
self.x += 10
return self.x
def move_left(self):
self.x -= 10
return self.x
player = Hero(56, 420)
player_img = player.appearance()
x = 0
y = 0
platformx = 0
platformy = 0
WHITE = (255, 255, 255)
pygame.init()
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
screen.fill(WHITE)
pygame.display.set_caption("Rum Islands")
timer = pygame.time.Clock()
level = [
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", #45 x 25
"P P",
"P P",
"P P",
"P PPPPPPPPPPP P",
"P P",
"P P",
"P P",
"P PPPPPPPP P",
"P P",
"P PPPPPPP P",
"P PPPPPP P",
"P P",
"P PPPPPPP P",
"P P",
"P PPPPPP P",
"P P",
"P PPPPPPPPPPP P",
"P P",
"P PPPPPPPPPPP P",
"P P",
"P P",
"P P",
"P P",
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",]
pygame.key.set_repeat(10,10)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x = player.move_left()
elif event.key == pygame.K_RIGHT:
x = player.move_right()
screen.fill(WHITE)
for row in level:
for col in row:
if col == "P":
pygame.draw.rect(screen, RED, (platformx, platformy, 40, 20))
platformx += 15
platformy += 20
platformx = 0
screen.blit(player_img, (x, y))
pygame.display.update()
修改后的代码。你知道吗
你的问题是
platformy
,因为你再也没有把它设为零。你知道吗相关问题 更多 >
编程相关推荐