
In this Move Image in Pygame tutorial, you will learn how to control a game character using the keyboard.
In the previous lesson, we displayed a player image on the screen. It looked like a real game character, but it couldn’t move. Today, we will make that player move using the arrow keys. By the end of this lesson, you will have your first controllable game character.
What Will You Learn?
In this lesson, you will learn:
- How to move an image in Pygame
- How to use arrow keys
- How to change the player’s position
- How keyboard movement works in games
Project Folder
Your project should look like this:
Lesson6
│
├── lesson6_move_player.py
│
└── images
│
└── player.png
Program 1: Display the Player Image
If you completed Lesson 5, you already know this code.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Move Player")
player = pygame.image.load("images/player.png")
x = 350
y = 250
running = True
while running:
screen.fill((255,255,255))
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.
The player image appears in the center of the screen.
Why Doesn’t the Player Move?
Look carefully.
The values of x and y never change.
x = 350
y = 250
Since these values stay the same, the image always appears in the same place.
To move the player, we need to change these values.
Program 2: Detect Keyboard Input
Inside the game loop, add:
keys = pygame.key.get_pressed()
This checks which key is currently being pressed.
Think of it as asking the keyboard:
“Is any key being pressed right now?”
Program 3: Move Right
Add this code:
if keys[pygame.K_RIGHT]:
x += 5
Now run the program.
Press the Right Arrow key.
Your player starts moving to the right.
Congratulations! 🎉
You just created your first moving game character.
Move Left
if keys[pygame.K_LEFT]: x -= 5
Press the Left Arrow key.
The player moves left.
Move Up
if keys[pygame.K_UP]:
y -= 5
Notice something interesting.
The Y value becomes smaller, but the player moves up.
That’s because the top-left corner of the screen is (0,0).
Move Down
if keys[pygame.K_DOWN]:
y += 5
Now the player moves downward.
Complete Program
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Move Image in Pygame")
player = pygame.image.load("images/player.png")
x = 350
y = 250
running = True
while running:
screen.fill((255,255,255))
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
x += 5
if keys[pygame.K_LEFT]:
x -= 5
if keys[pygame.K_UP]:
y -= 5
if keys[pygame.K_DOWN]:
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 and try all four arrow keys.
Your game now feels much more interactive.
Experiment Time
Try changing the movement speed.
Current speed:
x += 5
Faster movement:
x += 10
Slower movement:
x += 2
Observe how the game feels with different speeds.
Real-World Example
Think about a racing game.
- Press Right Arrow → Car moves right.
- Press Left Arrow → Car moves left.
- Press Up Arrow → Car moves forward.
- Press Down Arrow → Car slows down or moves backward.
The same concept is used in many games.
Mini Project
Create a simple game where:
- The player starts in the center.
- The player can move in all four directions.
- The player cannot move automatically.
- The player only moves when you press an arrow key.
Challenge
Challenge 1
Increase the movement speed.
Challenge 2
Change the window title to:
pygame.display.set_caption("My First Game Character")
Challenge 3
Replace the player image with another PNG image and test it.
Common Mistakes
The image is not moving
Make sure this line is inside the game loop:
keys = pygame.key.get_pressed()
Arrow keys are not working
Check that you have written:
pygame.K_RIGHT
pygame.K_LEFT
pygame.K_UP
pygame.K_DOWN
Exactly as shown.
The player image disappears
The player may have moved outside the game window.
In future lessons, we will stop the player from leaving the screen.
What You Learned Today
In this lesson, you learned:
- How to move an image in Pygame
- How to use keyboard input
- How to change X and Y positions
- How to control a player with arrow keys
- How player movement works in games
Frequently Asked Questions
How do I move an image in Pygame?
Load the image using pygame.image.load(), then change its X and Y coordinates based on keyboard input before drawing it with screen.blit().
How do I control a player in Pygame?
Use pygame.key.get_pressed() to detect arrow keys or other keyboard keys and update the player’s position.
Why does my player move off the screen?
The program currently doesn’t check screen boundaries. In the next lesson, you’ll learn how to keep the player inside the game window.
What is the difference between moving a rectangle and moving an image?
Both use the same X and Y coordinates. The only difference is that an image is drawn using screen.blit(), while a rectangle is drawn using pygame.draw.rect().
Why do games use X and Y coordinates?
Every object in a 2D game needs a position. X controls left and right movement, while Y controls up and down movement.
Can I use my own character image?
Yes. You can replace player.png with any PNG image and use it as your game character.
What’s Next?
In Lesson 7, you will learn How to Keep the Player Inside the Screen in Pygame. You’ll add movement boundaries so your character cannot disappear outside the game window—just like in real games.
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.