简体   繁体   中英

PyGame KEYDOWN event not captured

When I execute the following code, the KEYDOWN events are not captured. The desired result is to exit the loop on any key press. How can I make the code have the desired effect?

while slideshow:

    clock.tick(30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
        elif event.type == pygame.KEYDOWN: slideshow = False

    scope.screen.blit(safimg, (0, 0))

    for t in statsurfs:
        scope.screen.blit(t, (fontline * lnum, fontcol))
        lnum += 1

    lnum = 5

    for t in dynsurfs:
        scope.screen.blit(t, (fontline * lnum, fontcol * 2 - t.get_width()))
        lnum += 1

    pygame.display.flip()

    pygame.time.delay(wtime * 1000)

    for i in imgfiles:
        img = pygame.image.load(i).convert()
        #img = pygame.transform.scale(img, scope.size)
        scope.screen.blit(img,(0, 0))
        pygame.display.flip()
        pygame.time.delay(wtime * 1000)

Also, any tips on how to optimize the code would be greatly appreciated as I'm new to Python programming.

The issue is most likely with the way you keep the program from going too fast (aka the clock). You're currently using

pygame.time.delay(1000 * wtime)

which doesn't work as you intend.

The correct way to do this is by using

clock = pygame.time.Clock()

(set clock at the beginning of your code) and then calling

clock.tick(FPS)

at the end of your loop (where FPS is the desired frames per second). This will make the program run through the loop 60 times a second. In your code,

pygame.time.delay(1000 * wtime)

simply makes the program wait, but the program isn't actually running anything; it's just waiting. If you have other questions just reply to this answer. Good luck :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM