python-3.xpygamekeyboard-input

Pygame get_pressed()


I've been checking a lot of tutorials, I've been testing it myself, I read the official papers a hundred of times. How the heck does pygame.key.get_pressed() work? It looks like it isn't used at all in gaming. Can you suggest me some raw code, no ketchup where I can get a good example?


Solution

  • get_pressed is just another way of getting keypresses

    You could do this in an event pumper:

    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        # Do stuff
    

    Although just using the KEYDOWN event is better as it adds a delay between presses.:

    # At the beginning of the loop...
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                # Do stuff