使用pygam时在初级Python游戏创建项目中获取AttributeError

2024-09-29 23:17:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我被一本叫做Python速成课程的Python初学者书中的游戏编写练习困住了。在这一点上,这是非常基本的。它只需要创建一个窗口,在这个窗口中,船的图像将加载在底部中心。我确信我几乎一行一行地复制了代码,并在网上遇到了与我在这里所做的几乎相同的示例,但我仍然不断遇到以下错误:

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个文件。我不认为这和设置.py,但我把它放进去以防万一,因为它很短。你知道吗

外星人_入侵.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()

发货.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)

设置.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()函数吗?如果整件事不是一个简单的小错误,不能抓住。你知道吗


Tags: theinpyrectselfgamegetsettings
1条回答
网友
1楼 · 发布于 2024-09-29 23:17:38

Ship()期望screen作为参数,但在AlienInvasion.__init__中,您在Ship(self)中使用self,因此您使用了AlienInvasion的instace,而不是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)

相关问题 更多 >

    热门问题