We are going to build a game that is very similar to something popular, we are calling it: Flap a Bat.
The goal of the game is to keep the bat flying and not hit the walls that close in. The bird will fall from the sky unless you hit the space key to give it some extra boost.
We will need a few sprites to get our game working:
Making the Bat Fly
Our bat should always be falling from the sky, we know that the Y axis controls the up / down movement. If we always want something to happen, then we should be using the forever block.
I’ve moved our bat to the position I want it to start flying with. Test this out and we will see that our bat slowly falls from the sky. We can animate the bat flying by adding in a costume change.
Giving the bat some extra boost
At the moment our bat falls from the sky with no way to keep it in the air. We need the user to tell us when to make the bat fly. We want our user to press the space key. To make our bat fall we used a negative number, so to make it fly we can use a positive number:
Make the building move
We want our building to be able to move across the screen, let’s select the bottom building sprite first. Drag it to where you want to start the movement.
I moved my building all the way to the right hand side of the screen. This will set the correct position on the X – Y movement blocks which we can use.
We can then use the glide block to set the position of the building that we want it to move towards:
I changed the X position to be -240 which is the lowest X number that our game area has.
If we run this we see that the building moves across the screen, but only once. We need to change this to happen all the time.
The next problem we have is that the buildings are always the same height from the bottom of the screen. We can add a little bit of randomness to this.
We used negative numbers because the bottom half of the screen on the Y direction is between 0 and -180. If we test this out, we find that the buildings move diagonally. This is because we set a Y position in the glide block. We want this Y position to be the same number that we randomly moved, if the glide block has the same Y position at the start and end, then it will move straight.
Drag the Y position block into the glide block:
Now we have a building moving across the screen at the bottom.
The process for moving the top building is the same, but this time we use positive numbers for the random part. Be sure to place the building in the starting position first!
My code looked like:
We need a test to see if the bat hits the building. To test if the bat is touching the buildings we can use an “If” block. We need to see if the bat hits either the top building or the bottom building.
Select the bat sprite and modify the script to include the “If” test:
I’ve made my game stop at this point, but later we will change this.
We can also add a test to see if the bat hits the edge of the game screen:
We need a way to score our game, if we count each time the one of the buildings gets to the end we can use this as a chance to give the player a point.
Add a variable called ‘Score’ and select one of the buildings to use as the scoring reference.
We only need to do this to one building.
Increasing the difficulty
Our game is good, but we can make it get harder the longer the player plays it. We can make the buildings speed up.
Create a new variable called ‘Speed’, we are going to use this to decrease the amount of time that it takes for the building to move across the screen.
Use the same building sprite that we added our scoring code into, the other building will just need the speed block added to the glide:
Our game gets a little faster, but at some point the speed number is going to get really fast and it will look like the buildings aren’t moving at all. We need a way to set the max speed of the game:
We add a test to see if the speed is less than 2 seconds and then set it back to ½ a second. You can experiment to find a good number.
Give the bat lives and decrease the lives each time it hits a building. We can add an extra variable that will keep track of how many lives the bat has.
We still make it so that the game is over if the player hits the edge, but we take away a life each time the building is hit. We need to make sure that we move the bat to the middle of the screen to give the player a chance to keep going.
We also need a check to see if all of the lives are used up:
Where should this test go?