Reputation: 128
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
Same would go for player2
except [K.LEFT, K.RIGHT, K.UP, K.DOWN]
Any help would be appreciated
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?
Upvotes: 2
Views: 311
Reputation: 4739
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
Upvotes: 2