The aim is to create a game where you attempt to score a number of penalty goals.
The scene should look something like this:
The idea is that you use the cross to aim by using the arrow keys (up, down, left, right). Use the space key to shoot the ball into the net.
The goalie should use a random number to determine if they dive left or right and use a random number to determine how far from the centre they should move.
We should add our sprites first:
You will need to draw the nets and posts separately. We don’t want to draw them together because it’s easier to determine if the ball hit the posts if they are separate sprites.
Connect your aim sprite up to the left, right, up and down arrow keys. We use the ‘change X by’ and ‘change Y by’ blocks for this.
When the space button is pressed we want to move the ball towards the Aim sprite.
We can use the following block to help us point the ball in the correct direction (towards our aiming sprite):
We can use a loop (repeat block) to move the ball along, we can use a calculation like:
Inside the repeat we can move 5 steps (or change this to be faster, but both the repeat condition and the move steps should be the same).
Inside this loop we can also use the change size block to make it look like the ball is moving away from us.
This will look like:
To determine which way the goalie should move, we should use a random number to determine the left or right direction.
To show the line that follows the ball, we can use the Pen, think of this pen like a real pen. We need to put the pen down before we can write and we lift the pen off the surface to stop drawing. The pen moves with the sprite and leaves a mark.
For a 50% chance:
To determine if the ball hits the net we need to test both the net and the goalie. If the ball is touching the net and not touching the goalie, then it is a goal. We can construct this type of operation by combining operator blocks.
We have learnt about the green flag hat block before and how it is run when we start our program, but we can also create custom messages that we can broadcast to all of our sprites.
Create broadcast messages for the following events:
Reset will be raised after the goal kick has taken place and we need to reset for the second kick.
Win will be raised once the number of goals reaches our desired win value.
Lost will be raised once the number of misses reaches our allowed misses.
Code for the Aim Sprite:
Code for the net sprite:
Code for the post sprite:
Code for the lost sprite:
Code for the win sprite:
Code for the Goalie:
Code for the soccer ball: