How to move your Game Character Around in 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.
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.
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.
Updating the screen using the update() function of the display module.
Below is the implementation.
Example: Program for player movement
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().
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.
It returns a sequence of boolean values representing the state of every key on the keyboard.
Example: Moving players in continuous movement
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
We can also easily update the player sprite by creating a list of sprites.
image = [pygame.image.load(r’Player_image1.png’),
Example: Updating sprites
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course