Python 属性错误:';pygame.Surface';对象没有属性';屏幕';

Python 属性错误:';pygame.Surface';对象没有属性';屏幕';,python,python-3.x,pygame,pygame-surface,Python,Python 3.x,Pygame,Pygame Surface,我从《Python速成课程:一个实践的、基于项目的编程入门》一书中学习Python,第2E页。 在做游戏的时候,我得到了错误 Traceback (most recent call last): File "d:\BOOKS\python\my-projects\AlienInvasion\tempCodeRunnerFile.py", line 69, in <module> alinv = AlienInvasion() File "d:\BOOKS

我从《Python速成课程:一个实践的、基于项目的编程入门》一书中学习Python,第2E页。 在做游戏的时候,我得到了错误

Traceback (most recent call last):
File "d:\BOOKS\python\my-projects\AlienInvasion\tempCodeRunnerFile.py", line 69, in <module>
alinv = AlienInvasion()
File "d:\BOOKS\python\my-projects\AlienInvasion\tempCodeRunnerFile.py", line 22, in __init__
self.ship = Ship(self.screen)
File "d:\BOOKS\python\my-projects\AlienInvasion\ship.py", line 10, in   __init__
self.screen = alinv_game.screen
AttributeError: 'pygame.Surface' object has no attribute 'screen'
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)

我认为错误应该与screen声明有关,因为我认为Pygame.Surface没有“screen”属性,但我们可以通过get_rect方法获得它。请帮我找到那只虫子。我无法解决这个问题。

我相信你的意思是说
self.ship=ship(self.screen)
self.ship=ship(self)
。这样,
Ship
对象就可以访问
alieninvision
对象的所有属性,而不仅仅是它的
screen
属性(该属性没有
screen
属性,因此会出现错误)

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)
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