yy499121268 2021-05-31 15:42 采纳率: 50%
浏览 4295
已采纳

AttributeError: 'float' object has no attribute

按照书上的编写的一个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()

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

  • 写回答

3条回答 默认 最新

  • CSDN专家-HGJ 2021-05-31 15:59
    关注

    在函数update中self.moving_right: self.center += self.ai_settings.ship_speed_factor 和后面一句用法错误,因为在run_game函数中传入的参数ai_settings已经是一个类属性了,即在Settings类中定义的浮点数,在update函数中调用时就不能再用ai_settings.ship_speed_factor,改成self.center += self.ai_settings,后面一句作同样修改。这样改:

    def update(self):
            """根据移动标志调整飞船位置"""
            if self.moving_right:
                self.center += self.ai_settings
            if self.moving_left:
                self.center -= self.ai_settings
     
            # 根据self.center更新rect对象
            self.rect.centerx = self.center

    如对解答满意,请点采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。