Your first pygame program opens a window, draws something in it and stays open until you close it. It is about fifteen lines, and almost every pygame game starts with the same ones.
Press Play to see a sky, a sun and some grass. Change the numbers and press Play again.
Line by line
import pygame loads the library. pygame.init() starts all of its modules (display, fonts, sound) in one call.
screen = pygame.display.set_mode((480, 360)) opens a window 480 pixels wide and 360 tall and returns a Surface, an image you can draw on. Everything your game shows is drawn on screen. Note the double parentheses: the size is one tuple.
pygame.display.set_caption(...) sets the window title.
clock = pygame.time.Clock() makes a clock that keeps the game at a steady speed.
while running: is the game loop. Each pass draws one frame. Without it the program would reach its end and the window would close at once.
for event in pygame.event.get(): reads everything that happened since the last frame: key presses, mouse clicks, and the window's close button, which arrives as a pygame.QUIT event. Setting running = False ends the loop.
screen.fill(...) paints the whole screen one color, wiping out the last frame. The colors are (red, green, blue) numbers from 0 to 255; see pygame colors.
pygame.draw.circle and pygame.draw.rect draw the sun and the grass. The pygame.draw page has every shape.
pygame.display.flip() shows the finished frame. Pygame draws into a hidden buffer, and nothing appears in the window until you flip it.
clock.tick(60) waits so the loop runs at most 60 times a second.
pygame.quit() shuts pygame down after the loop ends.
Coordinates
Pygame measures in pixels from the top-left corner of the window. x grows to the right and y grows down, unlike the graphs from math class. In a 480 by 360 window:
| Position | Coordinates |
|---|---|
| Top-left corner | (0, 0) |
| Top-right corner | (479, 0) |
| Center | (240, 180) |
| Bottom-left corner | (0, 359) |
That is why the grass starts at y = 280: it is near the bottom.
Make something move
A frame is redrawn sixty times a second, so changing a number a little each frame makes something move. This version moves the sun across the sky:
Try removing the screen.fill(...) line: the sun leaves a trail, because the old frames are never painted over.
Next steps
The loop you just wrote is the heart of every pygame game. The game loop explains its three parts, and keyboard input lets the player take control.
Frequently Asked Questions
How do I open a window in pygame?
Call pygame.init(), then screen = pygame.display.set_mode((width, height)). The window stays open only while your program runs, so follow it with a loop that reads events and calls pygame.display.flip().
Why does my pygame window close immediately?
The program reached its last line and ended. A pygame program keeps its window open with a while loop that runs until the player closes the window.
How do I set the window title?
pygame.display.set_caption("My Game"), usually right after set_mode().
Do I need pygame.init()?
It is good practice. It starts every pygame module at once, including fonts and sound. Without it you must start the modules you use yourself, for example pygame.font.init().