Why did this work? | Sololearn: Learn to code for FREE!
0

Why did this work?

Im writing and game in python using pygame. Im working on hero movement and couldnt get my hero to move by simply holding down a directional key I had to continually tap it. I solved the problem by adding a line of code:"pygame.key.set_repeat(True)" i added this code under the line that says keys = pygame.key.get_pressed() and it didnt work. Then i decided to add it above the main loop as you will see and it worked. So problem is fixed but i need to know why. Heres the code: import pygame pygame.init() win = pygame.display.set_mode((500,500)) pygame.display.set_caption("My First Pygame") x = 50 y = 425 width = 40 height = 60 vel = 5 isJump = True jumpCount = 10 pygame.key.set_repeat(True) run = True while run: pygame.time.delay(100) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and x > vel: x -= vel

8th Mar 2020, 5:26 PM
Inkosi
Inkosi - avatar
2 ответов
+ 1
Maybe its like a configuration and you have to set it before using it? I don't know but that would make sense
9th Mar 2020, 4:09 PM
Jakob Meier
Jakob Meier - avatar
0
That actually makes perfect sense
9th Mar 2020, 5:07 PM
Inkosi
Inkosi - avatar