This article covers the game development of a Platformer game in Pygame.
Welcome to the Pygame Platformer Game Development! In this section, weβll be building a 2D Platformer game using the Python game library, Pygame. Fair warning to all our readers, this article is primarily targeted towards people already somewhat familiar with Pygame.
We will only be skimming over basic pygame concepts, reserving most of our time for the more advanced concepts. If youβre quick and intuitive youβll probably be able to follow along, but I still recommend you read our Pygame Tutorial (aimed towards absolute beginners) first.
This article will cover some advanced concepts (listed below). Due to the sheer size of the code (300+ lines) and the explanation required, weβll be splitting the game across several articles, each tackling a certain number of problems and features.
About the Game
Chances are youβve played one of these platformer games before. Itβs a simple game where you keep moving your character upwards by jumping on the available platforms. If you miss a platform and fall to your doom, itβs game over. You earn a point for every platform you cross. Thereβs no limit to the game, ending only when you fall and die (or get bored and quit).
Included Concepts
Below are all the Pygame game programming concepts included in this game. Donβt worry, all of them will be explained alongside the source code. As mentioned earlier, these will be split across several articles due to size limitations.
- Collision Detection
- Player movement (realistic sideways movement)
- Jump mechanics
- Gravity and Friction
- Random Level Generation
- Warpable screen movement
- Scrolling the screen (creating an infinite height)
- Creating a Score counter
- βGame Overβ Mechanic
- Random Platform movement
Part 1 β Setting the Foundation
In this article weβll set the foundation for our game. Creating our player sprite and setting up some movement controls.
Initialization and Constants
import pygame
from pygame.locals import *
pygame.init()
vec = pygame.math.Vector2 # 2 for two dimensional
HEIGHT = 450
WIDTH = 400
ACC = 0.5
FRIC = -0.12
FPS = 60
FramePerSec = pygame.time.Clock()
displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
The above code is all pre-game related. You can see us importing the pygame module(s), calling pygame.init()
to initialize pygame, setting up several constants such as screen height and width etc. We also set up a clock in pygame, which weβll use later to control the Frames displayed per second.
Next weβve set up the display screen using the WIDTH
and HEIGHT
variables and given the display window the name βGameβ.
Youβll have noticed the constants ACC
and FRIC
and the variable called vec
. These, weβll be using later on in the article to create realistic movement and implement gravity.
Above is an image of our current progress. A 450 by 500 pixel display screen. We have no objects made, so itβs a blank screen with the default black color.
Player and Platform Classes
In this game, weβre going to have two different types of entities. The player who we will be controlling and the platforms on which weβll be jumping. Weβre going to create two different classes for each one of these entities.
If you havenβt been using classes until now, this is a good time to start. This approach allow us to easily duplicate and access the objects weβre going to be creating. Youβll realize this once we begin creating many platforms. For now weβre just making one.
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.surf = pygame.Surface((30, 30))
self.surf.fill((128,255,40))
self.rect = self.surf.get_rect(center = (10, 420))
class platform(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.surf = pygame.Surface((WIDTH, 20))
self.surf.fill((255,0,0))
self.rect = self.surf.get_rect(center = (WIDTH/2, HEIGHT - 10))
PT1 = platform()
P1 = Player()
Most of this should only require basic Pygame knowledge. We create surface objects for each class with a fixed size. We give each of them a color using the fill()
function (RGB format). Finally, we create a rect
object from the surface object using the get_rect()
method on the surface object.
The center = (10, 420)
and center = (WIDTH/2, HEIGHT - 10)
parameters we passed are used to define the starting position of the objects when they are drawn to screen. Remember, top left hand corner is the origin point with the co-ordinates (0, 0)
.
Finally, we create two objects, PT1 (stands for platform 1) and P1 (stands for Player 1). These names are completely arbitrary of course, and you can change them to whatever you want.
We have no images to show our progress so far, because the screen still shows the same black screen as before. This is because we havenβt drawn any of the objects we created above to the display screen.
Sprites Groups + Game Loop
In this section weβll work on creating the game loop as well as introducing sprite groups.
For now weβll go with a generic βall_sprites
β, Sprite group and if the need arises, weβll create more later. Weβve proceeded to add both the platform and the player to this sprite group. This enables us to easily access all these sprites at the same time as youβll see later.
all_sprites = pygame.sprite.Group()
all_sprites.add(PT1)
all_sprites.add(P1)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
displaysurface.fill((0,0,0))
for entity in all_sprites:
displaysurface.blit(entity.surf, entity.rect)
pygame.display.update()
FramePerSec.tick(FPS)
Weβve setup the game loop to be able look for the QUIT
event and shut down Pygame accordingly. Besides this, we use the fill()
function on the displaysurface
to refresh the screen with each iteration.
Next up in the game loop, we iterate through the all_sprites()
group, drawing all of them to the screen. Without sprite groups, we would have to individually draw each one of them to screen.
Finally, we use the update()
function to push all the changes to the screen and update it. The tick()
function, used on the Clock()
object we created earlier limits the Game loop to refreshing 60 times per second.
This is what our current progress in our Platformer game has resulted in. However, we canβt interact with or control our player in any way yet. Weβll be dealing with this in the next section.
Implementing Movement
Now, this is a fairly complex part that uses concepts from Kinematics (Physics) and the equations of motion to bring in the concept of acceleration and deceleration. Furthermore, weβve also added the element of friction, else your speed would be sending you flying all over the place.
Due to the complexity, weβll study this in shorter pieces. First weβre going to add the following three lines to the Player class (init function
).
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.surf = pygame.Surface((30, 30))
self.surf.fill((128,255,40))
self.rect = self.surf.get_rect()
self.pos = vec((10, 385))
self.vel = vec(0,0)
self.acc = vec(0,0)
Itβs not as complicated as it looks. vec
is simply used to create variables with two dimensions. If you go back and look at the start where we initialized it, youβll see that itβs creating vectors. If youβre good with maths and physics, youβll understand this quickly.
Creating two dimensional vectors allows us to keep things simpler. Remember, velocity and acceleration are vector quantities. There is horizontal acceleration and also vertical acceleration. Same goes for velocity.
The first parameters represents acceleration/velocity along the X axis
and the second is for the Y axis
. Notice that weβve removed the center parameter. This is because weβve shifted control of the Playerβs position to the self.pos
variable. Next up is the move()
function that will allow us to control our player.
def move(self):
self.acc = vec(0,0)
pressed_keys = pygame.key.get_pressed()
if pressed_keys[K_LEFT]:
self.acc.x = -ACC
if pressed_keys[K_RIGHT]:
self.acc.x = ACC
This first part is pretty simple. The function first re-sets the value of the acceleration to 0, then checks for key presses. If the left key has been pressed, it will update the acceleration with a negative value (acceleration in the opposite direction). If the right key has been pressed, acceleration will have a positive value.
self.acc.x += self.vel.x * FRIC
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
This part is a bit complicated, so you can simply copy it if you want. You can see an equation of motion there on the third line. We also use friction to to decrease the value of the velocity. Without friction, our player would not de-accelerate. You can tweak the value of the FRIC
variable to adjust the movement.
if self.pos.x > WIDTH:
self.pos.x = 0
if self.pos.x < 0:
self.pos.x = WIDTH
self.rect.midbottom = self.pos
These two if statements are a clever trick that allows βscreen warpingβ. In other words, you can βgo throughβ the left side of the screen, and pop up on the right side. Of course, if you donβt want this feature, you can re-purpose the two if statements and wrap them around the whole code to ensure you donβt move off screen.
The last line updates the rect()
object of the Player with the new position that it has gained after being moved.
If you donβt want to add these concepts of Friction and acceleration, you can just go with the regular movement system that most games use. You can find it anywhere online or at our own Pygame Tutorial here.
Weβve created the move function, but itβs useful until weβve actually connected it to the rest of our code. Simply add the following line into your game loop.
P1.move()
This will cause the move() function of Player 1 to be called in every iteration of the game loop.
Below is a short video, show casing what weβve accomplished so far.
If you have any trouble with some of the code above, I recommend you try running it piece by piece and experimenting with it on your own. Leave out certain lines to discover their effect on the game. Game development in Pygame is a skill learnt best when youβre the one tinkering with the Platformer (or any game) code yourself.
Click on the button below to head over to the next Part in this series of Game Development with Pygame Platformer. The complete code for this article is also available in Part 2.
Related Articles:
- Pygame β The full tutorial
- Pygame projects with source code
- Audio and Sound β Pygame Mixer
- Pygame RPG Tutorial Series
Interested in taking things to the next level? Check out this article on Game Development Books to become a real Game Developer!
This marks the end of the Pygame Platformer Game Development article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article material can be asked in the comments section below.