Platformer Game (Part 2)

In this post we continue with our platformer tutorial for MakeCode Arcade. Make sure you have completed Part 1 of the tutorial before working through this part. In this tutorial we look at adding enemies and collectables as well as improving our jumping mechanics.

Adding Enemies

Enemy Sprite

To add enemies to our level, we can either add them one at a time in the code, or we can use placeholders in the tilemap to indicate the positions that the enemies will be placed. In the example below I use a simple M tilemap to indicate the positions that enemies will be placed at.

Adding enemy placeholders on the tilemap.

NOTE: if you want different kinds of monsters you will need separate placeholder tiles for each different kind of monster.

Once we have determined the positions for the monsters we replace them by sprites in the code. Because we will need to do this replacement for each level we will introduce a function that contains the code to do the replacement.

Functions appear under the Advanced options. Select f(x) Functions and then click on the Make a Function button.

Creating a function

Functions allow related code to be grouped together in a block. We can then call the function anywhere in the code.

We then edit the function by setting the name of the function, in this case “loadGreeblys”. For functions there is also the option to add parameters, which allow functions to be customised more – in this case we do not need to add any parameters.

Naming the function

Here’s the code for my function that loads the enemies. We need to change the enemy tiles (placeholders) to enemy sprites. In this case I have create a new sprite kind called “Greebly” – you could stick with the built-in Enemy type if you wish.

You can use the same image for the tilemap and the sprite, but you will need to copy the image in the tilemap editor and paste it to the sprite image editor. The sprite is placed on top of the tile and then the tile is removed. Finally the initial velocity of the enemy is set – in this case the enemy moves to the left. This could be randomised to provide less predictable behaviour.

Completed function for loading an enemy

After creating the function make sure you call the function in the “on start” block after the tilemap has been set.

Function call

Make sure you call this function in the “on start” block.

We then set the movement of the enemies so that they move left and right. You  can change the movement speed to suit, or perhaps even try random movement speeds. The enemy moves in a given direction until it hits a wall then changes direction. The code in the “on game update” block will be called repeatedly every frame meaning that a check for collision will occur before movement is done.

Configuring enemy movement

Now that we have the enemy movement sorted out we need to check for collisions between the player and enemies. Use the “overlaps” block from Sprites to check for the player overlapping with an enemy. For my game I will go for fairly simple behaviour – if the player touches an enemy they lose a life and go back to the start of the level. In my game the enemies on the first level are invincible so stay in place after a collision.

Collisions between the player and enemies

Collectables

To add collectables, such as coins, the steps we follow are similar to those involved in adding enemies. First we must write a function called “addCoins” which replaces the tile placeholders by coin sprites.

A function for loading coins

Then we add call to this function in the “on start” block.

We then add code to check for an overlap between the player sprite and a coin. If an overlap occurs, we add one to the score and destroy the coin.

Checking for the player touching a coin sprite

Improving Jumping

The initial code for jumping is a bit broken, because while it allows the player to jump, it also allows multiple jumps. To code single jumping we need to ensure that the player is on the ground to jump.

Code for Single Jump

To code double jumping, we need to add a variable to keep track of the number of jumps. If the player initiates a jump while touching the ground the jump count variable is set to 1. Otherwise, a check is done to ensure that the jump count is less than 2, after which the jump counter is increased to 2.

Code for Double Jumping

Next
Next

Platformer Game (Part 1)