即使在游戏的新开始之后
使用Pygame时,我有一个奇怪的行为。
[...]
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
[...]
while running:
[...]
dt = (new_time - start_time).total_seconds()
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
bot.brake(dt)
elif keys[pygame.K_UP]:
bot.accelerate(dt)
有时,即使我不推动任何钥匙,它也会立即开始加速机器人。当我调试时,即使我不触摸键盘,我也会发现钥匙被压在Pygames上。即使是在游戏的新开始,也会发生这种情况。
您能帮我找到这个问题的原因吗?
I have a strange behavior with when using pygame.
[...]
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
[...]
while running:
[...]
dt = (new_time - start_time).total_seconds()
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
bot.brake(dt)
elif keys[pygame.K_UP]:
bot.accelerate(dt)
Sometimes, even if I am not pushing any key, it instantaneously starts to accelerate the bot. When I debug, I found that the key is pressed for pygames, even if I don't touch the keyboard. That happens even on a fresh start of the game.
Can you help me find the cause of this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也许可以使用变量来进行操纵并弄清楚要按哪些键。尽管我喜欢在另一个功能中检测按键。
现在,您必须创建一个函数来控制玩家。
然后,您将这两个功能实现到“运行”脚本中
You may be able to use variables to manupulate and figure out what keys you're pressing. Though I like detecting Keypress in another function.
Now you have to create a function to control you player.
Then you implement both of these functions into the "While running" script