使用pygame在初学者的Python游戏创建项目中获取AttributeError-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

使用pygame在初学者的Python游戏创建项目中获取AttributeError

kun坤 2019-12-26 14:31:17 335

我被一本名为《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个文件。我不认为这和设置有任何关系。但是我把它包括进去了,因为它很短。 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()函数吗?这是如果整个事情不是一个简单的小错误无法捕捉。 问题来源StackOverflow 地址:/questions/59384638/getting-attributeerror-in-a-beginners-python-game-creating-project-while-using

人工智能 Python
分享到
取消 提交回答
全部回答(1)
  • kun坤
    2019-12-26 14:31:26

    Ship()期望屏幕作为参数,但是在外星人入侵中。你在Ship(self)中使用了self,所以你使用了instace的AlienInvasion,而不是screen,所以之后它会尝试AlienInvasion.get_rect(),你会得到你的错误。 你必须首先创造自我。筛选,然后与Ship()一起使用

    self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
    
    self.ship = Ship(self.screen)
    
    0 0
人工智能
使用钉钉扫一扫加入圈子
+ 订阅

了解行业+人工智能最先进的技术和实践,参与行业+人工智能实践项目

推荐文章
相似问题
推荐课程