Python pygame 需要帮助一次禁用多个按键 [英] Python pygame need help disabling multiple keypresses at once
问题描述
我创建了一个 Frogger 游戏原型,我不想让两个键同时按下以进行移动.目前在我的事件功能中,我有以下内容:
I have created a Frogger Game Prototype and I'm wanting to not allow two keys to be pressed at the same time for movement. currently in my event function I have the following:
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.player.speedx = -48
if event.key == pygame.K_RIGHT:
self.player.speedx = 48
if event.key == pygame.K_UP:
self.player.speedy = -48
if event.key == pygame.K_DOWN:
self.player.speedy = 48
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
self.player.speedx = 0
self.player.speedy = 0
if event.key == pygame.K_RIGHT:
self.player.speedx = 0
self.player.speedy = 0
if event.key == pygame.K_UP:
self.player.speedx = 0
self.player.speedy = 0
if event.key == pygame.K_DOWN:
self.player.speedx = 0
self.player.speedy = 0
self.player.rect.x += self.player.speedx
self.player.rect.y += self.player.speedy
这允许用户向左、向右、向上和向下移动.尽管我不希望用户能够同时按下两个键,例如:向左,向上或向右,向上,但它运行正常.还想排除另一个方向,例如:向左、向下或向右、向下.基本上我希望用户一次只能朝一个方向行驶.另外,无论如何要在按键之间设置时间限制吗?例如,您不能在 1000 毫秒过去之前再次按下某个键.
This allows the user to move left, right, up, and down. This is functioning properly although I don't want the user to be able to press two keys together ex: left, up or right, up. Also want to exclude the other direction as well ex: left, down or right, down. Basically I want the user only to be able to travel in one direction at a time. Also, is there anyway to put a time limit in between keypresses? For example you cannot press a key again before 1000 miliseconds have passed.
推荐答案
你所要做的就是评估按下一个键时速度为 0:
All you have to do is evaluate that the speed is 0 when a key is pressed:
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pygame.KEYDOWN:
if self.player.speedx == 0 and self.player.speedy == 0:
if event.key == pygame.K_LEFT:
self.player.speedx = -48
if event.key == pygame.K_RIGHT:
self.player.speedx = 48
if event.key == pygame.K_UP:
self.player.speedy = -48
if event.key == pygame.K_DOWN:
self.player.speedy = 48
# [...]
另一种选择是在按下 a 键时重置速度:
Another option is to reset the speed when the a key is pressed:
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.player.speedx = -48
self.player.speedy = 0
if event.key == pygame.K_RIGHT:
self.player.speedx = 48
self.player.speedy = 0
if event.key == pygame.K_UP:
self.player.speedx = 0
self.player.speedy = -48
if event.key == pygame.K_DOWN:
self.player.speedx = 0
self.player.speedy = 48
这两种实现方式不同.虽然第一个实现不允许您在按键时改变方向,但第二个实现立即改变方向.两者都试试,然后选择适合您的需求.
The two implementations behave differently. While the first implementation does not allow you to change direction while a key is pressed, the second implementation changes direction immediately. Try both and choose the one that suits your needs.
这篇关于Python pygame 需要帮助一次禁用多个按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!