《python编程从入门到实践》12-5练习:'Group' object has no attribute ‘xxx’
错误:Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/untitled/qqq/cemiansheji12-5lianxi.py", line 28, in <module>
run_game()
File "C:/Users/user/PycharmProjects/untitled/qqq/cemiansheji12-5lianxi.py", line 24, in run_game
cgf.bullets_update(bullets)
File "C:UsersuserPycharmProjectsuntitledqqqcemiansheji_game_fuctions.py", line 42, in bullets_update
bullets.bullet_update()
AttributeError: 'Group' object has no attribute 'bullet_update'
文件cemiansheji_game_fuctions:
import pygameimport sys
from qqq import cemiansheji_bullet
def check_KEYDOWN_event(event,spacecraft,bullets,shezhi,screen):
if event.key == pygame.K_UP:
spacecraft.moving_up = True
elif event.key == pygame.K_DOWN:
spacecraft.moving_down = True
elif event.key == pygame.K_SPACE:
fire_bullet(bullets, shezhi, screen, spacecraft)
def check_KEYUP_event(event,spacecraft):
if event.key == pygame.K_UP:
spacecraft.moving_up = False
elif event.key == pygame.K_DOWN:
spacecraft.moving_down = False
def check_event(spacecraft,bullets,shezhi,screen):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_KEYDOWN_event(event,spacecraft,bullets,shezhi,screen)
elif event.type == pygame.KEYUP:
check_KEYUP_event(event,spacecraft)
def screen_update(screen,shezhi,bullets,spacecraft):
screen.fill(shezhi.background_color)
spacecraft.draw_spacecraft()
for bullet in bullets.sprites():
bullet.draw_bullet()
pygame.display.flip()
def bullets_update(bullets):
bullets.bullet_update()
for bullet in bullets.copy():
if bullet.rect.right >= 1000:
bullets.remove(bullet)
def fire_bullet(bullets,shezhi,screen,spacecraft):
if len(bullets) < shezhi.bullet_allowed:
new_bullet = cemiansheji_bullet.Bullet(screen,spacecraft,shezhi)
bullets.add(new_bullet)
##### 主体函数:
import pygame
from qqq import cemiansheji_setting
from qqq import cemiansheji_spacecraft
from qqq import cemiansheji_game_fuctions as cgf
from pygame.sprite import Group
def run_game():
# 初始化游戏并创立一个屏幕对象pygame.init()
shezhi = cemiansheji_setting.Setting()
screen = pygame.display.set_mode((shezhi.screen_width,shezhi.screen_height))
pygame.display.set_caption("SPACECRAFT")
# 创建一艘宇宙飞船
spacecraft = cemiansheji_spacecraft.Spacecraft(shezhi,screen)
# 创立子弹编组
bullets = Group()
while True:
# 监视键盘和鼠标事件,有方向按键就往这个方向移动并且实施
cgf.check_event(spacecraft,bullets,shezhi,screen)
spacecraft.spacecraft_update()
cgf.bullets_update(bullets)
# 每次循环都重绘屏幕
cgf.screen_update(screen,shezhi,bullets,spacecraft)
run_game()
回答:
bullet_update => bullets_update
以上是 《python编程从入门到实践》12-5练习:'Group' object has no attribute ‘xxx’ 的全部内容, 来源链接: utcz.com/a/159998.html