Introduction to PyGame
Learn game development basics.
Introduction to PyGame is a free Python For Kids lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python For Kids learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to PyGame
Welcome to the PyGame lesson! Today, we'll learn the basics of game development using PyGame, a Python library that helps you create games with graphics, sound, and user interaction.

2
What is PyGame?
PyGame is a Python library for creating 2D games. It provides tools for:
- Displaying graphics
- Handling user input
- Adding sound effects and music
- Creating animations
Whether you want to make a simple game or a complex one, PyGame makes game development easier and more fun!
3
Installing PyGame
To use PyGame, you first need to install it. Use the following command in your terminal or command prompt:
pip install pygame
Once installed, you’re ready to create your first game!
4
Creating a Basic Game Window
Let’s start by creating a basic game window using PyGame. This window will be the foundation of your game.
Example:
import pygame
# Initializes PyGame
pygame.init()
# Sets up the game window
screen = pygame.display.set_mode((800, 600)) # Width: 800, Height: 600
pygame.display.set_caption("My First Game")
# Runs the game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT: # Checks if the user closes the window
running = False
# Quits PyGame
pygame.quit()
5
Adding Colors
You can fill the game window with colors using RGB values. Each color is defined by its red, green, and blue components.
Example:
import pygame
pygame.init()
# Sets up the game window
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Colorful Window")
# Colors in RGB
BLUE = (0, 0, 255)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fills the screen with blue color
screen.fill(BLUE)
pygame.display.update()
pygame.quit()
6
Drawing Shapes
PyGame allows you to draw shapes like rectangles, circles, and lines.
pygame.draw.rect(): Draws rectanglespygame.draw.circle(): Draws circlespygame.draw.line(): Draws lines
Example:
import pygame
pygame.init()
# Sets up the game window
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Drawing Shapes")
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fills the screen with white color
screen.fill(WHITE)
# Draws a red rectangle
pygame.draw.rect(screen, RED, (100, 100, 200, 150))
# Draws a green circle
pygame.draw.circle(screen, GREEN, (400, 300), 75)
pygame.display.update()
pygame.quit()
7
Handling User Input
Games often require user input, such as pressing keys or clicking the mouse. PyGame allows you to handle input using events.
Example:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("User Input")
WHITE = (255, 255, 255)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN: # Checks if a key is pressed
if event.key == pygame.K_SPACE:
print("Spacebar was pressed!")
elif event.type == pygame.MOUSEBUTTONDOWN: # Checks if the mouse is clicked
print("Mouse clicked at", event.pos)
screen.fill(WHITE)
pygame.display.update()
pygame.quit()
8
Moving Objects
To create animations or interactive games, you can move objects by updating their positions in each frame.
Example:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Moving Object")
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
x, y = 50, 50 # Starting position of the rectangle
velocity = 5 # Speed of movement
running = True
while running:
pygame.time.delay(30) # Delays the loop for 30ms
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Gets keys pressed
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= velocity
if keys[pygame.K_RIGHT]:
x += velocity
if keys[pygame.K_UP]:
y -= velocity
if keys[pygame.K_DOWN]:
y += velocity
screen.fill(WHITE)
pygame.draw.rect(screen, BLUE, (x, y, 50, 50)) # Draws the moving rectangle
pygame.display.update()
pygame.quit()
9
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Question")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 255, 0)) # Fills the screen with green
pygame.display.update()
pygame.quit()
10
Great Job!
You've learned the basics of using PyGame for game development, including creating a game window, drawing shapes, handling user input, and moving objects. PyGame is a powerful library that lets you bring your game ideas to life. Keep experimenting with its features to create your own fun and interactive games!

Frequently asked questions
Is the “Introduction to PyGame” lesson free?
Yes — the full text of “Introduction to PyGame” is free to read here on the web, and the Python For Kids course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python For Kids course, upgrade to CoddyKit PRO.
What will I learn in “Introduction to PyGame”?
Learn game development basics. You practise Python For Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python For Kids?
No prior experience is required. Python For Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Introduction to PyGame” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python For Kids lesson?
Yes. Every Python For Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Python Libraries
- Using Math Library
- Introduction to PyGame