对象没有属性,Python速成课程

时间:2019-10-25 09:33:42

标签: python-3.x

我有一个GameStats类。在那我有一个方法ship_left。我试图从相同的对象上下文中运行它。这种方法应该在外星人与飞船相撞后改变游戏中飞船的剩余数量。

我试图以几种方式更改该参数,但该参数一直伴随着AttributeError:'GameStats'对象没有属性'ship_left'的消息。

请查看代码:

class GameStats():
    """Track statistics for alien invasion."""

    def __init__(self, ai_settings):
        """Initialize statistics."""
        self.ai_settings = ai_settings
        self.reset_stats()

        # Start game in an inactive state.
        self.game_active = False

    def reset_stats(self):
        self.ships_left = self.ai_settings.ship_limit

我在另一个窗口中定义了ship_left:

def ship_hit(ai_settings, stats, screen, ship, aliens, bullets):

    """Respond to ship being hit by alien."""
    if stats.ship_left >= 0:
        # Decrement ship_left.
        stats.ship_left -= 1

        # Empty the list of aliens and bullets.
        aliens.empty()
        bullets.empty()

        # Create a new fleet and center the ship.
        create_fleet(ai_settings, screen, ship, aliens)
        ship.center_ship()

        # Pause
        sleep(0.5)
    else:
        stats.game_active = False

0 个答案:

没有答案