'pygame.Surface' object has no attribute 'alien_speed',完全按照教程写的,不知道哪错了?

'pygame.Surface' object has no attribute 'alien_speed',完全按照教程写的,不知道哪错了?

Traceback (most recent call last):

File "C:\Virtual Hard Drive D\Python project\venv\ALIEN INVASION\alien_invasion.py", line 52, in <module>

ai.run_game()

File "C:\Virtual Hard Drive D\Python project\venv\ALIEN INVASION\alien_invasion.py", line 20, in run_game

self.ship.update()

File "C:\Virtual Hard Drive D\Python project\venv\ALIEN INVASION\ship.py", line 19, in update

self.x += self.settings.ship_speed

AttributeError: 'pygame.Surface' object has no attribute 'ship_speed'

无法正常读取 setting.py 中的 ship_speed 取值,我将 ship.py 中的 self.x += self.settings.ship_speed 改成 self.x += 1 ,外星人群就可以正常右移了,请问是哪里出现问题了?

import sys

import pygame

from settings import Settings

from ship import Ship

class AlienInvasion:

def __init__(self):

pygame.init() # 初始化背景设置

self.settings = Settings()

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

self.screen = pygame.display.set_mode((1200,800))

pygame.display.set_caption("Alien Invasion")

self.ship = Ship(self)

self.bg_color = (230,230,230)

def run_game(self):

while True:

self._check_events()

self._update_screen()

self.ship.update()

def _check_events(self):

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

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_q:

sys.exit()

def _check_keyup_events(self,event):

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

self.screen.fill(self.bg_color)

self.ship.blitme()

pygame.display.flip()

if __name__ == '__main__':

ai = AlienInvasion()

ai.run_game()

class Settings:

def __init__(self):

self.screen_width = 1200

self.screen_height = 800

self.bg_color = (230, 230, 230)

self.ship_speed = 1.5

class Ship:

def __init__(self, ai_game):

self.screen = ai_game.screen

self.settings = ai_game.settings

self.screen_rect = ai_game.screen.get_rect()

self.image = pygame.image.load('images/ship.jpg')

self.rect = self.image.get_rect()

self.x = float(self.rect.x)

self.rect.midbottom = self.screen_rect.midbottom

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

self.rect.x = self.x

def blitme(self):

self.screen.blit(self.image, self.rect)


回答:

class AlienInvasion:

def __init__(self):

pygame.init() # 初始化背景设置

self.settings = Settings()

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

self.settings被覆盖了?


本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。

以上是 &#x27;pygame.Surface&#x27; object has no attribute &#x27;alien_speed&#x27;,完全按照教程写的,不知道哪错了? 的全部内容, 来源链接: utcz.com/p/938787.html

回到顶部