如果没有Sprite
类,我如何生成平台来对应level
的“E”?我尝试了一些不同的方法,但似乎都不管用。基本上,这个想法是“E”的应该只是在一排50x50块。你知道吗
import pygame
pygame.init()
display_width = 900
display_height = 600
size = (display_width, display_height)
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
platx = 350
platy = 150
platw = 50
plath = 50
clock = pygame.time.Clock()
platx = 600
platy = 350
platw = 50
plath = 50
level = ["E","E","E","E","E","E","E","E","E","E"]
class Platform:
def __init__(self, platx, platy, platw, plath):
self.platx = platx
self.platy = platy
self.platw = platw
self.plath = plath
def draw(self):
pygame.draw.rect(screen, black, [self.platx, self.platy, self.platw, self.plath])
platforms = Platform(platx, platy, platw, plath)
game_exit = False
while not game_exit:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
#why doesnt a loop like this work?
#for i in level:
# if i == "E":
# platforms.draw()
# platx += display_width *.1
#shouldn't it be placing platforms 1/10 of the display_width from the starting platx?
screen.fill(white)
pygame.display.update()
如果我正确理解了您的代码,这就是需要修复的代码:
问题是
platx
已经作为参数传递给了Platform.__init__
。由于变量platform.platx
不再具有全局platx
的弱参数(作为参数传递),因此它们不再匹配。因此,不是递增platx
`平台.platx应该递增。你知道吗另外,您需要在绘制每一行之后重置
platform.platx
。你知道吗相关问题 更多 >
编程相关推荐