Python抛出AttributeError:'Ship'对象没有属性'bullet_width'

时间:2017-06-15 17:14:45

标签: python python-3.x pygame

有人可以帮我修复此错误:

AttributeError: 'Ship' object has no attribute 'bullet_width'. 

我还在学习。我只是想从书中创建一个名为“Alien Invasion”的游戏,但每当我试图按照空间进行操作时它会抛出

AttributeError: 'Ship' object has no attribute 'bullet_width'. 

我甚至不知道错误意味着什么,除了我错过了某个地方的请求。

对我所做错的任何帮助和解释都将不胜感激。先感谢您。很抱歉,如果它让你感到不舒服,可能会有一些中国人,因为我来自中国。

以下是我的模特:

alien_invasion.py

import sys
import pygame
from pygame.sprite import Group

from settings import Settings
from ship import  Ship
import game_functions as gf

def run_game():
    #初始化pygame、设置和屏幕对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_hight))
    pygame.display.set_caption("Alien Invavsion")

    #设置背景色
    bg_color=(230,230,230)

    #创建一艘飞船
    ship = Ship(ai_settings, screen)
    #创建一个用于存储子弹的编组
    bullets = Group()

    #开始游戏的主循环
    while True:
        #监视键盘和鼠标事件
        gf.check_events(ai_settings, screen, ship, bullets)
        ship.update()
        bullets.update()
        gf.updata_screen(ai_settings, screen, ship, bullets)

run_game()

settings.py

class Settings():
    """存储《外星人入侵》的所有设置的类"""

    def __init__(self):
        """初始化游戏的设置"""
        #屏幕设置
        self.screen_width = 1200
        self.screen_hight = 700
        self.bg_color = (250,250,250)

        #飞船的位置
        self.ship_speed_factor = 1.5

        #子弹设置
        self.bullet_speed_factor = 1
        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = 60, 60, 60

game_functions.py

import sys
from bullet import Bullet
import pygame

def check_keydown_events(event, ai_settings, screen, ship, 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(ai_settings, screen, ship, bullets):
    """响应按键和鼠标事件"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ship, screen, bullets, ai_settings)
        elif event.type == pygame.KEYUP:
                check_keyup_events(event,ship)


def updata_screen(ai_settings, screen, ship, bullets):
    """更新屏幕上的图像,并切换到新屏幕"""
    # 每次循环时都重绘屏幕
    screen.fill(ai_settings.bg_color)
    #在飞船和外星人后面重绘所有子弹
    for bullet in bullets.sprites():
        bullet.draw_bullet()

    ship.blitme()

    # 让最近绘制的屏幕可见
    pygame.display.flip()

ship.py

import pygame

class Ship():
    def __init__(self, ai_settings, screen):
        """初始化飞船并设置其初始位置"""
        self.screen=screen
        self.ai_settings = ai_settings

        #加载飞船图像并获取其外接矩形
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        #将每艘新飞船放在屏幕底部中央
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        #在飞船的属性center中存储小数值
        self.center = float(self.rect.centerx)

        #移动标志
        self.moving_right = False
        self.moving_left = False

    def update(self):
        """根据移动标志调整飞船的位置"""
        #更新飞船的center值,而不是rect
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_left and self.rect.left > 0:
            self.center -= self.ai_settings.ship_speed_factor

        #根据self.centerx跟新的rect对象
        self.rect.centerx = self.center

    def blitme(self):
        """在指定位置绘制飞船"""
        self.screen.blit(self.image, self.rect)

bullet.py

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    """一个对飞船发射子弹的进行管理的类"""

    def __init__(self, ai_settings, screen, ship):
        """在飞船所处位置创建一个子弹对象"""
        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.conterx = ship.rect.centerx
        self.rect.top = ship = 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):
        """向上移动子弹"""
        #更新表示子弹位置的小数值
        self.y -= self.speed_factor
        #更新表示子弹的rect的位置
        self.rect.y = self.y


    def draw_bullet(self):
        """在屏幕上绘制子弹"""
        pygame.draw.rect(self.screen, self.color, self.rect)

3 个答案:

答案 0 :(得分:1)

Ship对象正在访问它的bullet_width属性,而这种情况并不存在。看起来你混淆了一些参数的顺序,所以它认为船是一个具有bullet_width属性的对象,如Bullet()Settings()

{+ 1}}的Ctrl + F:

.bullet_width
class Settings():
    def __init__(self):
        self.bullet_width = 3

似乎是第二个。 Ctrl + F代表class Bullet(Sprite): def __init__(self, ai_settings, screen, ship): self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)

Bullet(

好的,现在按Ctrl + F查看def check_keydown_events(event, ai_settings, screen, ship, bullets): new_bullet = Bullet(ai_settings, screen, ship)

check_keydown_events(

AHAH!您可能需要def check_events(ai_settings, screen, ship, bullets): elif event.type == pygame.KEYDOWN: check_keydown_events(event, ship, screen, bullets, ai_settings) ,因为check_keydown_events(event, ai_settings, screen, ship, bullets)被传递为ship,这会导致错误。

当你遇到这样的错误时,你知道某些东西不是它所说的那样。在这种情况下,使用调试器并在错误发生之前放置一个断点,然后检查对象。无论如何,方便的ai_settings会立即向您显示问题所在:

print('DEBUG:', everything_neccessary)

答案 1 :(得分:0)

至少从上面的代码中你的Ship对象没有bullet_width。它看起来像保存在ai_settings中。以上是完整的代码吗?或者您的代码中是否还有其他位置,当您确实需要输入ship.bullet_width并且发生错误时,您可能会尝试访问ship.ai_settings.bullet_width

答案 2 :(得分:-1)

我也在学习python,我找到了2个问题供你参考: 1. game_functions.py:    一个。 updata是错误的,应该更新    湾check_keydown_events中的顺序错误*(check_keydown_events(事件,发货,屏幕,项目符号,ai_settings)*,正确的顺序如下:    check_keydown_events(事件,ai_setting,屏幕,船舶,子弹) 2. alien_invasion.py bg_color与ship.py重复对比 (换句话说,你已经在ship.py中设置了属性&#34; bg_c​​olor&#34;没有必要在alien_invasion.py中重复它)

希望能帮到你〜