
Welcome Back, Game Developer!
In the previous lesson, you created your first Pygame window. But there was one problem. The window was completely empty!
Today, we will learn how to draw shapes and add colors to our game. By the end of this lesson, you will be able to:
✅ Draw rectangles
✅ Draw circles
✅ Draw lines
✅ Use different colors
✅ Create simple drawings
Let’s start!
Create a New File
Create a file named:
lesson2_shapes.py
Now type the following code.
Program 1: Create a White Background
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Drawing Shapes")
running = True
while running:
screen.fill((255, 255, 255))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Run the program.
You will see a white window.
Understanding Colors
In Pygame, colors are created using RGB values.
RGB means:
R = Red
G = Green
B = Blue
Examples:
| Color | RGB Value |
|---|---|
| Red | (255, 0, 0) |
| Green | (0, 255, 0) |
| Blue | (0, 0, 255) |
| White | (255, 255, 255) |
| Black | (0, 0, 0) |
| Yellow | (255, 255, 0) |
What Does screen.fill() Do?
screen.fill((255, 255, 255))
This fills the entire window with a color.
Example:
White
screen.fill((255,255,255))
Blue
screen.fill((0,0,255))
Green
screen.fill((0,255,0))
Try changing the color and run the program again.
Program 2: Draw a Rectangle
Add this line after:
screen.fill((255,255,255))
pygame.draw.rect(screen, (255,0,0), (100,100,200,100))
Complete code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Rectangle")
running = True
while running:
screen.fill((255,255,255))
pygame.draw.rect(screen, (255,0,0), (100,100,200,100))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Run the program.
A red rectangle will appear.
Understanding Rectangle Values
pygame.draw.rect(screen, (255,0,0), (100,100,200,100))
Here:
100 = X Position
100 = Y Position
200 = Width
100 = Height
Think of it like this:
(X,Y)
|
V
+------------------+
| |
| Rectangle |
| |
+------------------+
Activity 1
Try changing:
(100,100,200,100)
to
(300,200,150,150)
What happens?
Observe carefully.
Program 3: Draw a Circle
Replace the rectangle code with:
pygame.draw.circle(screen, (0,255,0), (400,300), 60)
Run the program.
You will see a green circle.
Understanding Circle Values
pygame.draw.circle(screen, (0,255,0), (400,300), 60)
Here:
400 = X Position
300 = Y Position
60 = Radius
The radius controls the size of the circle.
Activity 2
Try:
pygame.draw.circle(screen, (0,0,255), (400,300), 100)
What changed?
Program 4: Draw a Line
Add this code:
pygame.draw.line(screen, (0,0,0), (50,50), (700,500), 5)
Run the program.
A black line will appear.
Understanding Line Values
pygame.draw.line(screen, (0,0,0), (50,50), (700,500), 5)
Here:
(50,50) = Start Point
(700,500) = End Point
5 = Thickness
Program 5: Draw Multiple Shapes
Now let’s combine everything.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("My Drawing")
running = True
while running:
screen.fill((255,255,255))
pygame.draw.rect(screen, (255,0,0), (100,100,200,100))
pygame.draw.circle(screen, (0,255,0), (500,200), 60)
pygame.draw.line(screen, (0,0,255), (50,500), (750,500), 5)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Congratulations!
You just created your first drawing using Pygame.
Mini Project: Draw a Traffic Signal
Try creating:
- A black rectangle
- A red circle
- A yellow circle
- A green circle
Hint:
Use:
pygame.draw.rect()
and
pygame.draw.circle()
Challenge Time
Can you create:
Challenge 1
A smiling face using circles.
Challenge 2
A simple house using:
- Rectangles
- Lines
Challenge 3
The Indian Flag using rectangles.
Try it yourself before looking for solutions.
Common Errors and Solutions
Error
NameError
Solution:
Check spelling carefully.
Example:
pygame.draw.circle
must be written correctly.
Error
SyntaxError
Solution:
Check brackets and commas.
Example:
(255,0,0)
must contain commas.
What You Learned Today
Today you learned:
✅ How RGB colors work
✅ How to change background colors
✅ How to draw rectangles
✅ How to draw circles
✅ How to draw lines
✅ How to combine multiple shapes
Quick Revision
Which function is used to:
- Fill the screen with a color?
- Draw a rectangle?
- Draw a circle?
- Draw a line?
If you can answer these questions, you are ready for the next lesson.
What’s Next?
In Lesson 3, we will learn:
✅ Coordinates in Pygame
✅ X and Y positions
✅ Moving shapes around the screen
✅ Understanding game objects
This is where your games will start coming alive! 🎮✨
Frequently Asked Questions (FAQ)
Why are colors written as numbers?
Pygame uses RGB values to create colors. Each value can range from 0 to 255.
Can I use any color?
Yes. By mixing RGB values, you can create millions of colors.
Why is my shape not visible?
Check:
- Position values
- Color values
- Spelling mistakes
What is the easiest shape to draw?
A rectangle is usually the easiest shape for beginners.
Codermantra Practice Task
Create a drawing that contains:
- One rectangle
- Two circles
- One line
- Three different colors
Save it as:
my_first_drawing.py
Run the program and show it to your friends or family. 🎉
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.