Greeting to all from Greece. Trying to learn pygame i started with a simple rect moving with keys that i've read in : https://nerdparadise.com/programming/pygame/part1 The problem i get with the following code is that the rect moves as if i pressed a key more than one times. import pygame pygame.init() screen = pygame.display.set_mode((400, 300)) done = False is_blue = True x = 30 y = 30 n = 1 pygame.key.set_repeat() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: is_blue = not is_blue pressed = pygame.key.get_pressed() if pressed[pygame.K_UP]: y -= 3 if pressed[pygame.K_DOWN]: y += 3 if pressed[pygame.K_LEFT]: x -= 3 if pressed[pygame.K_RIGHT]: x += 3 screen.fill((0, 0, 0)) if is_blue: color = (0, 128, 255) else: color = (255, 100, 0) pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60)) pygame.display.flip()
after a lot of testing i think pygame.key.get_pressed() works
like that. it read kbd's state but that state has inertia! i mean
it will keep its values for some time so if you call
key.get.pressed() too soon you'll get again the same state . i
tried to simulate same behavior in SDL2 but no lack. but if i
looked correctly pygames's src it relies on SDL1. i dont know
if that's related. So i'd like some feedback. I could correct the program with controling the frame rate with pygame.clock. But i'd like to understand why in a basic tutorial a function
like key.get_pressed with (
Thanks.
chomwitt |