Python中出现AttributeError: 'float' object has no attribute?

嘻哈
  • 1
新手上路,请多包涵

按照书上的编写的一个python的飞船游戏 代码也都是按照书上的编写的 但就不知道问题出在哪里
下面是报错信息:

E:\Python_test\Alien_invasion\Scripts\python.exe E:/pythonProject/alien_invasion.py
pygame 2.0.1 (SDL 2.0.14, Python 3.7.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "E:/pythonProject/alien_invasion.py", line 27, in <module>
    run_game()
  File "E:/pythonProject/alien_invasion.py", line 23, in run_game
    ship.update()
  File "E:\pythonProject\ship.py", line 31, in update
    self.center += self.ai_settings.ship_speed_factor
AttributeError: 'float' object has no attribute 'ship_speed_factor'

到运行飞船左右移动的时后,按下左右键就报错:
下面是源码:
alien_invasion.py

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



def run_game():
    pygame.init()
    # 定义窗口大小
    al_setting = Settings()
    screen = pygame.display.set_mode((al_setting.screen_winth,al_setting.screen_height))
    pygame.display.set_caption("Alien Invasion")
    ai_settings = al_setting.ship_speed_factor

    ship = Ship(ai_settings, screen)

    # 游戏主循环
    while True:
        # 监听键盘鼠标
        gf.check_events(ship)
        ship.update()
        # 显示界面
        gf.update_screen(al_setting,screen,ship)

run_game()

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/airplane.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):
        """根据移动标志调整飞船位置"""
        if self.moving_right:
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_left:
            self.center -= self.ai_settings.ship_speed_factor

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

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

settings.py

class Settings():
    def __init__(self):
        """初始化游戏设置"""
        self.screen_winth = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)
        # 飞船的飞行速度
        self.ship_speed_factor = 1.5

game_functions.py

import sys
import pygame

import ship


def check_events(ship):
    """监听键盘和鼠标事件"""

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                #向右移动飞船
                ship.moving_right = True
            elif event.key == pygame.K_LEFT:
                #向右移动飞船
                ship.moving_left = True

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = False
            elif event.key == pygame.K_LEFT:
                ship.moving_left = False

def update_screen(ai_settings, screen, ship):
    """更新屏幕上的图像,并切换到新屏幕"""
    # 每次循环都会重绘屏幕
    screen.fill(ai_settings.bg_color)
    ship.blitem()

    #刷新屏幕,是最新绘制的屏幕可见
    pygame.display.flip()

这是全部代码了,运行时一按左右键,就报错了,怎不知道哪里错了,麻烦各位大佬帮忙看看,指正一下哪里的问题,谢谢。

回复
阅读 449
1 个回答

在你的run_game函数里,把ai_setting赋值成了float


def run_game():
    pygame.init()
    # 定义窗口大小
    al_setting = Settings()
    screen = pygame.display.set_mode((al_setting.screen_winth,al_setting.screen_height))
    pygame.display.set_caption("Alien Invasion")
    ai_settings = al_setting.ship_speed_factor

你把ai_setting和 al_setting弄错了。
而且ship的初始化参数应该是setting对象,而非float的飞行速度。

你知道吗?

宣传栏