Модуль, который проходит через два изображения и переключается с помощью клавиш клавиатуры - PullRequest
0 голосов
/ 25 мая 2019

Я пытаюсь создать модуль, который проходит через два или более изображений при нажатии клавиши и останавливает цикл при нажатии клавиши.К сожалению, как только я получаю изображения, чтобы начать цикл, они не останавливаются.Пожалуйста, помогите, я программирую это на Python 2.7 и Pygame.Вот мой закомментированный код.

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()

1 Ответ

0 голосов
/ 26 мая 2019

Вы используете событие KEYDOWN для проверки, нажата ли клавиша. Это не работает таким образом. Событие генерируется в тот момент, когда вы нажимаете только клавишу. См. этот пост для более подробного объяснения.

Чтобы проверить, нажата ли клавиша, используйте:

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.

Вы можете попробовать переписать ваш цикл while следующим образом:

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()
...