
Welcome to the Pygame Images Tutorial.
Until now, we have been using rectangles and circles to represent game objects. While this is useful for learning, real games use images, also called sprites, for players, enemies, coins, and many other objects.
In this lesson, you will learn how to load an image, display it on the screen, and prepare it for use as a game character. By the end of this lesson, you will replace a simple rectangle with a real image.
What is a Sprite?
A sprite is simply an image used inside a game. For example:
- 👦 Player
- 👾 Enemy
- 🪙 Coin
- ❤️ Heart
- 🚀 Rocket
- 🌳 Tree
Every modern 2D game is built using sprites.
Which Image Format Should You Use?
Pygame supports several image formats, including:
- PNG (Recommended)
- JPG
- BMP
For games, PNG is the best choice because it supports transparent backgrounds.
Example:
player.png
coin.png
enemy.png
background.png
Create Your Project Folder
Inside your project folder, create a new folder named:
images
Your project should look like this:
Lesson5
│
├── lesson5_images.py
│
└── images
│
├── player.png
Keeping images in a separate folder makes your project organized.
Program 1: Load an Image
Create a file named:
lesson5_images.py
Now write the following code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Pygame Images")
player = pygame.image.load("images/player.png")
running = True
while running:
screen.fill((255,255,255))
screen.blit(player,(300,200))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Run the program.
If everything is correct, your player image will appear on the screen.
Understanding pygame.image.load()
This line loads an image from your computer.
player = pygame.image.load("images/player.png")
Here:
- images is the folder name.
- player.png is the image file.
You can change the filename if your image has a different name.
Understanding screen.blit()
The most important line in this lesson is:
screen.blit(player,(300,200))
The blit() function draws an image on the game screen.
Here:
300 = X Position
200 = Y Position
Just like shapes, images also use X and Y coordinates.
Move the Image
Try changing:
screen.blit(player,(300,200))
to:
screen.blit(player,(100,100))
Run the program again. The image moves to a new position. Try a few different values to understand how image positioning works.
Program 2: Display Two Images
If you have another image named coin.png, place it inside the images folder.
coin = pygame.image.load("images/coin.png")
screen.blit(player,(100,250))
screen.blit(coin,(500,250))
Now your game contains two different objects.
Common Errors
FileNotFoundError
Message:
No file 'player.png' found
Solution
- Check the filename.
- Make sure the image is inside the images folder.
- Verify the spelling, including uppercase and lowercase letters.
Unsupported Image Format
If the image does not load, convert it to PNG format and try again.
Image Not Visible
Check:
- Image path
- X and Y coordinates
- Image file name
Mini Project
Create a simple game screen that contains:
- One player image
- One coin image
- White background
Place them at different positions.
Example:
Player Coin
There is no right or wrong design. Experiment with different positions.
Practice Challenge
Challenge 1
Move the player image to all four corners of the screen.
Challenge 2
Display three different images on the screen.
Challenge 3
Replace the player image with your favorite game character.
What You Learned Today
In this lesson, you learned:
- What a sprite is
- Why PNG images are preferred
- How to load an image in Pygame
- How to display an image using blit()
- How to organize image files in a project
- How to display multiple images on the screen
Quick Revision
- Which function is used to load an image?
- Which function displays an image on the screen?
- Which image format is best for Pygame?
- Why should images be stored in a separate folder?
What’s Next?
In Lesson 6, you will learn how to move images using the keyboard.
Instead of moving a rectangle, you will control your own game character using the arrow keys. Your game will start feeling much more like a real game.
Frequently Asked Questions (SEO & AEO Optimized)
How do I load an image in Pygame?
Use the pygame.image.load() function and provide the correct path to your image file.
How do I display an image in Pygame?
Use the screen.blit() function to draw the image at a specific X and Y position.
Which image format is best for Pygame?
PNG is the best choice because it supports transparent backgrounds and is widely used in 2D games.
Why is my image not showing in Pygame?
Common reasons include an incorrect file path, a misspelled filename, or placing the image outside the project folder.
What is a sprite in Pygame?
A sprite is a game image that represents a player, enemy, coin, obstacle, or any visual object in a game.
Can I use JPG images in Pygame?
Yes, but PNG is recommended because it supports transparency, which is useful for game characters.
How do professional games display characters?
Most 2D games use sprites (images) that are loaded into memory and displayed on the screen using functions like blit().
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.