gpt4 book ai didi

python - 属性错误 : member not defined python

转载 作者:太空宇宙 更新时间:2023-11-04 05:39:16 26 4
gpt4 key购买 nike

您好,我正在尝试检测是否按下了“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

最佳答案

在使用 event.key 之前,您必须检查 event.type == pygame.KEYDOWNevent.type == pygame.KEYUP > 因为并非所有事件都定义了 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

关于python - 属性错误 : member not defined python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34575274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com