Pygame

2.6.1 · active · verified Fri Apr 10

Pygame is a set of Python modules designed for writing video games. It leverages the Simple DirectMedia Layer (SDL) library, providing cross-platform access to your computer's multimedia hardware, such as sound, video, mouse, keyboard, and joystick. The current stable version is 2.6.1, with frequent minor and bugfix releases, often bundling updates to the underlying SDL library.

Warnings

Install

Imports

Quickstart

This quickstart code initializes Pygame, creates a window, draws a red square on a white background, and handles the basic event loop for closing the window. It demonstrates essential components like initialization, display setup, event handling, drawing, and display updates.

import pygame

pygame.init()

WIDTH, HEIGHT = 800, 600
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame Quickstart")

WHITE = (255, 255, 255)
RED = (255, 0, 0)

running = True
clock = pygame.time.Clock()
FPS = 60

player_rect = pygame.Rect(WIDTH // 2 - 25, HEIGHT // 2 - 25, 50, 50)

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

    # Drawing
    SCREEN.fill(WHITE) # Fill the background
    pygame.draw.rect(SCREEN, RED, player_rect) # Draw a red square

    pygame.display.flip() # Update the full display Surface to the screen

    clock.tick(FPS) # Control frame rate

pygame.quit()

view raw JSON →