属性错误: 'pygame.Surface'对象没有属性'screen'



我正在从《Python速成班:基于项目的编程入门》这本书中学习Python。在制作游戏时,我得到错误

Traceback (most recent call last):
File "d:BOOKSpythonmy-projectsAlienInvasiontempCodeRunnerFile.py", line 69, in <module>
alinv = AlienInvasion()
File "d:BOOKSpythonmy-projectsAlienInvasiontempCodeRunnerFile.py", line 22, in __init__
self.ship = Ship(self.screen)
File "d:BOOKSpythonmy-projectsAlienInvasionship.py", line 10, in   __init__
self.screen = alinv_game.screen
AttributeError: 'pygame.Surface' object has no attribute 'screen'

我的代码是:AlienInvasion.py

# Importing modules
import sys
import pygame
from settings import Settings
from ship import Ship

class AlienInvasion:
'''Class to manage game assets and behavior'''
def __init__(self):
'''Constructor to initialize the game and its assets'''
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self.screen)
def run_game(self):
'''Starts the main loop for the game'''
while True:
self._check_events()
self.ship.update()
self._update_screen()
def _check_events(self):
'''Watch for the keyboard and mouse events'''
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)
def _check_keydown_events(self, event):
"""Respond to keypresses."""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_ESCAPE:
sys.exit()
def _check_keyup_events(self, event):
"""Respond to key releases."""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _update_screen(self):
'''Redraw the screen during each pass of the loop'''
self.screen.fill(self.settings.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'''
alinv = AlienInvasion()
alinv.run_game()

ship.py

import pygame

class Ship:
'''A class to manage the ship'''
def __init__(self, alinv_game):
'''Initialize the ship and set its starting position'''
self.screen = alinv_game.screen
self.settings = alinv_game.settings
self.screen_rect = alinv_game.screen.get_rect()
'''Load the ship image and get its rect'''
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
'''Start each new ship at the bottom-center of the screen'''
self.rect.midbottom = self.screen_rect.midbottom
'''Store a decimal value for ship's horizontal position'''
self.x = float(self.rect.x)
'''Movement Flag'''
self.moving_right = False
self.moving_left = False
def update(self):

if self.moving_right and self.rect.right < self.screen_rect.right:
self.x += self.settings.ship_speed
if self.moving_left and self.rect.left > 0:
self.x -= self.settings.ship_speed
'''Update rect obj from self.x'''
self.rect.x = self.x
def blitme(self):
'''Draw the ship at its current location.'''
self.screen.blit(self.image, self.rect)

在Ship类的更新方法中,我实现了两个独立的块来允许Ship .rect.x值当左和右都增加然后减少按下方向键。否则,自我。moving_right将如果'elif'实现,则优先级更高。

settings.py

class Settings:
'''A class to store all settings for Alien Invasion Game.'''
def __init__(self):
'''Initialize the game's settings'''
# Screen Settings:
self.screen_width = 1280
self.screen_height = 720
self.bg_color = (230, 230, 230)
# Ship Settings:
self.ship_speed = 1.5

我认为错误应该是与屏幕声明,因为我认为Pygame。Surface没有'screen'属性,但我们可以通过get_rect方法获得它。请帮我找到这个bug。我无法解决这个问题。

我想你的意思是把self.ship = Ship(self.screen)写成self.ship = Ship(self)。这样,Ship对象可以访问AlienInvasion对象的所有属性,而不仅仅是它的screen属性(并且该属性没有screen属性,因此出现错误)。

最新更新