Scratch Intermediate - Duck Shooter Game
Scratch Intermediate - Duck Shooter Game
In this exercise you are going to create a game to shoot flying ducks. The screen is shown
below. The ducks will enter from the left of the screen and you need to position the cross hairs
to shoot the duck. Your score will be recorded.
Duck Shoot
We will also reduce the size of the duck by clicking on the Shrink button. Click this
button four times.
To make the duck fly, we need to have another costume for this duck
with the wings in a different position. From the New Costume menu
choose Import and import Duck 2. Edit this costume also to remove the
white space and to resize it. Rename the Duck sprite to Duck1 and the
duck costumes to Ducka and Duckb.
Flying Ducks
Scripts are the programs that enable actions to take place. We need to create scripts to make
the duck fly. We will begin by making the ducks wings flap. Begin by positioning the duck in
the centre of the stage.
To make the duck fly we continually change the ducks costume from Ducka to Duckb, making
sure we leave a short time delay so that we can see the costumes change.
Click on the scripts tab and then choose the Control block. Since we want the
duck to start flying when the game starts, we select the when is clicked
command and drag it to the scripts area..
Now, from the Looks block, drag switch to costume Duckb to the
scripts area. If we join this to the when clicked command the duck
will change to costume Duckb.
Join the two commands as shown and then click the green flag. You
will see the costume change.
From the Control block drag the forever loop command. Drag the
Switch costume commands into the loop as shown.
Click the green flag to see what happens. The duck changes costume,
but it happens so quickly that you do not notice it. We must slow the
process down.
From the Control block, drag the wait 1 secs command to the scripts
area. And place it below switch to costume Duckb. Place a second copy
of the wait 1 secs command below the switch to costume Ducka
command. Change the wait time from 1 secs to 0.2 secs and then press
the green flag.
We now need to make the duck move across the screen. To do this we need to change the x
position of the duck. We begin with the duck at the left of the screen and move it to the right.
Begin as before with the when is clicked command. Again we will use the forever command.
However we need to set the start position of the duck. Since this is at the left of the screen the
x position is -240. We will also start the duck half way up the screen this is at y position 0.
This will set the start position of the duck, but will not move it across
the screen, to do this we need to use a loop to increase the x position.
We now need to repeat this command until the ducks x position is 240
and then start over again. Select the repeat until loop from the Control
block and drag it across to the scripts area and place the change x by 10
and wait 0.1 secs inside this loop.
We will repeat this loop until the x position of the duck is 240, ie the right
hand side of the screen. We need to create a rule that will repeat the
actions until the x position is greater that 240. To do this we will use the
x position statement from the Motion block and the > operator from the
Operators block. Drag the x position statement in to the left hand box
on the comparison control and type the number 240 into the right
hand box. This creates the statement as shown.
No place this statement into the repeat loop and place this loop
inside the forever loop to create nested loops as shown.
Click the green flag and watch the duck fly across the screen. You
can change the speed by altering the wait time.
Shooting the Duck
We need to add scripts to the cross hairs so that we can fire the gun. We will allow the gun to
move up and down and left and right.
Create the script to move the cross hairs down, notice that
the button used is the down arrow and y is changed by -10.
Create the following scripts to move the cross hairs left and right.
Use the arrow keys to move the cross hairs to check your scripts work.
We now need to be able to fire the gun to hit a duck. We will fire the gun by pressing the space
key. This will create a fire sound and if the duck is hit a sound will play.
We begin by importing the shotgun sound. From the sounds tab, click
Sounds and then Import. Navigate to your Scratch folder and
import the sound file shotgun.
We will now create the script to fire the gun. From the Control
block, drag the when space is pressed command onto the
scripts area and drag the play sound shotgun from the Sound
block. Include the stop script command from the Control block
and create the following script. Press the space bar to check
that the script works.
We now need to check if the duck has been hit. To do this we will use an IF
statement to check if the duck has been hit. If the duck is hit we will broadcast
a message and the duck will fall from the screen.
When we shoot a duck we will make it fall down the screen. We can do this when we receive
the hit message. We then need to put the duck back to its start position so that it appears as a
new flying duck.
Select the command when I receive Hit from the Control block.
To make the duck fall from the screen we need to reduce its y
position unit it is at the bottom of the screen. We do this using a
repeat command. Drag the repeat until command onto the scripts
area.
Place the command change y by 10 inside the repeat loop and change
the value to -10.
Since we will repeat this action until the y position reaches -170 we must include the statement
y position < -170 in the loop condition. Use the y position
statement from the Motion block and the less than (<) operator
from the Operator block to create the statement as shown.
We now need some method of keeping the score of the number of ducks shot. As we shoot a
duck we must increase this score. We begin by setting the score to 0.
Click on the stage and the scripts tab. Click on the Variables block
and the option make variable. A variable is the name given to a
container for a value which can change, much in the same way
we use the word time. The time is a number which changes.
When we ask someone the time we want to know what its
current value is. In this case we will use the word Score to keep
a record of the score. Type in the details as shown and a score will now
appear at the top of the screen.
We need to reset the score to 0 at the start of the game. Use the set
Score to 0 command to create the following script.
Double click the crosshairs sprite to open this script. Drag the
command change Score by 1 from the variables block and place in
the loop to create the script.
Most games have some form of time included so that the game doesnt last for ever. We can
include a timer to stop the game after a set time interval.
Make a new variable called Time and then create the script as
shown. We include a stop all at the end of this script so that the
game stops.
Unfortunately, you may notice that the gun will still fire and
shoot ducks even after the game has finished. Moreover, the
duck appears at the left edge of the screen and doesnt fly a
case of shooting stationary ducks. We need to make sure that
we cannot fire after the time has run out.
Use the Control block to drag the If loop onto the scripts area. We now need to use the greater
than (>) operator to check that the Time is greater than 0. Drag the operator
and the variable Time on to the stage and create the script shown.
Now place this code inside the IF loop and finish off the script as
shown.
We also need to change the start position of the ducks so that they dont enter the screen at
the same place. We will also add some interest to the game by having the ducks enter at
random positions. We will use the pick random command to do this.
Change the scripts for Duck 1 so that it enters at a random height of between 10 and 50.
Now run the game and shoot as many ducks as you can! Experiment with changing the wait
times, adding more ducks, having smaller ducks with higher scores, etc.