我正在为此开发一个平台游戏我试图开发一个关卡类但是我继续收到错误'AttributeError:type object'Level'没有属性'platform_list'我无法识别问题,我已经采取了我觉得的一些代码是错误触发的区域是否可以提供帮助? 'line 214,in current_level.update(LVL)','line70,in update self.platform_list.update()'
class Level(object):
def __init__(self, Sprite):
""" Constructor. Pass in a handle to player. Needed for when moving platforms
collide with the player. """
self.platform_list = pygame.sprite.Group()
self.enemy_list = pygame.sprite.Group()
self.sprite = Sprite
self.background = None
def update(self):
""" Update everything in this level."""
self.platform_list.update()
self.enemy_list.update()
def draw(self, DS):
""" Draw everything on this level. """
# Draw all the sprite lists that we have
self.platform_list.draw(DS)
self.enemy_list.draw(DS)
LVL = Level
sprite = Sprite([400, 550])
level_list = []
level_list.append(Level_01)
# Set the current level
current_level_no = 0
current_level = level_list[current_level_no]
active_sprite_list = pygame.sprite.Group()
sprite.level = current_level
sprite.rect.x = 340
sprite.rect.y = H - sprite.rect.height
active_sprite_list.add(sprite)
# Loop until the user clicks the close button.
done = False
while not done:
events()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
key = pygame.key.get_pressed()
if key == [pygame.K_RIGHT]:
sprite.go_right()
if key == [pygame.K_LEFT]:
sprite.go_left()
if key == [pygame.K_UP]:
sprite.jump()
# If the player gets near the right side, shift the world left (-x)
if sprite.rect.right > W:
sprite.rect.right = W
# If the player gets near the left side, shift the world right (+x)
if sprite.rect.left < 0:
sprite.rect.left = 0
current_level.draw(DS)
active_sprite_list.draw(DS)
# Call the `update` method of the sprite to move it.
sprite.update()
# Update the player.
active_sprite_list.update()
# Update items in the level
current_level.update(LVL)
DS.fill(BLACK)
# Blit the sprite's image at the sprite's rect.topleft position.
DS.blit(sprite.image, sprite.rect)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
答案 0 :(得分:0)
您的变量LVL
不是您认为的那样。
你应该像这样初始化它:
sprite = Sprite([400, 550])
LVL = Level(sprite)
level_list = [LVL]
然后只需致电current_level.update()
。
另外,我建议将名称LVL
更改为initial_level
。更有意义。