Overview

Pygame 2.1, released on December 23, 2021, improves event handling and introduces the Window class for better window control.

Main Features

Improved events

The event system is enriched with new types and better handling of custom events and game controllers.

python
import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Pygame 2.1')

# Improved event loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            print(f'Key: {pygame.key.name(event.key)}')
        elif event.type == pygame.MOUSEWHEEL:
            print(f'Wheel: x={event.x}, y={event.y}')

    screen.fill((30, 30, 30))
    pygame.display.flip()

pygame.quit()

Window class

The new Window class offers finer control over the window: position, size, fullscreen mode, and multi-monitor management.

python
import pygame

pygame.init()

# Window control
screen = pygame.display.set_mode((800, 600), pygame.RESIZABLE)
pygame.display.set_caption('Resizable window')

# Position and information
info = pygame.display.Info()
print(f'Resolution: {info.current_w}x{info.current_h}')

# Toggle fullscreen
# pygame.display.toggle_fullscreen()

pygame.quit()

Sources