报错:'Event' object has no attribute 'key'

在我的game_function中出现的错误,直接在check-events中加入按q退出选项就会出现这个报错,但是放在check-down中却提示这个错误
game——function:
importsys
importpygame
frombulletimportBullet
defcheck_keydown_events(event,ai_settings,screen,ship,bullets):
ifevent.key==pygame.K_RIGHT:
ship.moving_right=True
elifevent.key==pygame.K_LEFT:
ship.moving_left=True
elifevent.key==pygame.K_SPACE:
fire_bullet(ai_settings,screen,ship,bullets)
elifevent.key==pygame.K_q:
sys.exit()
deffire_bullet(ai_settings,screen,ship,bullets):
iflen(bullets)new_bullet=Bullet(ai_settings,screen,ship)
bullets.add(new_bullet)
defcheck_keyup_events(event,ship):
ifevent.key==pygame.K_RIGHT:
ship.moving_right=False
elifevent.key==pygame.K_LEFT:
ship.moving_left=False
defcheck_events(ai_settings,screen,ship,bullets):
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
sys.exit()
elifevent.type==pygame.KEYDOWN:
check_keydown_events(event,ai_settings,screen,ship,bullets)
elifevent.type==pygame.KEYUP:
check_keyup_events(event,ship)
elifevent.key==pygame.K_w:
sys.exit()
defupdate_screen(ai_settings,screen,ship,bullets):
screen.fill(ai_settings.bg_color)
forbulletinbullets.sprites():
bullet.draw_bullet()
ship.blitme()
pygame.display.flip()
defupdate_bullets(bullets):
bullets.update()
forbulletinbullets.copy():
ifbullet.rect.bottom<=0:
bullets.remove(bullet)
按w就提示:
Event'objecthasnoattribute'key'
月关宝盒
浏览 2415回答 2
2回答

扬帆大鱼

elifevent.key==pygame.K_w:是不是应该改成:elifevent.type==pygame.K_w:

呼啦一阵风

因为不是所有的event都有KEY值,所以你必须先判断这个event类型是什么,然后再去用他的KEY,你直接用KEY,这个属于语法错误了
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript