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

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

请帮我解决错误。 我已经仔细检查了所有内容,但没有任何效果,同样的回溯 这是完整的回溯:

File "E:\Python\Alien_Invasion\alien_invasion.py", line 29, in <module>
run_game()
File "E:\Python\Alien_Invasion\alien_invasion.py", line 23, in run_game
gf.check_events(ai_settings, ship, screen, bullets)
File "E:\Python\Alien_Invasion\game_functions.py", line 29, in check_events
check_keydown_events(event, ship, ai_settings, screen, bullets)
File "E:\Python\Alien_Invasion\game_functions.py", line 13, in check_keydown_events
new_bullet = Bullet(ai_settings, screen, ship)
File "E:\Python\Alien_Invasion\bullet.py", line 12, in __init__
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)
AttributeError: 'pygame.Surface' object has no attribute 'bullet_width'

这是有问题的“bullet.py”文件的代码:

class Bullet(Sprite):
def __init__(self, ai_settings, ship, screen):
    #Создаєм обєкт пулі
    super(Bullet, self).__init__()
    self.screen = screen

    #Создаем пулю в позиции (0,0)
    self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height) 
    self.rect.centerx = ship.rect.centerx
    self.rect.top = ship.rect.top

    #Позиция пуле задана дробным числом
    self.y = float(self.rect.y)

    self.color = ai_settings.bullet_color
    self.speed_factor = ai_settings.bullet_speed_factor

def update(self):
    #Перемещает пулю по y
    self.y -= self.speed_factor
    #Обновлює позицію пулі в float 
    self.rect.y = self.y

def draw_bullet(self):
    #Выводит пули на экран
    pygame.draw.rect(self.screen, self.color, self.rect)

这是一个 class 设置实例化 Bullet 对象

class Settings():
    def __init__(self):
        self.screen_width = 1200
        self.screen_hight = 800
        self.bg_color = (20,228,255)
        self.ship_speed_factor = 1

        #Параметры пуль 
        self.bullet_speed_factor = 1
        self.bullet_width = 3 
        self.bullet_height = 15
        self.bullet_color = 253,251,255

这是发生 while 循环的主要模块。

import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group

def run_game():
    #Ініціалізує pygame, setting і створює екран
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, 
                                      ai_settings.screen_hight)) 
    pygame.display.set_caption("Alien Invasion")
    #Создаєм корабль
    ship = Ship(screen, ai_settings)

    #Створюєм группу для хранения пуль 
    bullets = Group()

    #Запуск основного циклу гри
    while True:
        gf.check_events(ai_settings, ship, screen, bullets)
        ship.update()
        ship.blitme()
        bullets.update()
        gf.update_screen(ai_settings, screen, ship, bullets)

run_game()


这是一个功能性游戏程序模块

import sys
import pygame
from bullet import Bullet 

def check_keydown_events(event, ship, screen, ai_settings, bullets):
    #Нажатие клавиш
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    elif event.key == pygame.K_SPACE:
        #Создание новой пули и занесение её в группу bullets
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)

def check_keyup_events(event, ship):
    #Отпуск клавиш
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False

def check_events(ship, ai_settings, screen, bullets):
    #Обработка нажатия клавиш и мышки
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ship, ai_settings, screen, bullets)

def update_screen(ai_settings, screen, ship, bullets):  
    #Обновляет экран, и последнюю картинку
    for bullet in bullets.sprites():
        bullet.draw_bullet()

    screen.fill(ai_settings.bg_color)
    ship.blitme()
    #Отображает последний экран
    pygame.display.flip()



的签名是check_keydown_events

def check_keydown_events(event, ship, screen, ai_settings, bullets):

调用 check_events 中的函数时,您不小心交换了 shipai_settings 参数

check_keydown_events(event, ship, ai_settings, screen, bullets)

正确的叫法是:

check_keydown_events(event, screen, ship, ai_settings, bullets)