0

I'm trying to construct a module that loops through two or more images when a key is pressed and stops looping when the key is lifted. Unfortunately, once I get the images to start looping, they don't stop. Please help, I'm programming this in Python 2.7 and with Pygame. Here is my commented code.

import pygame, sys 
running = True
run = False
pygame.init()
screen = pygame.display.set_mode([640,480]) #Initializes pygame window
screen.fill([255, 255, 255]) #Fills screen with white
picture = pygame.image.load('picture1.png') #Loads image 1
picturetwo = pygame.image.load('picture2.png') #Loads image 2
screen.blit(picture, [50, 50])
import pygame, sys 
running = True
run = False
pygame.init()
screen = pygame.display.set_mode([640,480]) #Initializes pygame window
screen.fill([255, 255, 255]) #Fills screen with white
picture = pygame.image.load('picture1.png') #Loads image 1
picturetwo = pygame.image.load('picture2.png') #Loads image 2
screen.blit(picture, [50, 50])
#Places picture in window. 50 pixels down from the top and 50 pixels right from the top
pygame.display.flip()
while running:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
        #If a key is pressed                #If that key is the right arrow
            run = True
        while run == True:
            pygame.time.delay(500)
            pygame.draw.rect(screen, [255,255,255], [50, 50, 150, 150], 0)
            #Creates a white rectangle to fill over the preceding image
            screen.blit(picturetwo, [50, 50])
            #Loads the second image over the first rectangle
            pygame.display.flip()
            #Repeats
            pygame.time.delay(500)
            pygame.draw.rect(screen, [255,255,255], [50, 50, 150, 150], 0)
            screen.blit(picture, [50, 50])
            pygame.display.flip()
            if event.key != pygame.K_RIGHT:
#If the right key is not pressed, exits loop. DOES NOT WORK
                run = False
        if event.type == pygame.QUIT: #Exits program
            running = False
pygame.quit()
Steven.S
  • 1
  • 4

1 Answers1

0

You are using the event KEYDOWN to check if a key is kept pressed. It does not work that way. The event is emitted the moment you press the key only. See this post for a more detailed explanation.

To check if a key is pressed, use:

pressed = pygame.key.get_pressed()
if pressed[pygame.K_RIGHT]: #or the constant corresponding to the key to check 
    #do things to do when that key is pressed.

You could try to rewrite your while loop this way:

while running:
    #check the incoming events, just to check when to quit
    for event in pygame.event.get():
        if event.type == pygame.QUIT: #Exits program
            running = False

    pressed = pygame.key.get_pressed()
    if pressed[K_RIGHT]:
        pygame.time.delay(500)
        pygame.draw.rect(screen, [255,255,255], [50, 50, 150, 150], 0)
        #Creates a white rectangle to fill over the preceding image
        screen.blit(picturetwo, [50, 50])
        #Loads the second image over the first rectangle
        pygame.display.flip()
        #Repeats
        pygame.time.delay(500)
        pygame.draw.rect(screen, [255,255,255], [50, 50, 150, 150], 0)
        screen.blit(picture, [50, 50])
        pygame.display.flip()
Valentino
  • 7,291
  • 6
  • 18
  • 34