多人游戏同时按下多个键的问题 [英] Issue with multi keys being pressed at once for two player game
问题描述
我不确定这是我的编码问题还是我的计算机限制,但我似乎对在两人游戏中按下多个键有问题.按键本身工作正常,我也想要它们,但由于两个玩家可以同时按下 4 个按键(比如 W、A、UP和 LEFT) 如果他们都在加速前进并想向左或向右躲避某些东西,那么如果同时按下所有 4 个键,其中一个键似乎对其中一个玩家不起作用.... 如果 player1
释放 'up' (W),'left' (A) 会突然起作用 - 这是同时 player2
正在按向上" (UP) 并说向右 (RIGHT)
I am not sure if it is my coding or just my computers limitations but I seem to have issue with multiple keys being pressed in a two player game. The keys themselves work correctly and as I want them too, but since two players could be pressing 4 keys simultaneously (say W, A, UP and LEFT) if they are both speeding forward and want to dodge something left or right, it seems that one of the keys will not work for one of the players if all 4 are being pressed at same time.... 'left' (A) will suddenly work if player1
releases 'up' (W) - this is while simultaneously player2
is pressing 'up' (UP) and say right (RIGHT)
是否有更好的方法来请求关键事件/按下以使其停止发生,或者我的代码是否正常,也许我的计算机只是滞后?
Is there a better way to ask for key event / pressed for this to stop happening or is my code ok and maybe my computer is just lagging?
keystate1 = pygame.key.get_pressed()
if keystate1[pygame.K_a]:
self.speedx = -5
if keystate1[pygame.K_d]:
self.speedx = 5
if keystate1[pygame.K_w]:
self.speedy = -5
if keystate1[pygame.K.s]:
self.speedy = 5
同样适用于 player2
,除了 [K.LEFT, K.RIGHT, K.UP, K.DOWN]
Same would go for player2
except [K.LEFT, K.RIGHT, K.UP, K.DOWN]
任何帮助将不胜感激
经过进一步测试,当 player1
和 player1
按下 A 时,它似乎只会发生在 player1
上.player2
同时按下 (W & UP)...然后 player1
不会移动除非 player1
放开 W 或 player2 放开 UP.它根本不影响 player2
(UP、DOWN、LEFT、RIGHT) 或 player1
的任何其他方向,除了左 A?
Upon further testing it only seems to happen to player1
pressing A when both player1
& player2
are pressing up (W & UP) at same time... then player1
won't move left unless player1
lets go of W or player2 lets go of UP.
It doesn't affect player2
at all (UP, DOWN, LEFT, RIGHT) or any other direction of player1
except left A?
推荐答案
这是您键盘的硬件限制.由于它们的制造方式,它们中的大多数将无法注册同时按下的 4 个非修饰键
This is a hardware limitation of your keyboard. Most of them will not be able to register 4 non-modifier keys being pressed at the same time due to the way they are manufactured
这篇关于多人游戏同时按下多个键的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!