[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] keep a key pressed and then move a sprite



Hi everyone,

i m learning to use Pygame and i wonder how make a sprite moving by keeping a key pressed. I checked all the method for the Event class but i don't find how to do it. I thought to use some kind of loop with boolean but there is maybe a "better" way to do with Event methods.

Here is a part of my code :

while True:
        clock.tick(60) #set on 60 frames per second
       
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
           
            elif event.type == KEYDOWN:
                if event.key == K_LEFT:
                    heroes.move_left() #want to repeat it as long as the left key is pressed
   
        pygame.display.update(heroes_group.draw(screen))