0% found this document useful (0 votes)
843 views11 pages

Scratch Intermediate - Duck Shooter Game

The document provides instructions for creating a duck shooting game in Scratch. It includes steps to: 1. Import background and sprite assets, create a crosshair sprite, and position game elements on the stage. 2. Add scripts to make the duck sprite fly across the screen by switching costumes and changing its x position over time. 3. Create scripts for the crosshair sprite to move with arrow keys and shoot when the space bar is pressed, checking if it touches the duck. 4. Add scripts so that when the duck is shot it falls off the screen and returns to its starting position, and the score is increased. 5. Include a timer variable to limit the length of the

Uploaded by

kerrywensley499
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
843 views11 pages

Scratch Intermediate - Duck Shooter Game

The document provides instructions for creating a duck shooting game in Scratch. It includes steps to: 1. Import background and sprite assets, create a crosshair sprite, and position game elements on the stage. 2. Add scripts to make the duck sprite fly across the screen by switching costumes and changing its x position over time. 3. Create scripts for the crosshair sprite to move with arrow keys and shoot when the space bar is pressed, checking if it touches the duck. 4. Add scripts so that when the duck is shot it falls off the screen and returns to its starting position, and the score is increased. 5. Include a timer variable to limit the length of the

Uploaded by

kerrywensley499
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Duck Shoot

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

Setting the Stage


Double click on the stage to select it and then click the Backgrounds tab. Click Import and,
from the outdoors folder, select wooden-house.

Delete Background1 so that you only have one


background.

Creating the Cross Hairs


We will now create sprite to act as the sight on a gun. Click
on the Paint new sprite button. Use the circle tool and the
line tool to create a small circle with a vertical and a
horizontal line.

Right click on the Cat sprite and delete it.

We will now import the Duck sprite. Navigate to your


Scratch folder and import Duck 1.

Unfortunately, there is white space around the duck.


Click the Edit button and choose the Fill tool.
Select the Transparent colour and fill all the white
space with transparent.

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.

We now need to change the costume back to costume Ducka. Drag


switch to costume Duckb to the scripts area, but use the combo box
to change the command to switch to costume Ducka. If you join the
commands, and click the green flag you will see the costume change.
However the change only occurs once, we need to repeat this action.

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.

Use the Motion block to create the following script.

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 will begin by using the change x by 10 command from the Motion


block and the wait 1 secs from the Control block. Change the wait time to
0.1 secs.

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.

We will begin by setting the start position of the cross hairs.


This will be the centre of the screen with the x position at 0 and
the y position at 0. Create the script as shown.

We will use the up arrow to mode the cross hairs up 10


places. Begin by clicking on the cross hair sprite. From
the Control block, drag the when space key pressed
command and use the combo box to choose up arrow as
shown.

From the Motion block, choose change y by 10 and from the


control block, choose stop script. Connect these commands
together to produce the complete script as shown.

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.

We begin by dragging the if loop from the Control block. We


will use this loop if the duck has been hit. From the Sensing
block drag the touching Duck 1? command onto the scripts area
and place it in the If control.

Drag the broadcast command from the Control block and


click the combo box. Click on the word new and type the
word hit. This message will be broadcast when the cross
hairs touch the duck. It will not appear as a message on the screen.

Now complete the script as shown.


Falling Ducks

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.

Finally we need to return the duck to its start position. Remember


this was the y position 0 and the x position -240. Create these
two commands from the Motion block.

Finally put all the commands together to create the following


script.
Keeping Score

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.

We need to increase the score each time a duck is hit. We need to


change the script which runs when the duck is hit. This script is
shown. We now need to include a command to increase the score
when the IF command is run.

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.

Now run the game and shoot the duck!


Timing, Timing, Timing!

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.

We will begin by creating a new variable called Time. Double click


on the Stage to open the scripts.

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.

Try running the game to see how it works.

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.

Double click on the cross hairs to open the script. We need to


change the script so that the gun will only fire if the Time
variable is greater than (>) 0.

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.

Now play the game!


More Ducks

To make the game more interesting we will now introduce a


second duck. Right click on Duck1 and select duplicate. This
will create a second duck with all the scripts. Change the
name of this Duck to Duck2 and the costume names to
Duck2a and Duck2b. You will need to change the scripts for
Duck2 to reflect the changes of name.

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.

From the Operators block choose the command pick


random 1 to 10. Place the command into the set y position
and change this to pick random -10 to -50. Place the
command into the hit script also.

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.

You might also like