In this tutorial series we will be working to create a side scrolling game with pygame. This game will involve objects randomly appearing on the screen that our character must avoid by jumping our sliding.
We are going to be using a series of different images to animate our game. Please download all of the game assets from one of the below links.
GitHub: Click Here
Download Zip: Download Now
Note: The folder named "images" must be in the same directory as your python file.
To save us a bit of time I have created a starter file for us to work off of. This file simply contains the code to load all of the images and animate the character. If you would like a detailed explanation of the starter file please watch the video.
In every game there is something called a main loop or a game loop. This loop runs until the game is exited and is what is responsible for checking for events from the user. In our program we will use a while loop to represent the game loop.
To make it appear as our character is moving forward we will actually just scroll the background backwards. We will use two background images and move them to the left each frame. Once one of the background images reaches the end of the screen we will reset its position back to the right. This will allow us to continually scroll the background.
Now that we are moving our background we need to draw it to the screen. To do this we will setup a function and call it from our game loop. This function will be called redrawWindow() and will be responsible for drawing all of our objects to the screen.
Changing the Background Speed
Now that we have our background moving we want to increase its speed at a regular interval. To do this we will use a timer event. Every time the timer goes off we will increase the speed and reset the timer.
To check if this timer is triggered we will add some code the game loop.
Now our background increases in speed!
Drawing our Character
Since we already have our player class setup this is not very difficult. We just need to create an instance of our player and then call its draw method from the redrawWindow function.
Now we need to draw this player.
Moving Our Character
To move our character we need to check for some specific key press events. We will do this inside the game loop.
And now our character can slide and jump!