在初学者的 Python 游戏创建项目中使用 pygame 时出现 AttributeError

Getting AttributeError in a beginner's Python game creating project while using pygame

我被一本名为 Python 速成课程的初学者 Python 书上的游戏编写练习困住了。在这一点上,这是非常基本的。它只应该创建一个 window,其中船的图像将加载在底部中心。我确定我几乎逐行复制了代码,并在网上遇到了与我在这里所做的差不多的示例,但我仍然不断收到以下错误:

Traceback (most recent call last):
  File "c:/Users/.../chapteraliens/alien_invasion.py", line 40, in <module>
    ai = AlienInvasion()
  File "c:/Users/.../chapteraliens/alien_invasion.py", line 17, in __init__
    self.ship = Ship(self)
  File "c:\Users\...\chapteraliens\ship.py", line 9, in __init__
    self.screen_rect = screen.get_rect()
AttributeError: 'AlienInvasion' object has no attribute 'get_rect'

这是我创建的 3 个文件。我不认为它与 settings.py 有任何关系,但我把它包括在内是为了以防万一,因为它很短。

alien_invasion.py

import sys
import pygame

from settings import Settings
from ship import Ship

class AlienInvasion:
    """Overall class to manage game assets and behavior."""

    def __init__(self):
        """Initialize the game and create the game resources."""
        pygame.init()
        pygame.display.set_caption("Alien Invasion") 

        self.settings = Settings()
        self.ship = Ship(self)

        self.bg_color = (self.settings.bg_color)
        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            """Watch for the keyboard and mouse events."""
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # Set the visual elements of the game.
            self.screen.fill(self.bg_color)
            self.ship.blitme()

            # Make the most recently drawn screen visible.
            pygame.display.flip()

if __name__ == "__main__":  
    # Make a game instance, and run the game.
    ai = AlienInvasion()
    ai.run_game()

ship.py

import pygame

class Ship:
    """The class that handles the management of the ship."""

    def __init__(self, screen):
        """Initialize the ship and set up the starting position."""
        self.screen = screen
        self.screen_rect = screen.get_rect()

        # Load the ship image and get its rect.
        self.image = pygame.image.load("ship_w.bmp")
        self.rect = self.image.get_rect()

        # Start each new ship at the bottom of center of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

    def blitme(self):
        """Draw the ship at its current location."""
        self.screen.blit(self.image, self.rect)

settings.py

class Settings:
    """A class to store settings for the game."""

    def __init__(self):
        """Initialize the game's settings."""
        # Screen settings
        self.screen_width = 800
        self.screen_height = 600
        self.bg_color = (40, 40, 40)

然后我试图注释掉与 get_rect 错误有关的行,但这只是为了让这次 "blit" 行出现相同的错误。我注释掉的行和新的错误如下:

...
        # self.screen_rect = screen.get_rect()
...
        # self.rect.midbottom = self.screen_rect.midbottom
...
Traceback (most recent call last):
  File "c:/Users/.../chapteraliens/alien_invasion.py", line 41, in <module>
    ai.run_game()
  File "c:/Users/.../chapteraliens/alien_invasion.py", line 33, in run_game
    self.ship.blitme()
  File "c:\Users\...\chapteraliens\ship.py", line 20, in blitme
    self.screen.blit(self.image, self.rect)
AttributeError: 'AlienInvasion' object has no attribute 'blit'

可能是我错误地使用了 get_rect() 和 blit() 函数?那就是如果整个事情不是一个无法捕获的简单小错误。

Ship() 期望 screen 作为参数,但在 AlienInvasion.__init__ 中你在 Ship(self) 中使用 self 所以你使用 AlienInvasion 的实例,而不是screen - 所以稍后它会尝试获取 AlienInvasion.get_rect() 并且你得到你的错误。

您必须先创建 self.screen,然后将其与 Ship() 一起使用

self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))

self.ship = Ship(self.screen)