Making a square move continously in pygame

Question:

So I am quite new to pygame and I am trying to make this black square move up, down, left, and right. I was hoping I could do it like when I press any of those keys and hold them in the black box would move continuously up. Can anyone help me?
This is my code

import pygame, sys
import math

pygame.init()


width = 1900
height = 1000
white = (255,255,255)
black = (0,0,0)
player_color = (0,0,0)
player_pos = [950,500]
player_size = (20)

screen = pygame.display.set_mode((width,height))



game_over = False

while not game_over:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            game_over = True

        if event.type == pygame.KEYDOWN:

            x = player_pos[0]
            y = player_pos[1]   

            if event.key == pygame.K_LEFT:
                x -= player_size
            elif event.key == pygame. K_RIGHT:
                x += player_size
            elif event.key == pygame. K_DOWN:
                y += player_size
            elif event.key == pygame. K_UP:
                y -= player_size
            player_pos = [x,y]

    screen.fill(white)
    
    pygame.draw.rect(screen,black, (player_pos[0], player_pos[1],20,20))
    

    
    pygame.display.update()
Asked By: Inexpetatus

||

Answers:

You have to use pygame.key.get_pressed() instead of the keyboard events. See How can I make a sprite move when key is held down.

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.

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.

Use pygame.time.Clock to control the frames per second and thus the game speed and the movement speed of the player.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

import pygame, sys
pygame.init()

width = 1900
height = 1000
white = (255,255,255)
black = (0,0,0)
player_color = (0,0,0)
player_pos = [950,500]
player_size = (20)

screen = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()

game_over = False
while not game_over:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True

    keys = pygame.key.get_pressed()
    player_pos[0] += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * player_size
    player_pos[1] += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * player_size

    screen.fill(white)
    pygame.draw.rect(screen, player_color, (player_pos[0], player_pos[1],20,20))
    pygame.display.update()

pygame.quit()
sys.exit()
Answered By: Rabbid76
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.