出现了一个error,求助大神
在看一本书,Python编程 从入门到实践遇到了一个问题,就是我下面的算法(功能:实现飞船的左右移动,按住空格进行射击),
问题::Ship()里面没有对于 bullet_width 的描述,不需要,但是为什么提示弹出错误??
代码如下:(初学者 代码比较混乱)
import pygame
import sys
from pygame.sprite import Sprite
from pygame.sprite import Group
#事件检查
def check_keydown_events(event,ai_setting,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:
new_bullet=Bullet(ai_setting,screen,ship)
bullets.add(new_bullet)
def check_keyup_events(event,ship):
#按键反馈
if event.key==pygame.K_RIGHT:
ship.moving_right=False
if event.key==pygame.K_LEFT:
ship.moving_left=False
def check_event(ai_setting,screen,ship,bullets):
for event in pygame.event.get():#利用pygame里面的事件获取机制,判断退出/按下/弹起
if event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
check_keydown_events(event,ship,screen,ship,bullets)
check_keydown_events(event,ai_setting,screen,ship,bullets)
elif event.type==pygame.KEYUP:
check_keyup_events(event,ship)
#屏幕更新
def update_screen(ai_setting,bg_color,screen,ship,bullets):
screen.fill(bg_color)
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
#让最近的屏幕可视
pygame.display.flip()
class Settings():
#储存关于游戏的初始化的变量
def __init__(self):
#初始化页面
self.screen_width=1200
self.screen_height=800
self.bg_color=(230,230,230)# R/G/B
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
class Ship():
#储存关于飞船的信息
def __init__(self,ai_setting,screen):
self.screen=screen
self.ai_setting=ai_setting
self.image=pygame.image.load("D:/PY/py6/aline_invasion/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
#移动标志
self.moving_right=False
self.moving_left=False
#在飞船的属性里面设置存储
self.center=float(self.rect.centerx)
def update(self):
#根据移动的标志调整飞船的位置
if self.moving_right and self.rect.right < self.screen_rect.right:
#self.rect.centerx += 1
self.center+=self.ai_setting.ship_speed_factor
if self.moving_left and self.rect.left > 0:
#self.rect.centerx -= 1
self.center-=self.ai_setting.ship_speed_factor
#根据中间的 self.center 更新 self.rect.center
self.rect.centerx=self.center
def blitme(self):
#在主屏幕绘制成像
self.screen.blit(self.image,self.rect)
class Bullet(Sprite):
#继承了pygame里面的Sprite的类
#这是对于子弹进行管理的类
def __init__(self,ai_setting,screen,ship):
super(Bullet,self).__init__() #继承
self.screen=screen
#在(0,0)那里创建一个子弹的矩形框。
self.rect=pygame.rect(0,0,ai_setting.bullet_width,ai_setting.bullet_height)
self.rect.centerx=ship.rect.centerx
self.rect.top=ship.rect.top
#存储用小数表示子弹
self.y=float(self.rect.y)
self.color=ai_setting.bullet_color
self.speed_factor=ai_setting.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)
#主函数
def run_time():
pygame.init()#初始化屏幕screen
ai_setting=Settings()#实例化
screen=pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height))
#元组意味着不可以修改
pygame.display.set_caption("郭真真吃香肠")#标题
ship=Ship(ai_setting,screen)
#创建一个存储子弹的编组,借用pygame的子弹pygame.sprite.Group
bullets=Group()
#捕捉退出机制
while True:
check_event(ai_setting,screen,ship,bullets)
#这里需要导入第三方的函数ship作为形式参数。
#下面的是刷新界面
ship.update()
#更新子弹位置
bullets.update()
#依据键盘输入更改坐标
update_screen(ai_setting,ai_setting.bg_color,screen,ship,bullets)
# screen.fill(ai_setting.bg_color)#在每次循环的时候自动填充背景
# ship.blitme() #飞船
# #让最近的屏幕可视
# pygame.display.flip()
run_time()
然后运行的时候,左右移动是没有问题的,但是按住空格按键准备射击的时候出现报错,错误如下:
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 142, in <module>
run_time()
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 128, in run_time
check_event(ai_setting,screen,ship,bullets)
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 29, in check_event
check_keydown_events(event,ship,screen,ship,bullets)
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 14, in check_keydown_events
new_bullet=Bullet(ai_setting,screen,ship)
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 98, in __init__
self.rect=pygame.rect(0,0,ai_setting.bullet_width,ai_setting.bullet_height)
AttributeError: 'Ship' object has no attribute 'bullet_width'
请按任意键继续. . .
求大神告知bug在哪里???感激。。。。 前面的标点错误,应该是逗号而不是. BngThea 发表于 2018-12-30 16:08
前面的标点错误,应该是逗号而不是.
大哥 能不能指一下是第几行??? check_keydown_events(event,ship,screen,ship,bullets)
check_keydown_events(event,ai_setting,screen,ship,bullets)
这2行才是问题主要 塔利班 发表于 2018-12-30 17:41
check_keydown_events(event,ship,screen,ship,bullets)
...
我改成了下面的,然后报错变更了。
#事件检查机制函数
def check_keydown_events(event,ship,screen,bullets,ai_setting):
#按下键盘
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:
new_bullet=Bullet(ai_setting,screen,ship)
bullets.add(new_bullet)
报错变成下面的内容:
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 141, in <module>
run_time()
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 127, in run_time
check_event(ai_setting,screen,ship,bullets)
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 29, in check_event
check_keydown_events(event,ai_setting,screen,ship,bullets)
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 14, in check_keydown_events
new_bullet=Bullet(ai_setting,screen,ship)
File "D:\PY\py6\aline_invasion\cha 项目1.py", line 97, in __init__
self.rect=pygame.rect(0,0,ai_setting.bullet_width,ai_setting.bullet_height)
AttributeError: 'Group' object has no attribute 'bullet_width'
请按任意键继续. . . 删掉29行
95行 super(Sprite,self)改为super()
页:
[1]