
In this Move Enemy in Pygame tutorial, you will learn how to make an enemy move automatically across the screen.
In the previous lesson, we added an enemy image. The enemy looked good, but it always stayed in the same place. Real games are more exciting because enemies move. By the end of this lesson, your enemy will automatically move from left to right and return when it reaches the edge of the screen.
What Will You Learn?
In this lesson, you will learn:
- How to move an enemy in Pygame
- How automatic movement works
- What speed variables are
- How to make an enemy return after reaching the screen edge
Step 1: Create an Enemy Speed
Below the enemy position, add:
enemy_speed = 3
The value 3 means the enemy will move 3 pixels every frame.
You can increase or decrease this number later.
Step 2: Move the Enemy
Inside the game loop, before drawing the enemy, add:
enemy_x += enemy_speed
Run the program.
The enemy starts moving to the right.
What’s the Problem?
Keep watching the game.
After a few seconds…
The enemy disappears!
Why?
Because its X position keeps increasing forever.
We need to bring it back.
Step 3: Return the Enemy
Add this code below:
enemy_x += enemy_speed
if enemy_x > 800:
enemy_x = -64
Now run the game again.
The enemy will:
- Move across the screen
- Leave the screen
- Come back from the left side
This creates an endless movement.
Understanding the Logic
Suppose:
Window Width = 800
Enemy Width = 64
When the enemy moves beyond:
800
we place it here:
-64
That means the entire enemy starts outside the screen and slowly enters again.
Complete Program
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Moving Enemy")
background = pygame.image.load("images/background.png")
player = pygame.image.load("images/player.png")
enemy = pygame.image.load("images/enemy.png")
x = 350
y = 250
enemy_x = 0
enemy_y = 100
enemy_speed = 3
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
enemy_x += enemy_speed
if enemy_x > 800:
enemy_x = -64
screen.blit(player,(x,y))
screen.blit(enemy,(enemy_x,enemy_y))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Run the program.
Now your game has:
- Background
- Player
- Player movement
- Screen boundaries
- Moving enemy
It feels much more like a real game.
How Does Enemy Movement Work?
Look at this line:
enemy_x += enemy_speed
Every frame:
0
3
6
9
12
15
18
The enemy moves a little farther.
Since the game loop runs many times every second, the movement looks smooth.
Change the Enemy Speed
Slow movement:
enemy_speed = 2
Normal movement:
enemy_speed = 4
Fast movement:
enemy_speed = 8
Try all three values and see the difference.
Mini Project
Create a game where:
- The player moves with the keyboard.
- The enemy moves automatically.
- The enemy returns after leaving the screen.
Congratulations! You have built the basic mechanics used in many arcade games.
Practice Challenge
Challenge 1
Increase the enemy speed to 6.
Challenge 2
Place the enemy lower on the screen.
Example:
enemy_y = 300
Challenge 3
Create two enemies.
Hint:
Create another set of variables:
enemy2_x
enemy2_y
enemy2_speed
Try to make both enemies move independently.
Common Mistakes
Enemy is not moving
Make sure this line is inside the game loop:
enemy_x += enemy_speed
Enemy disappears forever
Check that you added:
if enemy_x > 800:
enemy_x = -64
Enemy moves too fast
Reduce the speed.
Example:
enemy_speed = 2
Best Practice
Avoid writing numbers like 800 and 64 directly in many places.
Instead, create variables:
SCREEN_WIDTH = 800
PLAYER_WIDTH = 64
ENEMY_WIDTH = 64
Then write:
if enemy_x > SCREEN_WIDTH:
enemy_x = -ENEMY_WIDTH
This makes your code easier to update later.
What You Learned Today
Today you learned:
- How to move an enemy in Pygame
- How automatic movement works
- How to use a speed variable
- How to reset an enemy’s position
- How to create a simple enemy animation
Quick Revision
- Which variable controls the enemy’s speed?
- Why do we use:
enemy_x += enemy_speed
- Why does the enemy disappear?
- Why do we reset:
enemy_x = -64
What’s Next?
In Lesson 11: Collision Detection in Pygame, you will learn how to detect when the player touches the enemy.
This is one of the most important concepts in game development and the foundation for scoring, lives, and game over screens.
Frequently Asked Questions (SEO & AEO Optimized)
How do I move an enemy in Pygame?
Create an enemy position variable and increase its X or Y value inside the game loop.
How do I make an enemy move automatically in Pygame?
Use a speed variable, such as enemy_speed, and update the enemy’s position every frame.
Why does my enemy disappear in Pygame?
The enemy moves outside the game window. Reset its position when it reaches the edge of the screen.
Can I create multiple moving enemies in Pygame?
Yes. Create separate position and speed variables for each enemy or use lists to manage multiple enemies.
What is an enemy speed variable in Pygame?
An enemy speed variable stores how many pixels the enemy moves during each frame of the game.
Can the enemy move vertically?
Yes. Instead of increasing enemy_x, increase enemy_y to make the enemy move from top to bottom.
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.