0

I have a checkboard game set up with two gun sprites. I want to be able to move the gun sprites around freely but for some reason, pygame keeps missing my inputs. The code I have set up for the gun sprites is

while running:  
     for event in pygame.event.get():         
          if event.type == pygame.QUIT:             
               running = False      

     screen.blit(background, (0, 0))     
     
     if gunrotationcheck == 0: #Checks if gunrotationcheck is set to 0. If true, replace gun with up sprite
            gun = pygame.image.load('gun_up.png')
            screen.blit(gun, (gunx,guny))
     elif gunrotationcheck == 1: #Checks if gunrotationcheck is set to 1. If true, replace gun with left sprite
        gun = pygame.image.load('gun_left.png') 
        screen.blit(gun, (gunx-25,guny+30)) #Position fix
     elif gunrotationcheck == 3: #Checks if gunrotationcheck is set to 3. If true, replace gun with down sprite
        gun = pygame.image.load('gun_down.png') 
        screen.blit(gun, (gunx,guny))
     elif gunrotationcheck == 2: #Checks if gunrotationcheck is set to 2. If true, replace gun with right sprite
        gun = pygame.image.load('gun_right.png') 
        screen.blit(gun, (gunx-25,guny+30)) #Position fix

     events = pygame.event.get()
     for event in events:
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a: #Move left
                gunx -= 50
            if event.key == pygame.K_d: #Move right
                gunx += 50
            if event.key == pygame.K_w: #Move up
                guny -= 50
            if event.key == pygame.K_s: #Move down
                guny += 50
            if event.key == pygame.K_j: #Turn left
                gunrotationcheck = 1
                gun = pygame.image.load('gun_left.png')
                screen.blit(gun, (gunx,guny))
            if event.key == pygame.K_l: #Turn right
                gunrotationcheck = 2
                gun = pygame.image.load('gun_right.png')
                screen.blit(gun, (gunx,guny))
            if event.key == pygame.K_i: #Turn up
                gunrotationcheck = 0
                gun = pygame.image.load('gun_up.png')
                screen.blit(gun, (gunx,guny))
            if event.key == pygame.K_k: #Turn down
                gunrotationcheck = 3
                gun = pygame.image.load('gun_down.png')
                screen.blit(gun, (gunx,guny))```
0

1 Answer 1

0

pygame.event.get() get all the messages and remove them from the queue. See the documentation:

This will get all the messages and remove them from the queue. [...]

If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.

Implement only one event loop:

while running:  
     for event in pygame.event.get():         
          if event.type == pygame.QUIT:             
               running = False  
          if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a: #Move left
                gunx -= 50
            if event.key == pygame.K_d: #Move right
                gunx += 50
            if event.key == pygame.K_w: #Move up
                guny -= 50
            if event.key == pygame.K_s: #Move down
                guny += 50
            # [...]

     screen.blit(background, (0, 0))     
     
     # [...]

See also Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.