How to move your Game Character Around in Pygame
Last Updated :
23 Aug, 2021
Prerequisites: Pygame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. You can create different types of games using pygame including arcade games, platformer games, and many more.
Images in use:

You can control your player’s movement. For this first create a display surface object using display.set_mode() method of pygame and add player’s sprite using image.load() method of pygame. The set_mode() function is used to initialize a display surface or window. The size argument is a pair of numbers representing the width and height. The flags argument is a collection of additional options. The depth argument represents the number of bits to use for color.
Syntax:
set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0)
Create a variable to store the velocity of the player. Set the initial coordinates of the player. Now, change the x and y coordinates of the player according to the keyboard events i.e. events that occur when the state of key changes.
blit(surface, surfacerect) function is used to draw the images on the screen.
Syntax:
blit(surface, surfacerect)
For collecting all the events from the queue get() function of the event module is used then we are iterating over all the events using a for loop.
Syntax:
get(eventtype=None)
Updating the screen using the update() function of the display module.
Syntax:
update(rectangle=None)
Below is the implementation.
Example: Program for player movement
Python3
import pygame
from pygame. locals import *
pygame.init()
window = pygame.display.set_mode(( 600 , 600 ))
pygame.display.set_caption( 'Player Movement' )
image = pygame.image.load(r 'Player_image.png' )
x = 100
y = 100
velocity = 12
run = True
while run:
window.fill(( 255 , 255 , 255 ))
window.blit(image, (x, y))
for event in pygame.event.get():
if event. type = = pygame.QUIT:
run = False
pygame.quit()
quit()
if event. type = = pygame.KEYDOWN:
if event.key = = pygame.K_LEFT:
x - = velocity
if event.key = = pygame.K_RIGHT:
x + = velocity
if event.key = = pygame.K_UP:
y - = velocity
if event.key = = pygame.K_DOWN:
y + = velocity
pygame.display.update()
|
Output:
The player can also be moved in a continuous movement. For this everything else remains the same except some changes are to be made. Here we are creating a new clock object to control the game’s frame rate using clock().
Syntax:
Clock()
A new variable is created (named key_pressed_is) to store the key pressed by the user. For this, we are using the get_pressed() function of the key module.
Syntax:
get_pressed()
It returns a sequence of boolean values representing the state of every key on the keyboard.
Example: Moving players in continuous movement
Python3
import pygame
from pygame. locals import *
pygame.init()
window = pygame.display.set_mode(( 600 , 600 ))
pygame.display.set_caption( 'Player Movement' )
clock = pygame.time.Clock()
image = pygame.image.load(r 'Player_image.png' )
x = 100
y = 100
velocity = 12
run = True
while run:
clock.tick( 60 )
window.fill(( 255 , 255 , 255 ))
window.blit(image, (x, y))
for event in pygame.event.get():
if event. type = = pygame.QUIT:
run = False
pygame.quit()
quit()
key_pressed_is = pygame.key.get_pressed()
if key_pressed_is[K_LEFT]:
x - = 8
if key_pressed_is[K_RIGHT]:
x + = 8
if key_pressed_is[K_UP]:
y - = 8
if key_pressed_is[K_DOWN]:
y + = 8
pygame.display.update()
|
Output:

Flipping the Player Sprite
You can easily flip any sprite using the flip() function of the transform module of the pygame. For example, if we want to flip the sprite whenever the player changes the direction of movement then we can use the below line
window.blit(pygame.transform.flip(image, False, True), (x,y))
flip() function used to flip the surface object horizontally, vertically. or both. This function has three parameters:
- Image to flip
- Boolean value to do a horizontal flip
- Boolean value to do a vertical flip
Below is the implementation.
Example: Flipping player image
Python3
import pygame
from pygame. locals import *
pygame.init()
window = pygame.display.set_mode(( 600 , 600 ))
pygame.display.set_caption( 'Player Movement' )
clock = pygame.time.Clock()
direction = True
image = pygame.image.load(r 'Player_image.png' )
x = 100
y = 100
velocity = 12
run = True
while run:
clock.tick( 60 )
window.fill(( 255 , 255 , 255 ))
if direction = = True :
window.blit(image, (x, y))
if direction = = False :
window.blit(pygame.transform.flip(image, True , False ), (x, y))
for event in pygame.event.get():
if event. type = = pygame.QUIT:
run = False
pygame.quit()
quit()
if event. type = = pygame.KEYDOWN:
if event.key = = pygame.K_RIGHT:
direction = True
elif event.key = = pygame.K_LEFT:
direction = False
key_pressed_is = pygame.key.get_pressed()
if key_pressed_is[K_LEFT]:
x - = 5
if key_pressed_is[K_RIGHT]:
x + = 5
if key_pressed_is[K_UP]:
y - = 5
if key_pressed_is[K_DOWN]:
y + = 5
pygame.display.update()
|
Output:
We can also easily update the player sprite by creating a list of sprites.
image = [pygame.image.load(r’Player_image1.png’),
pygame.image.load(r’Player_image2.png’)]
Example: Updating sprites
Python3
import pygame
from pygame. locals import *
pygame.init()
window = pygame.display.set_mode(( 600 , 600 ))
pygame.display.set_caption( 'Player Movement' )
clock = pygame.time.Clock()
direction = True
image = [pygame.image.load(r 'Player_image1.png' ),
pygame.image.load(r 'Player_image2.png' )]
x = 100
y = 100
velocity = 12
run = True
while run:
clock.tick( 60 )
window.fill(( 255 , 255 , 255 ))
if direction = = True :
window.blit(image[ 0 ], (x, y))
if direction = = False :
window.blit(image[ 1 ], (x, y))
for event in pygame.event.get():
if event. type = = pygame.QUIT:
run = False
pygame.quit()
quit()
if event. type = = pygame.KEYDOWN:
if event.key = = pygame.K_RIGHT:
direction = True
elif event.key = = pygame.K_LEFT:
direction = False
key_pressed_is = pygame.key.get_pressed()
if key_pressed_is[K_LEFT]:
x - = 5
if key_pressed_is[K_RIGHT]:
x + = 5
if key_pressed_is[K_UP]:
y - = 5
if key_pressed_is[K_DOWN]:
y + = 5
pygame.display.update()
|
Output:
Similar Reads
How to make Flappy Bird Game in Pygame?
In this article, we are going to see how to make a flappy bird game in Pygame. We all are familiar with this game. In this game, the main objective of the player is to gain the maximum points by defending the bird from hurdles. Here, we will build our own Flappy Bird game using Python. We will be us
11 min read
How to Create a pop up in pygame with pgu?
In this article, we will learn about how to create a pop-up in pygame with pgu in Python. PyGame The pygame library is an open-source Python module designed to assist you in creating games and other multimedia applications. Pygame, which is built on top of the highly portable SDL (Simple DirectMedia
6 min read
How to Change the Name of a Pygame window?
PyGame window is a simple window that displays our game on the window screen. By default, pygame uses "Pygame window" as its title and pygame icon as its logo for pygame window. We can use set_caption() function to change the name and set_icon() to set icon of our window. To change the name of pygam
2 min read
How to change screen background color in Pygame?
Pygame is a Python library designed to develop video games. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. Functions Used: pygame.init(): This function is used to initialize all the pygame
1 min read
How to change the PyGame icon?
While building a video game, do you wish to set your image or company's logo as the icon for a game? If yes, then you can do it easily by using set_icon() function after declaring the image you wish to set as an icon. Read the article given below to know more in detail. Syntax: pygame_icon = pygame
2 min read
Dodger Game using Pygame in Python
A Dodger game is a type of game in which a player has access to control an object or any character to dodge (to avoid) upcoming obstacles and earn points according to the number of items dodged if the player is not able to dodge the object the game will be over and final score will be displayed in t
5 min read
How to add Custom Events in Pygame?
In this article, we will see how to add custom events in PyGame. Installation PyGame library can be installed using the below command: pip install pygame Although PyGame comes with a set of events (Eg: KEYDOWN and KEYUP), it allows us to create our own additional custom events according to the requ
4 min read
How to create a text input box with Pygame?
In this article, we will discuss how to create a text input box using PyGame. Installation Before initializing pygame library we need to install it. This library can be installed into the system by using pip tool that is provided by Python for its library installation. Pygame can be installed by wri
3 min read
How to set up the Game Loop in PygGame ?
In this article, we will see how to set up a game loop in PyGame. Game Loop is the loop that keeps the game running. It keeps running till the user wants to exit. While the game loop is running it mainly does the following tasks: Update our game window to show visual changesUpdate our game states ba
3 min read
How to create MS Paint clone with Python and PyGame?
In this article, we will create a simple MS paint program with Python and PyGame. MS Paint is a simple program made by Microsoft, it allows users to create basic art and painting. Since its inception, MS Paint has been included with every version of Microsoft Windows. MS Paint provides features for
9 min read