Allowing resizing window in PyGame

Pygame is a Python module for game development that provides graphics and sound libraries. By default, Pygame windows are not resizable, but you can make them resizable by adding the pygame.RESIZABLE flag.

Installation of PyGame

Install Pygame using pip in your terminal or command prompt ?

pip install pygame

Creating a Normal (Non-Resizable) Window

Here's how to create a basic Pygame window that cannot be resized ?

import pygame

# Initialize pygame
pygame.init()

# Create a non-resizable window
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Non-Resizable Window')

# Main game loop
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # Fill screen with white color
    screen.fill((255, 255, 255))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Creating a Resizable Window

To make a Pygame window resizable, add the pygame.RESIZABLE flag to the set_mode() function ?

import pygame

# Initialize pygame
pygame.init()

# Create a resizable window
screen = pygame.display.set_mode((400, 300), pygame.RESIZABLE)
pygame.display.set_caption('Resizable Window')

# Main game loop
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.VIDEORESIZE:
            # Handle window resize
            screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)
    
    # Fill screen with light blue color
    screen.fill((173, 216, 230))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Handling Window Resize Events

When a user resizes the window, Pygame generates a VIDEORESIZE event. You can handle this event to update your display ?

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300), pygame.RESIZABLE)
pygame.display.set_caption('Resize Event Handler')

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

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.VIDEORESIZE:
            # Get new size and recreate surface
            width, height = event.size
            screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
            print(f"Window resized to: {width}x{height}")
    
    # Draw content that adapts to window size
    screen.fill((100, 150, 200))
    
    # Draw a rectangle that scales with window size
    rect_width = screen.get_width() // 2
    rect_height = screen.get_height() // 2
    rect_x = (screen.get_width() - rect_width) // 2
    rect_y = (screen.get_height() - rect_height) // 2
    
    pygame.draw.rect(screen, (255, 255, 255), 
                    (rect_x, rect_y, rect_width, rect_height))
    
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Key Differences

Window Type Code User Can Resize?
Normal Window pygame.display.set_mode((400, 300)) No
Resizable Window pygame.display.set_mode((400, 300), pygame.RESIZABLE) Yes

Best Practices

  • Always handle the VIDEORESIZE event when using resizable windows

  • Use pygame.init() before creating any display

  • Call pygame.quit() to properly close the application

  • Consider minimum and maximum window sizes for better user experience

Conclusion

Making Pygame windows resizable is simple - just add the pygame.RESIZABLE flag to set_mode(). Handle the VIDEORESIZE event to properly respond to size changes and ensure your game content adapts to the new window dimensions.

Updated on: 2026-03-27T13:23:16+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements