So i am making a game with pygame for a school project. I was just freestyling a bit and decided to try and make movement for my character. I did it with the pygame.key.get_pressed function since that was an example in class. I went a bit further with this and wanted to make the diagonal speed different. so i wrote some logic to "try" and solve this. Here is the relevant bit of code i wrote. all of this is in a single class and inside a single function which i defined which i use in my while loop.
keys = pygame.key.get_pressed() # checking pressed keys
if keys[pygame.K_d] and (keys[pygame.K_w] == keys[pygame.K_s]):
self.x_p += self.v_1
if keys[pygame.K_a] and (keys[pygame.K_w] == keys[pygame.K_s]):
self.x_p -= self.v_1
if keys[pygame.K_w] and (keys[pygame.K_d] == keys[pygame.K_a]):
self.y_p -= self.v_1
if keys[pygame.K_s] and (keys[pygame.K_d] == keys[pygame.K_a]):
self.y_p += self.v_1
if (keys[pygame.K_s] and keys[pygame.K_d]) and not (keys[pygame.K_w] or keys[pygame.K_a]):
self.y_p += self.v_2
self.x_p += self.v_2
if (keys[pygame.K_a] and keys[pygame.K_s]) and not (keys[pygame.K_w] or keys[pygame.K_d]):
self.y_p += self.v_2
self.x_p -= self.v_2
if (keys[pygame.K_d] and keys[pygame.K_w]) and not (keys[pygame.K_s] or keys[pygame.K_a]):
self.y_p -= self.v_2
self.x_p += self.v_2
if (keys[pygame.K_w] and keys[pygame.K_a]) and not (keys[pygame.K_s] or keys[pygame.K_d]):
self.y_p -= self.v_2
self.x_p -= self.v_2
I use v1 and v2 because i want a constant speed that stays the same regardless of if you are moving straight or diagonally. But the problem is that if holding w and d you go, as expected, to the upper right. if i then hold s it should go only to the right. but instead it doesnt change its movement. To make it even weirder when i let go of w and d the motion to the top right continues. I know this question might be very stupid, but i would like to know why my code doesn't work right now.
So i tried to solve this myself and i came to the conclusion that when i press both w and s the expression (keys[pygame.K_w] == keys[pygame.K_s])
should be true. and this works, but if i hold w and d first and then press s, the expresion, which should turn true, remains false. So my problem really comes down to if this is a known issue of a computer not registering an input sometimes or if there is something else wrong with my code or perhaps even a pygame thing. so I think it doesn't register the input of the s on the keyboard. So if anyone could please help, you would save my day:).
But perhaps it is a very different problem altogether.