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

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)

到这里为止就是全部的程序了,请问哪位大佬可以指点一下,谢谢?

python object has no attribute_Python中出现AttributeError: object has no attribute相关推荐

  1. Python中type和object的关系

    知乎上看到的提问: 两个是互为实例的关系,但不是互为子类的关系,只有type是object的子类,反之则不成立. 大牛说两者是蛋生鸡鸡生蛋的关系,但我还是不明白,有懂的麻烦解释一下, 希望不要给出外文 ...

  2. 成功解决keras库中出现AttributeError: ‘str‘ object has no attribute ‘decode‘

    成功解决keras库中出现AttributeError: 'str' object has no attribute 'decode' 目录 解决问题 解决思路 解决方法 解决问题 Attribute ...

  3. Python中出现:AttributeError: module 'numpy' has no attribute 'dtype'问题解决

    QUESTION:Python中出现:AttributeError: module 'numpy' has no attribute 'dtype'问题解决 ANWSER: 这个问题可是困扰了我一天的 ...

  4. python中的object是什么意思_在Python中使用’@ patch.object’和’with patch.object’有什么区别?...

    在为我的应用程序编写单元测试时,我一直在使用@ mock.patch和@ patch.object装饰器.但是现在,对于我使用装饰器的一些单元测试,我收到一个错误'TypeError:staticme ...

  5. python编写ATM类_Python中编写类的各种技巧和方法

    有关 Python 内编写类的各种技巧和方法(构建和初始化.重载操作符.类描述.属性访问控制.自定义序列.反射机制.可调用对象.上下文管理.构建描述符对象.Pickling).你可以把它当作一个教程, ...

  6. python 类继承object_python-面向对象-17-继承/object类/mro方法

    1.继承的基本概念 继承是面向对象程序设计的重要特征,也是实现"代码复用"的重要手段. 如果一个新类继承自一个设计好的类,就直接具备已有类的特征,这样就大大降低了工作难度,因为很多 ...

  7. python redis 消息队列_python中利用redis构建任务队列(queue)

    Python中的使用标准queue模块就可以建立多进程使用的队列,但是使用redis和redis-queue(rq)模块使这一操作更加简单. Part 1. 比如首先我们使用队列来简单的储存数据:我们 ...

  8. python装饰器类-Python 装饰器装饰类中的方法

    title: Python 装饰器装饰类中的方法 comments: true date: 2017-04-17 20:44:31 tags: ['Python', 'Decorate'] categ ...

  9. python装饰器实例-基于Python 装饰器装饰类中的方法实例

    title: Python 装饰器装饰类中的方法 comments: true date: 2017-04-17 20:44:31 tags: ['Python', 'Decorate'] categ ...

最新文章

  1. php前面有人排队,PHP实现打印类(实现队列排队打印)
  2. androidinclude作用
  3. 【二维费用的01背包 HDU3496    HDU2184】
  4. pytorch 常用层(四)
  5. ITK:布雷森汉姆线BresenhamLine
  6. React开发(211):react中refs转发到dom组件
  7. open×××结合pam_mysql认证出错的解决方法
  8. 使用 pycharm安装各个模块
  9. 从汇编的眼光看C++(之指针拷贝)
  10. [转载] 老版本ubuntu 更新源
  11. Android Studio使用Android Annotations注解框架笔记
  12. python编程软件哪个好-Python开发工具哪个好?好用的Python开发工具排行榜推荐下载...
  13. Hive语言手册-ORC
  14. 服务器ftp查看文件,ftp命令查看文件列表 - 卡饭网
  15. 台式电脑主板插线步骤图_台式电脑主板接线图解
  16. pomelo之master服务器的启动
  17. 利用神经网络预测股票价格走势
  18. 编译原理(一)编译程序、解释程序、程序设计语言范型
  19. 复习之JavaScript基本语法(三)——getElement[...]方法使用
  20. vue页面fav icon

热门文章

  1. 更换虚拟机的内核版本
  2. SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
  3. Bootstrap实现手风琴效果
  4. servlet中@webServlet注解出错导致404
  5. 【深度学习训练小技巧】1080ti与2080ti区别、apex与梯度累加
  6. 【luogu2835】 刻录光盘
  7. js replace()去除代码中空格
  8. 录屏软件录制视频,如何转换成MP4的格式?
  9. Unity渲染流程概述
  10. 网络学习:交换机里的常用命令