
In this Keep Player Inside the Screen in Pygame tutorial, you will learn how to stop your game character from leaving the game window.
In the previous lesson, we moved the player using the keyboard. But there was one small problem. If you keep pressing an arrow key, the player disappears outside the screen. Real games don’t allow this.
In this lesson, you will learn how to keep the player inside the game window using simple if conditions. By the end of this lesson, your game will feel much more professional.
What Will You Learn?
After completing this lesson, you will know:
- How to keep the player inside the screen in Pygame
- How to create movement boundaries
- How to stop the player from leaving the game window
- How many beginner games control player movement
Why Do We Need Screen Boundaries?
Imagine playing a racing game.
If your car goes completely outside the screen, you cannot see it anymore.
The same thing happens in our game.
Without boundaries:
❌ The player disappears.
❌ The game becomes difficult to play.
So we need to stop the player before it reaches the edge of the screen.
Our Current Program
We already have code like this:
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
This moves the player, but it never checks whether the player is still inside the game window.
Understanding the Screen Size
Our game window is:
screen = pygame.display.set_mode((800,600))
That means:
Width = 800 pixels
Height = 600 pixels
Our player image is 64 × 64 pixels.
So the player should move only inside these limits.
Stop Moving Left
Replace this code:
if keys[pygame.K_LEFT]:
x -= 5
with:
if keys[pygame.K_LEFT] and x > 0:
x -= 5
Now the player cannot move past the left edge.
Stop Moving Right
Replace:
if keys[pygame.K_RIGHT]:
x += 5
with:
if keys[pygame.K_RIGHT] and x < 736:
x += 5
Why 736?
Window width:
800 pixels
Player width:
64 pixels
Calculation:
800 - 64 = 736
This keeps the entire player visible.
Stop Moving Up
Replace:
if keys[pygame.K_UP]:
y -= 5
with:
if keys[pygame.K_UP] and y > 0:
y -= 5
Stop Moving Down
Replace:
if keys[pygame.K_DOWN]:
y += 5
with:
if keys[pygame.K_DOWN] and y < 536:
y += 5
Calculation:
600 - 64 = 536
Now the player cannot move below the bottom edge.
Complete Program
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Player Boundary")
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] 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.
Move the player in all four directions.
Notice that the player never leaves the game window.
Understanding the Logic
Look at this line:
if keys[pygame.K_RIGHT] and x < 736:
It means:
- Is the Right Arrow key pressed?
- Is the player still inside the screen?
If both conditions are true, then move the player.
Otherwise, stop moving.
Mini Project
Create a game where:
- The player starts in the center.
- The player moves using the arrow keys.
- The player cannot leave the game window.
Congratulations! You have now built one of the most common game mechanics used in 2D games.
Practice Challenge
Challenge 1
Increase the movement speed from 5 to 8.
Does the boundary still work?
Challenge 2
Change the window size to:
screen = pygame.display.set_mode((1000,700))
Can you calculate the new right and bottom limits for a 64 × 64 player image?
Challenge 3
Replace the player image with another PNG file and test the movement.
Common Mistakes
My player still disappears
Check that your boundary values match the size of your game window and your player image.
Why is the player stopping before the edge?
Your image size may be larger than 64 × 64 pixels.
Adjust the boundary values based on the actual image size.
Can I use a different movement speed?
Yes. You can use 2, 5, 8, or 10 pixels per frame depending on how fast you want the player to move.
What You Learned Today
Today you learned:
- How to keep the player inside the screen in Pygame
- How to create screen boundaries
- Why movement limits are important
- How to calculate the correct boundary values
- How professional games keep characters visible
Quick Revision
- Why does the player disappear without boundaries?
- What does this condition check?
x < 736
- Why do we subtract the player width from the screen width?
- Which values change when the game window size changes?
What’s Next?
In Lesson 8, you will learn How to Add a Background Image in Pygame.
Your game will start looking like a real game by adding:
- A background image
- A player character
- Proper movement inside the game window
Frequently Asked Questions (SEO & AEO Optimized)
How do I keep the player inside the screen in Pygame?
Check the player’s X and Y coordinates before moving. Only update the position if the player is still inside the game window.
Why does my player move outside the screen in Pygame?
This happens because the program updates the player’s position without checking the screen boundaries.
What is a screen boundary in Pygame?
A screen boundary is a limit that prevents game objects from moving beyond the visible game window.
How do I stop a sprite from leaving the game window?
Use if conditions to compare the sprite’s position with the screen width and height before changing its coordinates.
How do I calculate the right boundary in Pygame?
Subtract the player’s width from the game window width.
Example:
Window Width = 800Player Width = 64Right Boundary = 736
Why do professional games use movement boundaries?
Movement boundaries keep the player visible, improve gameplay, and prevent objects from disappearing outside the game screen.
Can I use different boundary values?
Yes. Boundary values depend on your game’s window size and the dimensions of your player image.
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.