《python编程从入门到实践》中在学习外星人入侵项目中运行程序时出现报错 AttributeError: 'Event' object has no attribute 'key'
错误代码如下:
# coding = utf-8
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""管理游戏资源与行为的类"""
def __init__(self):
"""初始化游戏并创建游戏资源"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
"""开始游戏的主循环"""
while True:
self._check_events()
self.ship.update()
self._update_screen()
def _check_events(self):
"""响应按键和鼠标事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)‘
#按Q键退出游戏
elif event.key == pygame.K_q:
sys.exit()
def _check_keydown_events(self,event):
"""响应按键"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
def _check_keyup_events(self,event):
"""响应松开"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _update_screen(self):
"""更新屏幕上的图像,并切换到新屏幕"""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
pygame.display.flip()
if __name__ == "__main__":
#创建游戏实例并运行游戏
ai = AlienInvasion()
ai.run_game()
运行错误提示
C:\Users\鱼语雨\Desktop\python\python\alien_invasion>python alien_invasion.py
pygame 2.1.2 (SDL 2.0.18, Python 3.10.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\鱼语雨\Desktop\python\python\alien_invasion\alien_invasion.py", line 69, in <module>
ai.run_game()
File "C:\Users\鱼语雨\Desktop\python\python\alien_invasion\alien_invasion.py", line 25, in run_game
self._check_events()
File "C:\Users\鱼语雨\Desktop\python\python\alien_invasion\alien_invasion.py", line 39, in _check_events
elif event.key == pygame.K_q:
AttributeError: 'Event' object has no attribute 'key'
导致错误的原因为“ #按Q键退出游戏”这部分程序中“elif event.key == pygame.K_q:”这句语句写在了与 事件类型 “event.type == pygame.KEYDOWN:” 并列的位置上。
“KEYDOWN”和“KEYUP”作为 键盘事件,其提供了一个成员属性key,通过该属性来获取本次键盘事件的内容,及按下了那个按键,所以“event.key”不能与“event.type”处于并列位置。
使用pygame处理事件时,逻辑一般是先判断事件类型,然后在根据不同的事件操作,执行不同的游戏操作,及先判断事件类型,再判断事件内容。
正确代码如下:
# coding = utf-8
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""管理游戏资源与行为的类"""
def __init__(self):
"""初始化游戏并创建游戏资源"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
"""开始游戏的主循环"""
while True:
self._check_events()
self.ship.update()
self._update_screen()
def _check_events(self):
"""响应按键和鼠标事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)
def _check_keydown_events(self,event):
"""响应按键"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
#按Q键退出游戏
elif event.key == pygame.K_q:
sys.exit()
def _check_keyup_events(self,event):
"""响应松开"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _update_screen(self):
"""更新屏幕上的图像,并切换到新屏幕"""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
pygame.display.flip()
if __name__ == "__main__":
#创建游戏实例并运行游戏
ai = AlienInvasion()
ai.run_game()