您好,我试图检测" w"按下键,我不断收到错误,无法查看我出错的地方。感谢你的建议。
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.key == pygame.K_w: #line 82
player.walkNorthAnimation()
t.displayTree()
错误是:
Traceback (most recent call last):
File "unnamed.py", line 91, in <module>
main()
File "unnamed.py", line 82, in main
if event.key == pygame.K_w:
AttributeError: event member not defined
答案 0 :(得分:1)
在使用event.type == pygame.KEYDOWN
之前,您必须先检查event.type == pygame.KEYUP
或event.key
,因为并非所有事件都已定义event.key
。
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w: #line 82
player.walkNorthAnimation()
请参阅PyGame文档:Event
QUIT none
ACTIVEEVENT gain, state
KEYDOWN unicode, key, mod
KEYUP key, mod
MOUSEMOTION pos, rel, buttons
MOUSEBUTTONUP pos, button
MOUSEBUTTONDOWN pos, button
JOYAXISMOTION joy, axis, value
JOYBALLMOTION joy, ball, rel
JOYHATMOTION joy, hat, value
JOYBUTTONUP joy, button
JOYBUTTONDOWN joy, button
VIDEORESIZE size, w, h
VIDEOEXPOSE none
USEREVENT code
答案 1 :(得分:0)
使用dir(event)
显示事件的属性,以查看event.key
是否确实存在。