本文介绍了属性错误:成员未定义python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在尝试检测是否按下了"w"键,并且不断出现错误,看不到哪里出了问题.感谢您的建议.
Hi there I'm trying to detect if the "w" key is pressed and I keep getting an error and can't see where I've went wrong. Grateful for advice.
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
推荐答案
使用event.key
之前必须检查event.type == pygame.KEYDOWN
或event.type == pygame.KEYUP
,因为并非所有事件都定义了event.key
.
You have to check event.type == pygame.KEYDOWN
or event.type == pygame.KEYUP
before you use event.key
because not all events have event.key
defined.
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文档:事件
see PyGame documentation: 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
这篇关于属性错误:成员未定义python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!