How to make a character move into a game with Pygame

Pygame is a set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Pygame adds functionality on top of the excellent SDL library, which allows you to create fully featured games and multimedia programs in the Python language. Pygame is highly portable and runs on nearly every platform and operating system.

To make a player move in a platform game, you would typically use a combination of keyboard input and programming logic to update the player’s position on the screen. Here’s a simple example in Python using the Pygame library:

This is the image used for the player. Save this into the folder with the name “player.png”
import pygame

pygame.init()

# Set up the game window
window_size = (800, 600)
window = pygame.display.set_mode(window_size)
pygame.display.set_caption("My Platform Game")
clock = pygame.time.Clock()
# Set up the player
player_image = pygame.image.load("player.png")
player_position = [100, 400]
player_speed = 1

# Game loop


direction = ""

running = True
while running:

    match direction:
        case "left":
            player_position[0] -= player_speed
        case "right":
            player_position[0] += player_speed
        case "up":
            player_position[1] -= player_speed
        case "down":
            player_position[1] += player_speed

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT: direction = "left"
            if event.key == pygame.K_RIGHT: direction = "right"
            if event.key == pygame.K_UP: direction = "up"
            if event.key == pygame.K_DOWN: direction = "down"
        if event.type == pygame.KEYUP:
            direction = ""
    
    window.fill((255, 255, 255))
    window.blit(player_image, player_position)
    pygame.display.flip()
    clock.tick(120)
pygame.quit()

In this example, the game loop continuously checks for events (such as keyboard input) and updates the player’s position accordingly. The player’s position is represented by a list of x and y coordinates (player_position), which are updated based on the keyboard input and the player’s speed (player_speed). Finally, the player is drawn on the screen using the blit() function.

Moving continuosly with pygame.key.getpressed()

Now, let’ move it continuosly with pygame.key.getpressed, instead of using pygame.event.get()

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Gioco")
clock = pygame.time.Clock()

x = 50
y = 50
player = pygame.image.load("player.png")
width = player.get_width()
height = player.get_height()
vel = 1
RED = (255, 0, 0)

run = True
while run:
    
    # RECT = x, y, width, height

    for event in pygame.event.get():
        if event.type == pygame.QUIT: # quit the window
            run = False

    keys = pygame.key.get_pressed() #check the key pressed

    if keys[pygame.K_LEFT] and x > vel:
        print(pygame.K_LEFT)
        x -= vel

    if keys[pygame.K_RIGHT] and x < 500 - width - vel:
        x += vel

    if keys[pygame.K_UP] and y > vel:
        y -= vel

    if keys[pygame.K_DOWN] and y < 500 - height - vel:
        y += vel

    win.fill((0, 0, 0))
    # pygame.draw.rect(win, RED, RECT)
    win.blit(player, (x,y))
    pygame.display.update()
    clock.tick(120)

pygame.quit()


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.