Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a sprite move when key is held down

Tags:

python

pygame

Currently the sprite only moves 1 pixel every time a key is pressed. How could I cause the plumber sprite to move constantly when left or right key is being held down?

while running:
    setup_background()
    spriteimg = plumberright

    screen.blit(spriteimg,(x1, y1))

    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             running = False
         elif event.type == pygame.KEYDOWN:
             if event.key == pygame.K_UP:
                 x1 = x1 + 0
                 y1 = y1 - 1
             elif event.key == pygame.K_DOWN:
                 x1 = x1 + 0
                 y1 = y1 + 1
             elif event.key == pygame.K_LEFT:
                 x1 = x1 -1
                 y1 = y1 + 0
             elif event.key == pygame.K_RIGHT:
                 x1 = x1 + 1
                 y1 = y1 + 0

    pygame.display.flip()
    clock.tick(120)
like image 438
enrique2334 Avatar asked Apr 01 '12 03:04

enrique2334


People also ask

How do I know if a key is down pygame?

Detecting if a key is pressed:event() queue methods pygame. KEYDOWN and pygame. KEYUP events respectively. For example, if we want to detect if a key was pressed, we will track if any event of pygame.

What is Keydown in Pygame?

KEYDOWN and pygame. KEYUP to detect if a key is physically pressed down or released. You can activate keyboard repeat by using pygame. key. set_repeat to generate multiple pygame.


2 Answers

You can use pygame.key.get_pressed to do that.

example:

while running:
    keys = pygame.key.get_pressed()  #checking pressed keys
    if keys[pygame.K_UP]:
        y1 -= 1
    if keys[pygame.K_DOWN]:
        y1 += 1
like image 65
qiao Avatar answered Sep 17 '22 13:09

qiao


The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.

If you want to achieve a continuously movement, you have to use pygame.key.get_pressed(). pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement:

while running:

    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             running = False

    keys = pygame.key.get_pressed()

    x1 += keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]
    y1 += keys[pygame.K_DOWN] - keys[pygame.K_UP]

    x1 = x1 % window.get_width()
    y1 = y1 % window.get_height()

    setup_background()
    spriteimg = plumberright
    screen.blit(spriteimg, (x1, y1))

    pygame.display.flip()
    clock.tick(120)

See also Key and Keyboard event

Minimal example: repl.it/@Rabbid76/PyGame-ContinuousMovement

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

rect = pygame.Rect(0, 0, 20, 20)
rect.center = window.get_rect().center
vel = 5

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            print(pygame.key.name(event.key))

    keys = pygame.key.get_pressed()
    
    rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel
    rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel
        
    rect.centerx = rect.centerx % window.get_width()
    rect.centery = rect.centery % window.get_height()

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), rect)
    pygame.display.flip()

pygame.quit()
exit()
like image 39
Rabbid76 Avatar answered Sep 18 '22 13:09

Rabbid76