Pygame: Setup and Installation
Pygame is a popular Python library for building 2D games, handling graphics, sound, input, and timing so you don't have to write that from scratch. In this project, you'll build a clone of the classic arcade game Asteroids: a ship that rotates and thrusts through space, dodging (and eventually destroying) asteroids drifting across the screen.
Installing Pygame
Pygame doesn't run inside this course's browser sandbox (it needs a real window to draw into), so for this project you'll write and run code locally with a real Python install.
pip install pygame
Verify it installed correctly:
import pygame
print(pygame.ver)
Opening a window
Every Pygame program starts the same way: initialize the library, create a window (called a surface), and give it a title.
import pygame
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Asteroids")
pygame.init()sets up every Pygame subsystem (display, sound, input) in one call.pygame.display.set_mode((width, height))creates the actual window and returns aSurfaceyou'll draw everything onto.- Nothing shows up yet, and the window will look "frozen"/unresponsive, that's because there's no loop keeping it open and processing events, which is exactly what the next lesson builds.
NOTE
Running this script as-is will open a window and then immediately close it (or hang, depending on your OS), that's expected. A real window needs a game loop to stay open and respond to events, covered next.