Module 3 Developing Movement & Making New Levels
Developing Movement & Making New Levels
Of course games aren't just about jumping up and down – you’ll be wanting your character to jump off things and bump into walls, too… important stuff for making your game interesting and challenging to play.
All good platformers need a variety of levels too, and in this module you’ll learn how to add new levels to your game so Shaun has lots of exciting environments to explore. Let’s take a more in-depth look at jumping, running and falling, as well as making your game a bit more expansive…
As ever, launch the project on the right and let's get started!
No Youtube video above? Download it here!
- Changing Animation
- New Levels
- Duplicating Levels
- End Zones
Now we want to get the character jumping… but first we need to create some more variables.
In character we’ll make four variables (all for this sprite only):
- 'gravitySpeed' (technically I know gravity isn’t a speed but this is just to make things a bit more simple). We'll use this to store a value of the maximum speed we want our character to fall.
- 'state', explained below.
- 'jumpSpeed', this is the amount of force the character will experience pushing upwards.
- 'dir' (direction) this stores whether the character is facing left or right.
We said we'd explain what a 'state' is, and we're people of our word.
But first, think about the movement of your character - it can be in a running, jumping or idle (standing still). These are all a type of...
A state is a type of variable, but it’s used to show what condition something is in.
We can use these motion states to determine what animation the game should show. If your character is running we show a running animation, if they’re jumping we show a jumping animation, and so on. We can also use states to stop you being able to jump if you’re already in the air and stuff like that.
The 'initialiseGame' message will tell anything that’s listening to reset all of it’s variables and positions, ready to start the game again (like when you restart a level).
Next we need to update the character sprite code.
We add a “when I receive 'initialiseGame'" block. This is where we reset the position of our character and set gravity. (we have chosen 10).
Here’s a close up of what your code should look like at the moment!
Now we need to change the last 'if' statement so it matches the code here.
Here, we are checking 3 things:
- That the player has pressed the 'Up Arrow'
- That the character isn’t already jumping
- That their feet are touching the floor.
If all three of these conditions are met we can start the jump process. To do this we set the jump speed to a value higher than that of gravity (the higher the number we make it the higher Shaun will jump).
We also want to set our 'touchingFoot' variable to be 'false'. We’ll explain the reason for this in a bit.
Here we are checking if the character is jumping and reducing the amount of jump speed they have (as long as it’s above 0).
This is where we actually move the character. First we check to see if the foot sensor is touching. If not, we set the state to jump and move the character in the Y plane (up or down) depending on the speed of gravity and current jump speed.
The reason we set the 'touchingFoot' variable in the block further up to 'false' was to make this section run, if either the player presses jump or the character falls off a ledge.
When we press jump, the 'jumpSpeed' starts as a high number, then while we are in the air it reduces in power. So, at first your character will climb the air quite quickly, but as the jump continues and the jump speed value decreases, your character will then start to fall back to the floor.
The eagle-eyed among you will have noticed that rather than just using an 'If' statement we’re using an 'if' then 'else' in the last bit. This means that if the condition in the top bit isn’t met, the game will run instructions in the else bit .
In this case we’re setting the state to 'idle' and the jump speed to the same as gravity. This ensures that if your character walks off an edge they will still accelerate to full gravity speed.
So now we have a fully movable character who can run, jump, fall, and get stopped by walls!
We will now use our 'dir' variable, which we made earlier.
First, we need to check your character’s state is not jumping; if it isn’t we can reset the state variable to 'idle'.
Next, in the section of code where we move the character left and right, we can also check if we’re jumping. If not, we change the state to 'run'.
Here’s a close up of the changes we made to the character state.
Now we set our 'dir' variables to either 'left' or 'right' depending on which arrow was last pressed.
Now on the 'idle' character costume we add this script.
We listen for the 'updateAnimation' message from the the game loop and tell our costume to only show it if the character’s current state matches the one specific to this costume. We also set its x and y positions to the character and flip it depending on the 'dir' variable.
Once you have added this bit of code, duplicate it onto your other costumes. You also need to update the states so they match the animations, e.g 'run' and 'jump'.
Next, we need to make some instructions to change the level, but because we will want to do it more than once, we’ll put it inside a custom block. In some programming languages these are called 'functions'.
So, to make a custom block, we need to go to 'More blocks' and choose 'Make a block'. A pop-up box will appear, in here we’re going to name the custom block.
In this case, we want to make a set of instructions which will take us to a level. So we’ll call our new block 'goToLevel' and add a number under the 'Options' drop down. The number we put into this block will be the level number we want to go to.
We now need to grab the forever loop from our 'when flag clicked' block and drop it into our new block and set the 'currentLevel' variable to the number input.
To set the 'currentLevel' variable to the number input of the new block, just drag the 'number1' in to the set block.
We also want to broadcast a new message called 'levelStart' before we start the forever loop. We’ll use this when we want to initialise a new level. We also want to broadcast an 'initialiseGame' message. This happens at the very beginning of the game and not at the beginning of each level - we’ll use this to set up variables and states which need to be set at the very beginning of the game.
Now, whenever we want to run this block, we can just call it from anywhere in the sprite (We’ll do this later). For the moment, though, we just want to run it after we have started the game.
Now, in your Level1 sprite, we need to make it only show if we are on level1. So we add the 'when I receive' blocks for 'levelStart' and 'initialiseGame' to it and another variable called 'myLevel' (for this sprite only).
This is what your level 1 code should now look like.
So when we receive the message 'levelStart' we need to check if the game’s current level is the same as the level it is supposed to be!
If this is true, we show this level, if not we hide it. This way we can design loads of levels with this piece of code on them, and just change the number.
This makes game coding A LOT easier!
To make new a new level, it’s really simple! All you need to do is right click on the level you have just completed and select 'duplicate'.
Then in the 'when I receive initialiseGame' block, make sure you change the level number to 2.
Once the character has reached the end of a level. We want them to proceed on to the next one.
To do this we need to make a sprite, could be a door, could be a magic portal, could be a swirling vortex. You can decide what your new sprite looks like in the 'costumes' tab.
Soooo…. in the Game sprite we need to add a 'when I receive block' for that message so we add this.
It'll skip the game on to the next level when it receives the 'gotoNextLevel' message.
WOW! You can now create multiple levels and your character has the agility to complete them, but this isn't where game creation stops! The next step is creating puzzles and enemies for your character to defeat.