If you are a fan of Scratch 3.0, you must have played some of its platformer games. Platformer games (often simplified as platformer, or jump ‘n’ run) is a video game genre and subgenre of action games. Platformers are characterized by their heavy use of jumping and climbing to navigate the environment and reach their goal.
Basically there are two types of platformer game in Scratch 3.0: Static and Rolling. Static Platformer Game means that the backdrop would not scroll but the sprites could move or jump on the stage flexibly. Usually when the sprite moves to a specific edge or touches something, the level is cleared. Rolling Platformer Game means that the backdrop is rolling based on the action of the player, but the main player could always be kept at a specific position on the stage. It looks like having a camera fixed to the player.
On Scratch Wiki, there are many posts explaining how to create Platformer game, but they did not explain in detail why the code should be designed in that way. In this post, I would fill in this gap and concentrate on explaining the sample code.
I will just explain static platformer game in this post.
A demo project could be found here: https://scratch.mit.edu/projects/452056274/
Rules for the Platformer Game:
Different platformer game has different rules. For example, some allows double-jump or trible-jump, which means to jump when the Player is already in the air. Some allows for changing direction in the air. Those different behaviors influence the design of the code. For this sample project, its rules are listed here. They are also the most widely used rules for platformer games on Scratch 3.0.
- The Player could move left by pressing Left arrow key, or clicking mouse to the left of the Player.
- The Player could move right by pressing Right arrow key, or clicking mouse to the right of the Player.
- The Player could jump into the air by pressing Up arrow key, or clicking mouse over it. The Player could only jump when it is standing on the platform.
- If the Player bumps up onto an overhead platform or other obstacles, it could be bounced back and fall.
- When the Player is jumping, the algorithm considers the influence of gravity. When jumping up, the Player decelerates, while falling down, it accelerates.
Main Code Segment
The main code segment will run after the game begins. It sets the initial values of Player position and sets “XV”, “YV” values to 0. The two variables represent the movement speed in horizontal and vertical directions, respectively. After that, it wraps the block “Physics” to control the movement of the Player. Since “Physics” block is wrapped in “repeat until” block, it would run repeatedly until the Player clears or fails this level.
Now, let’s take a look at how “Physics” block controls the movement of the Player. This is right the core of the platformer game.
Step 1: Adjust Y position of Player and Check if Touching Platform
The program first changes the value of XV and YV. When Right Arrow key is pressed or mouse clicks to the right of the Player, XV will change by 1. When Left Arrow key is pressed or mouse clicks to the left of the Player, XV will change by -1. Please note here XV is changed by 1 or -1, instead of being set to 1 or -1. It is based on the common sense that the speed change is continuous and relevant to the previous state.
The change of YV is always -1, which simulates the effect of gravity and also reflects the existance of gravity. The program will first change YV and judge if the Player touches the Platform. it the Player does, the program adjusts its y position to make it just 1 unit away from Platform and set YV to 0.
Please note that YV could be either positive value or negative value. When YV > 0 and the Player touches Platform, it might be an overhead platform. Using this sample code, the Player could adjust its position to move down until it is 1 unit away from the Platform. At this point, YV value is set to 0, meaning a relative static state in y direction.
if you want the program to identify other hanging obstables, what you need to do is to encapsulate more “touching” blocks. For example, replace “if <touching Platform>” with “if <touching Platform> or <touching obstacle 1> or <touching obstacle 2>” in this code segment.
Step 2: Move Horizontally and Check If Touching Platform
The Player will move XV steps in x direction. XV is the value gotten in Step 1. After that, the program also judges if the Player touches Platform. The judgement here handles the condition when Player touches a platform wall, as shown in the below diagram. The program will adjust the x position of Player to make it just one unit away from the platform wall.
Step 3: check if the Player could jump
As I explained in Step 1, if the Player is staying on the Platform, it is actually 1 unit above the Platform (like “floating” on the platform). Under this condition, if the Player changes y position by -1, it could touch the Platform.
In this Step, the program changes y by -1 and checks if the Player touches Platform. If the Player does, meaning that it is standing on the platform, so when Up arrow key is pressed or mouse clicks over it, the Player will jump up by setting YV to 16.
The above three steps form a complete mini cycle of position adjustment for the Player. It adjusts the horizontal and vertical position of the Player and captures the jumping order. Since the computer runs very fast, running the above batch of code might just require a fraction of a second. As we could see on the game demonstration, some minor position adjustment would not be caught by our eyes and what we see is a smoothly running Player and a platform game.
Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.
One thought on “Design Platformer Game in Scratch 3.0 – Algorithm Behind the Sample Code – Part 1”
Pingback: Design Platformer Game in Scratch 3.0 – Algorithm Behind the Sample Code – Part 2 – The Coding Fun