
In this Pygame Background Image Tutorial, you will learn how to add a beautiful background image to your game.
Until now, your game has had a plain white background. While this is useful for learning, real games use background images to make the game world look more exciting.
By the end of this lesson, your game will have a colorful background and a moving player character.
What Is a Background Image?
A background image is the picture displayed behind all the game objects.
Examples:
- Blue Sky
- Green Grass
- Forest
- Desert
- Space
- Snow
- Beach
The background helps players understand where the game takes place.
Project Folder
Create a new folder structure like this:
Lesson8
│
├── lesson8_background.py
│
└── images
│
├── player.png
└── background.png
Keep all images inside the images folder.
Program 1: Load the Background Image
Add this line after loading the player image.
background = pygame.image.load("images/background.png")
This loads the background image into memory.
Program 2: Display the Background
Inside the game loop, replace:
screen.fill((255,255,255))
with:
screen.blit(background,(0,0))
Why (0,0)?
Because the background should start from the top-left corner of the screen.
Complete Program
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Background Image")
background = pygame.image.load("images/background.png")
player = pygame.image.load("images/player.png")
x = 350
y = 250
running = True
while running:
screen.blit(background,(0,0))
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and x < 736:
x += 5
if keys[pygame.K_LEFT] and x > 0:
x -= 5
if keys[pygame.K_UP] and y > 0:
y -= 5
if keys[pygame.K_DOWN] and y < 536:
y += 5
screen.blit(player,(x,y))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Run the program.
You should now see:
- A background image
- A player image
- Player movement with arrow keys
- The player staying inside the screen
Your project is starting to look like a real game.
Why Do We Draw the Background First?
Look at these two lines.
screen.blit(background,(0,0))
screen.blit(player,(x,y))
The order is important.
The background is drawn first.
Then the player is drawn on top of the background.
If you reverse the order, the background will cover the player.
Experiment Time
Try different background images.
For example:
- Forest
- Space
- Snow
- Desert
- Beach
Notice how the same game feels completely different with a new background.
Mini Project
Create a game scene with:
- One background image
- One player image
- Player movement
- Screen boundaries
Congratulations! You now have the basic structure of a real 2D game.
Practice Challenge
Challenge 1
Replace the background with a space image.
Challenge 2
Replace the background with a football ground.
Challenge 3
Create three different backgrounds and switch between them by changing the image file.
Common Mistakes
The background image is not showing
Check:
- File name
- File path
- Image is inside the images folder
Player disappears
Make sure the background is drawn before the player.
Correct order:
screen.blit(background,(0,0))
screen.blit(player,(x,y))
Background is too small
Use an image that matches your game window size.
For an 800 × 600 window, use an 800 × 600 background image whenever possible.
Best Practice
Use one background image that is the same size as your game window.
For example:
| Window Size | Recommended Background |
|---|---|
| 800 × 600 | 800 × 600 pixels |
| 1024 × 768 | 1024 × 768 pixels |
| 1280 × 720 | 1280 × 720 pixels |
This prevents stretching and keeps the game looking clean.
What You Learned Today
Today you learned:
- What a background image is
- How to load a background image
- How to display a background image
- Why drawing order is important
- How to combine the background and player
Quick Revision
- Which function loads a background image?
- Which function displays the background?
- Why should the background be drawn before the player?
- Where should a background image start on the screen?
What’s Next?
In Lesson 9, you will learn How to Add Enemy Images in Pygame.
You will create your first enemy, place it on the screen, and prepare your game for collision detection.
Frequently Asked Questions (SEO & AEO Optimized)
How do I add a background image in Pygame?
Load the image with pygame.image.load() and display it using screen.blit(background, (0,0)) inside the game loop.
Why is my background image not visible?
The most common reasons are an incorrect file path, a missing image, or drawing the background after other game objects.
Which image format is best for a Pygame background?
PNG is recommended because it provides good image quality and is commonly used in 2D games.
Should the background image match the game window size?
Yes. Using a background image with the same dimensions as the game window helps avoid stretching and distortion.
Can I change the background during gameplay?
Yes. You can load different background images and display them based on the game level or player actions.
What is the difference between screen.fill() and a background image?
screen.fill() paints the window with a single color, while a background image displays a complete scene such as a forest, city, or space environment.
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.