## Create Tilemap Arrays, more power-ups, and a secret level!

Written By: Cherie Tan

Difficulty
Moderate
Steps
14

### Introduction

This guide continues on from our previous guide, 2D Sidescrolling Platformer with MakeCode Arcade

In this guide, we'll show you how to create multiple levels, tile maps, backgrounds, more items as well as a secret level.

Complete this guide to take your 2D sidescrolling platformer to the next level, literally!

### Step 2   Create Levels

• As we've done before, we'll create a function again. This time, it will hold all relevant code that will create a new level in our 2D sidescrolling platformer game in MakeCode Arcade. Make a new function and name it createLevel
• Take note that the function type used here accepts an integer number argument, num. This num will be used to choose from different level maps and backgrounds.
• So far, we've used arrays for multiple enemy sprites as in our version of Space Invaders. This time, we'll use an array to hold the tile maps for all the four levels of our game.

Create an array and name it levelTileMaps, then duplicate the first level's tile map (from on start block) and place it in the array. That will be the first element of our array, an index 0.
• Duplicate the tile map three times, then edit them in the sprite editor to change the positions of the tiles. These will be the next three elements of our levelTileMaps array.
• Next, we'll create another array called backgroundMaps. As its name suggests, this will hold all the different backgrounds of the levels.
• In this guide, we've imported backgrounds created in an external software such as Aseprite. If you haven't done so yet, check out our previous guide on how to import your own.
• From the Scene tab, get out a set background image to ... block, place it right after set backgroundMaps to array of ...
• Then get out a list get value at ... block. Change this to read backgroundMaps get value at num block.
• We've also placed a set character position to x 25 y 88 block into the createLevel function, this will reset the player's position.
• The block from on start have been repeated here: set tile ... to ... with wall ON blocks.
• Finally, we'll still need to call the generateCoins function, generateItems function, and generateEnemies function to re-spawn these sprites every time a new level is created.

### Step 3   Create Secret Level

• Apart from the four levels, there will be another secret level! Create a new function for this and name is createSecretLevel. Change its background image and tile placement however you like.
• In this guide, we've also customised the tile colours for the secret level. Feel free to change it however you would like to.
• Reset the player's position with a set character position x 25 y 20 block.
• Create two new functions now, name them clearItems and generateSecretItems. Later, we'll add blocks to these two functions. For now, just make a call to both functions.
• Then, drag and drop a start countdown ... (s) block into the function. Set this to ten seconds.

### Step 4   On countdown end

• Once the countdown to ten seconds ends, the player will be transported back to an ordinary level. So get out a on countdown end ... block.
• Then make a call to createLevel function.

### Step 5   Update on sprite of kind Player overlaps otherSprite of kind PowerUp

• With more levels, this also means that there will be more than one powerUp that the player may pick up. Change the conditional statement to read if poweredUp greater than or equal to 1 then.
• Create another conditional statement, this time it should check if poweredUp = 2 then.
• This is an arbitrary condition that we will use to trigger the secret level map.
• After the destroy powerup, change poweredUp by 1, and play sound magic wand blocks, call the createSecretLevel function.

### Step 6   Clear Items

• Likewise, add a for element poisonousMushroom of array of sprites of kind Enemy block into the function. This block can be found in the generateEnemies function.
• Add a destroy poisonousMushroom block into this for loop.
• Previously, we've created a clearItems function but haven't yet added any code to it. So add three destroy ... blocks into it. Set it so that the heart, flag, and powerup sprites will be destroyed with this function call.
• Add a for element coin of array of sprites of kind Coin block into the function. Note: This same block has been used in our generateCoins function, so feel free to duplicate it from there.
• Add a destroy coin block into this for loop, this will make it so that every coin sprite in the array will be destroyed.
• Back in the createLevel function, add a function call to clearItems right before call generateCoins.

### Step 7   Generate Secret Items

• The next function we'll need to add code to is the generateSecretItems function we had created in a previous step. This function will be used to generate items exclusive to the secret level map. As you might notice, this function is quite similar to our generateItems function.
• Create new sprite art for the three items.
• Set their position as such so that they appear one after another.

### Step 8   When Player overlaps Golden Heart Sprite

• As we've done in our previous guide, we will add Overlap Events. This can be used to determine what happens when certain sprites overlap other sprites in the game. Here's what this chunk of code does:
• The on sprite of kind Player overlaps otherSprite of kind superLife block will increase the player life by 10.
• Then the sprite is destroyed with a destroy goldHeart block
• A sound effect is played with the play sound power up block

### Step 9   When Player overlaps Immunity Potion Sprite

• Next,  here's what this chunk of code does:
• The on sprite of kind Player overlaps otherSprite of kind immunity block will check to see if the player sprite has overlapped with the immunityPotion sprite.
• The immunityPotion sprite is destroyed with a destroy immunityPotion block.
• Create a new variable, immuneAbility and set to 1. This will be used to make the player immune to enemies later on in the code.
• Finally, play a sound effect with play sound power up.

### Step 10   When Player overlaps Gem Sprite

• Now to program what happens when the player sprite touches the third special item, the gem sprite, use a on sprite of kind Player overlaps otherSprite of kind gem block. What this chunk of code does:
• Changes the player score by 25 with a change score by 25 block.
• Destroys the gem sprite with a destroy gem block.
• Plays a sound effect with a play sound power up block.

### Step 11   Update on sprite of kind Player overlaps otherSprite of kind Enemy

• As we've added the immuneAbility, we still need to update the chunk of code that determines what happens when the player sprite overlaps with an enemy sprite! Add the following blocks to it, so that if immuneAbility = 1 and character overlaps with poisonousMushroom then three things will happen:

change life by 0
destroy poisonousMushroom sprite
play sound effect ba ding

• If the immuneAbility is not equals to 1, then that means the immunity potion has not been picked up, so the player is not yet immune to enemies.

### Step 12   Update Win Condition

• Finally, we've yet to update the win condition, so go back over to the on game update every 500 ms block. Now when the character sprite overlaps the flag sprite, change level by 1.
• Within that conditional statement, add another conditional statement: if level > 3 then game over WIN.
• If not, make a function call to createLevel.

### Step 13   Upload code to Meowbit

• Click on Meowbit
• Drag and drop the 2d-pltformer-with-more-levels.uf2 file to ARCADE-F4
• Want the guide's .uf2 file? Feel free to download a copy of the game here

### Step 14   Conclusion

• You're all done! You can play the complete game, here
• How can you modify this game to make it more fun? Some suggestions:

• Tinker with positioning of the randomly generated enemies and coins, so that they always appear above a tile.
• Add different enemy sprites with different penalties
• Add a shop-based system in the secret level, so that you can trade coins for more items.
• Check out the guides over at https://www.littlebird.com.au/a/how-to/#micro-bit to learn even more. Happy gaming!
© 2022 Little Bird Electronics Pty Ltd.
Made with ❤️ in SYD. All prices inc GST. ABN 15 634 521 449. We're 🐥 @lbhq on Twitter.