By Paul Derrington, Jr.
CEO | Chief Engineer | Research Engineer | Editor, D-CSIL
4 min read
Key Takeaways
Build a playable game in one Python file: move a paddle, catch a falling coin, and watch the score rise. You’ll use a game loop, elapsed time per frame, and rectangle collisions. Copy or download the tested script to try it yourself.

Step 1 — Install Pygame and prove it imports
Install Python 3 first. Then run python3 -m pip install pygame and check it with python3 -c "import pygame; print(pygame.version.ver)". Using python3 for both makes sure you install the library into the interpreter that will run the game.
The game was tested on Python 3.12 with Pygame 2.6.1. The APIs used here are documented in Pygame’s v2.6.0 reference.
Step 2 — A window, a clock, and a loop that can quit
Every Pygame program starts the same way, following the pattern in the documentation's quick start: pygame.init(), then screen = pygame.display.set_mode((640, 480)) to open the window, then clock = pygame.time.Clock(). Pygame, the docs note, gives you full control of program execution — there is no engine running your game for you. The loop is the game.
Inside the loop, order matters: first poll events with pygame.event.get() and set running = False when an event is pygame.QUIT (that is the window's close button); then update your game; then draw; then pygame.display.flip() to put the finished frame on screen; then clock.tick(60), which the time reference says will delay to keep the game running at no more than 60 frames per second when called once per frame. Skip the event poll and your window cannot be closed cleanly. The clock caps the frame rate and measures elapsed time so movement can account for it.
Step 3 — Move a paddle, and keep it on screen
The paddle is a rectangle: player = pygame.Rect(295, 430, 50, 16) — left, top, width, height. Rects are how Pygame stores position and size, and the Rect reference is blunt about one detail that shapes the code: Rect coordinates are all integers.
Movement follows the documentation's own moving-circle example. Read held keys with pygame.key.get_pressed(), and move by speed multiplied by dt — the seconds since the last frame, computed as dt = clock.tick(60) / 1000 at the end of the loop. At 300 pixels per second, player.x -= round(300 * dt) for the left arrow or A, and the mirror line for right. The round() is there because of the integer rule above. Using dt instead of a fixed step per frame keeps movement roughly consistent across frame rates. Rounding the movement to whole pixels each frame can still change the exact speed.
Then one line keeps the paddle honest: player.clamp_ip(screen.get_rect()). The Rect reference defines clamp_ip as moving the rectangle to be completely inside the argument rectangle, in place — so the paddle slides along the edges instead of leaving the window.
Step 4 — One falling coin is the whole game
The coin is a second Rect, 24 by 24, starting at the top at a random x: coin = pygame.Rect(random.randrange(0, 616), 0, 24, 24) — 616 is the window width minus the coin's width, so it never spawns half off-screen. Each frame it falls: coin.y += round(240 * dt).
Catching is one test. The Rect reference defines colliderect() as returning true if any portion of the two rectangles overlaps (edges merely touching do not count). So: if coin.colliderect(player): score += 1, and the coin respawns at the top with coin.update(random.randrange(0, 616), 0, 24, 24) — update() sets a Rect's position and size in place. If instead coin.top passes 480, the bottom of the window, it respawns the same way with no point. That is the entire rule set: move, fall, catch or miss. Everything else is presentation.
Step 5 — Draw the score and save the complete game
Text takes two calls, because the font reference is explicit that Pygame cannot draw text directly onto a surface: font.render(text, antialias, color) creates a new Surface holding the text, and you blit that Surface onto the screen. Passing None as the font name loads Pygame's built-in default font, so font = pygame.font.Font(None, 36) needs no font file. One limit from the same page: rendered text is a single line — newline characters are not rendered.
Download catch.py below or copy the program as shown. Keep the indentation.
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Catch")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)
player = pygame.Rect(295, 430, 50, 16)
coin = pygame.Rect(random.randrange(0, 616), 0, 24, 24)
score = 0
running = True
dt = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if not running:
break
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
player.x -= round(300 * dt)
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
player.x += round(300 * dt)
player.clamp_ip(screen.get_rect())
coin.y += round(240 * dt)
if coin.colliderect(player):
score += 1
coin.update(random.randrange(0, 616), 0, 24, 24)
elif coin.top > 480:
coin.update(random.randrange(0, 616), 0, 24, 24)
screen.fill((18, 18, 32))
pygame.draw.rect(screen, (240, 200, 60), coin)
pygame.draw.rect(screen, (80, 200, 120), player)
screen.blit(font.render(f"Score: {score}", True, (235, 235, 235)), (12, 10))
pygame.display.flip()
dt = clock.tick(60) / 1000
pygame.quit()
Step 6 — Run the game and change one thing
Run python3 catch.py from the folder where you saved the file. A 640 × 480 window titled Catch opens. Move with the arrow keys or A and D. Catch the falling gold coin to score; a miss respawns it without a point. Close the window to quit.
If it misbehaves, the cause is almost always one of three things: the window opens and closes instantly (the event loop or the while running loop is missing or mis-indented), the paddle vanishes off an edge (the clamp_ip line is missing), or motion is wildly fast or slow (dt is not being recomputed from clock.tick(60) each frame, or movement is a fixed step instead of speed * dt). Once it works, change exactly one number — the coin's 240 fall speed is a good first choice — and play it again. Tuning one variable at a time, and feeling what it does to the game, is most of what game design is.