gpt4 book ai didi

Python事件没有Key属性

转载 作者:行者123 更新时间:2023-11-30 22:33:59 25 4
gpt4 key购买 nike

我正在尝试为我的角色移动创建一个程序。这是我的代码

while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if(event.key == K_RIGHT):
playerPos[0] +=1
if (event.key == K_RIGHT) and playerPos[0] < MAPWIDTH -1:
playerPos[0] =+ 1

这是错误

Traceback (most recent call last):
File "H:\python things\firstgame.py", line 90, in <module>
if (event.key == K_RIGHT) and playerPos[0] < MAPWIDTH -1:
AttributeError: 'Event' object has no attribute 'key'

最佳答案

如果有人遇到这篇文章并遇到同样的问题,我修复了我的错误并最终解决了这个问题。添加评论以帮助其他人理解

while True:
#get all the user events
for event in pygame.event.get():

#if user wants to quit
if event.type == pygame.locals.QUIT:
#and the game close the window
pygame.quit()
sys.exit()

#if a key is pressed
elif event.type == pygame.locals.KEYDOWN:
#if right arrow is pressed
if event.key == K_RIGHT and playerPos[0] < MAPWIDTH - 1:
#change player's x postion
playerPos[0] +=1
if event.key == K_LEFT and playerPos[0] > 0:
#change player x position
playerPos[0] -=1
if event.key == K_UP and playerPos[0] > 0:
#change players x position
playerPos[1] -=1
if event.key == K_DOWN and playerPos[1] < MAPHEIGHT -1:
#change player x position
playerPos[1] += 1

关于Python事件没有Key属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44983710/

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