Name: Add More Levels
Description: The player reaches the goal of the first Level of the platformer and then progresses to the next level (and beyond)
Need for Pattern: Having more than one level is a way increasing the length and challenge of the game. You can also bring other elements of design and even game mechanics to new levels.
Links to other Computing Patterns: Systems Dynamics, Change Listener
Now we will reate a variable to hold the number of level we are on. We will then be able to change it. We create this level variable at the end of our on start loop.
Create a new Function by clicking on the Advanced tab on our toolbar, then Functions And then click Make a Function. Enter chooseLevel in the white box.
Drag only the set tilemap to into the new chooseLevel function by holding down Control on your Keyboard while you drag the block. Right click and select Format Code to make the screen tidy.
Drag in the call chooseLevel from Functions to the end of the begining of the createLevel function.
Now add a Logic block if true then inside the chooseLevel block Move the the set tilemap to inside the logic block. In the new if true then block replace true with at 0 = 0 block from Logic section.
Right click on the Logic block if level = 0 and duplicate it. Change 0 to 1 in the second block and drag it back into the chooseLevel function.
Repeat the process again changing the level number to two. Move a game over - win block into to this gap and delete the tilemap block.
In our game we have a listener which is always checking to see if there is an overlap between our Player and the Goal Sprite.
In our starting template this lists only a Game Over - Lose block. Delete that block so the on overlap listener block is empty.
We will replace it with code that changes the level number and builds a new level and moves the player back to the starting point.
In the on sprite of kind Player overlaps Door listener block add a change level by 1 block. Next add call createLevels block to recall the function that does the work of adding the different blocks to the game.
If you are not careful then some of the things from the last level like food or enemies may carry over to your next level.
From the Sprites area drag in a destroy all sprites of kind xx block to the start of the createLevel function. Change the option to Food or Enemy or what ever other kinds of object you may need to clear for the previous level.
In the image above all previous items of food are removed. You may need to several of these kinds of blocks for different types of items you add to your game.
Test your game to check that your changes have the desired behaviour and that there are no side effects. For example check that each time you touch the end goal you progress by a level and the design matches.
This Game Pattern is one of many allowing you to make improvements to your platform game and to learn coding and wider computing concepts. Find more on the Game Pattern page.