francesc
francesc

Reputation: 343

In pygame key read is delayed

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        elif (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_LEFT):
                print('LEFT')
            if (event.key == pygame.K_DOWN):
                print('DOWN')
            if (event.key == pygame.K_RIGHT):
                print('RIGHT')
            if (event.key == pygame.K_UP):
                print('UP')

    print('FRAME')
    pygame.display.update()
    clock.tick(1)

If you press arrow key just after 'FRAME' is printed, sometimes 'FRAME' is printed for second time before keypressed arrow is printed. Is as if pygame.event.get() is delayed. Example:

FRAME
FRAME
          <---- UP pressed here
FRAME
UP
FRAME
FRAME
          <---- UP pressed here
FRAME
UP

How could I correct this behaviour?

Upvotes: 2

Views: 98

Answers (1)

programmerpremium
programmerpremium

Reputation: 224

You are using clock.tick(1), so this is telling pygame that you want 1 fps. This will make everything slower, and pretty much pause your code for a tiny bit. Try changing the 1 to something like 60 or 30.

Upvotes: 1

Related Questions