This week we are going to be building a fun game where we pick up and throw Bananas at a flying object.
We first need to setup the scene with our sprites. I’ve added a Bat, Banana and the Monkey.
My backdrop is a nice plain brick wall.
The first part of our game that we are going to add some script to is moving the monkey.
We really want our Monkey to move left when we press the left arrow key and right when we press the right arrow.
We always want our Monkey to be ready to move, if we want to always be doing something, then we need to use a forever block. On our Monkey sprite, let’s add the script:
Now, it doesn’t do a lot, we really need to check to see if the arrow buttons have been pressed. If we want to test something, then we need to use the if -> then block.
We can find the blue key pressed block under the sensing tab. It will look like:
Notice that the shape of the block is the same as the if -> then shaded hole block?
Make the Monkey Move
Now that we can test if the arrow keys have been pressed, we need to make our monkey move. First up we get him to point in the correct direction. Use the point in direction block for this:
We need to make sure that our monkey points left inside the ‘left arrow’ if -> then block and that our monkey points right inside the ‘right arrow’ if-> then block.
Now is a great time to test our script.
Did we find that our Monkey turns upside down when we change direction?
Scratch thinks that it can rotate the sprite when it changes direction, we need to tell it that it should mirror the sprite. We do this by setting the rotation style to left-right:
Our Monkey now moves correctly! But it doesn’t look like he is running anywhere. To animate him to make it look like he is running, we need to switch costumes. We can use the ‘next costume’ block for this:
Our code now looks like:
We added another block to make sure that our monkey starts in the same spot each time.
We would like our bat to fly from side to side across the screen, this is going to give our monkey something to aim at. Switch over to the bat sprite and be sure to add the green flag hat block.
We always want our bat to be flying, so we will need to use a forever block.
Test this and observe what happens.
Notice that our bat hits the wall and stays there. Let’s find the special block that will help our sprite bounce back off the wall.
Now our code looks like:
We want to be able to fly back and forth only until the bat hits a banana.
To wait until the bat hits the banana we need to add another new block:
We can combine this with the touching sense block:
Now our bat will continue to fly backwards and forwards until it touches the banana. At the moment we don’t have any script after the repeat-until block that makes the bat fall out of the sky.
We need to add a few other little blocks to make the movement of the bat look better when it hits the banana.
After the ‘repeat until’ blocks, we can move the bat downwards, so that it makes him look like he is falling from the sky. Then we move a number of steps, which creates the falling effect.
This gets added to form:
Test it out: Try moving the banana over the bat when its flying. This will tell us that our logic is working.
Notice how we need to reset the bat’s position?
Add a go to block above the ‘repeat until’ block.
Now we just need to pick up and throw our bananas!
The last part that we need to add is the ability for our monkey to pick up and throw the bananas.
Let’s start with the banana pick up. We always want the banana to follow the Monkey as he moves around. We want the banana to follow the monkey until we press the ‘space’ key.
Test: Our banana should follow the monkey.
We also set the banana pointing up, so that when we press the space button and start throwing, it will be in the correct position to start moving.
After the space key has been pressed, we want to move the banana up the screen so that it looks like it has been thrown.
Next, we want the banana to appear either on the left or right hand side of the screen, ready for the monkey to pick it up again. Let’s use a “random number” to choose which side to place the bananas on once they appear again.
Finally add the wait “until touching Monkey” block. This will cause the bananas to sit in the corner until the monkey touches them. Once this happens it will cause the bananas to follow the monkey along, until the space button is pressed.