0

Keydown event is registering fine if i remove keyup events from my loop, but if i add keyup event then keydown event is not working. Here is my main game loop.

playercar= Car(GREENCAR)
left=False
right=False
speed=False
brake=False
while True:
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key== K_a:
                left=True
            if event.key== K_s:
                right=True
            if event.key== K_w:
                speed=True
            if event.key== K_z:
                brake=True
        if event.type==KEYUP:
            if event.key== K_a:
                left=False
            if event.key== K_s:
                right=False
            if event.key== K_w:
                speed=False
            if event.key== K_z:
                brake=False
    if left:
        playercar.rotatecar(left=True)
    if right:
        playercar.rotatecar(Right=True)
    if speed:
        playercar.speed()
    else:
        playercar.brake(brake=brake)                
    win.blit(GRASS,(0,0))
    win.blit(TRACK,(0,0))
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
  • Please explain what you mean by *event is not working*. Is an exception raised or is there an unexpected result? – qouify Oct 23 '21 at 05:10
  • If i remove keyup events from my code then everything is working fine except like if i press 'a' on my keyboard my player keeps turning left. And if i add keyup events (as shown in code above) then my player doesn't move at all. I am not getting any error but my player isn't moving. – Ashu Agrawal Oct 23 '21 at 05:32
  • I cannot see any obvious mistake. Could you post the full code? – qouify Oct 23 '21 at 07:35
  • I see that the rotate car method has keywords arguments `left` and `Right` (with capital `R`). Is that correct? – qouify Oct 23 '21 at 07:36
  • It seems that you cannot keep pressed a virtual key on your phone. – Rabbid76 Oct 23 '21 at 07:51
  • @Rabbid76 Phone??? Didn't think of that... – qouify Oct 23 '21 at 07:54
  • @qouify *"... i am running code in my android phone..."* from the comment to the deleted answer. – Rabbid76 Oct 23 '21 at 07:57
  • Yes i am running this in my android phone. – Ashu Agrawal Oct 23 '21 at 10:16
  • Is there any other way to solve that virtual key problem? – Ashu Agrawal Oct 23 '21 at 10:17
  • Because if i create a button on the screen then i can't keep pressed 2 buttons at the same time because MOUSE event can read only 1 touch position – Ashu Agrawal Oct 23 '21 at 10:20
  • 2
    You can use the `FINGERDOWN` and `FINGERUP` event. See [No more than one button works at a time in pygame, how to fix this?](https://stackoverflow.com/questions/69593109/no-more-than-one-button-works-at-a-time-in-pygame-how-to-fix-this/69593913#69593913) – Rabbid76 Oct 23 '21 at 10:24
  • Thanks a lot man☺️. I was looking for this for a long time – Ashu Agrawal Oct 23 '21 at 18:31

0 Answers0