Hands On: Loading the Background and the Sprites

The first few steps will set up your Scratch project so that you can start programming your game:

Create a new project called, let's say, Zahara1 - by clicking on the button.

Set the background of your stage to 'stars'. To do this, carry out the following steps:

Click on in the Sprites List (bottom right).

Go to the tab (top).

Import the background called stars.

Create a Sprite for Casper:

Click on the button (bottom-right).

Find and load the ghost1 file from the Costumes\Fantasy folder. This will be the Sprite's costume.

Click on the tab (top).

Import the Slurp file from the Sounds\Human folder. This is the sound that Casper will make when he collects the Wizard Hat.

In a similar fashion, create a sprite for the Chandrin (using cloak-1a from the Costumes\Fantasy folder, and Rattle from the Sounds\Effects folder).

In a similar fashion, create a sprite for the Wizard Hat (using wizardhat from the Costumes\Things folder). The Wizard Hat does not need a Sound, so you do not need to import a Sound file for it.

At the end of this exercise, your stage should look something like this:

Animating the Chandrin

Let's program the behavior of the Chandrin next. Here's what we want the Chadrin to do:

Move around randomly in the game world, after starting out at a predetermined location.

Destroy Casper if he runs into him.

Hands On: Chandrin Motion

Let's focus on item #1 above. Think about what it will take to make a Chandrin move about Randomly. Look at the blocks in the , , and categories.

Here's one way that you can do this:

Does this make sense? Did you think of something similar?

Run your script by clicking on . Does the Chandrin do what you wanted it to do?

Taking Stock: Chandrin Motion

The little script to animate the Chadrin involves a number of programming concepts:

Event Handling: this allows you to specify what you want your Sprite to do (that's the Handling part) when the green arrow is clicked (that's the Event). You do this by using the block.

Flow Control: a script/program normally just runs sequentially through its instructions (each block in Scratch is an instruction). In the Chandrin script, we surround the core logic within a loop, thus controlling the flow of the script.

Sequential Flow: this is the set of sequential steps (in blue): that make up the core of your script.

Look a little closer at the shapes of the blocks involved in this script. You'll see some interesting things:

Event Handling blocks have rounded or hat tops. These blocks are meant to be right at the beginning of scripts. They get your script going in response to an external event - like the user clicking the green flag.

Stackable blocks have notches on the top and/or bumps on the bottom. You snap these blocks into stacks that specify the sequence of actions that your sprite needs to carry out.

Exercise: Try to find a stackable block which does not have a bump on the bottom. Can you figure out why this block does not have a bottom bump?

Some stackable blocks have a hole in them that you can fill with another block or number. The shape of the hole determines the type of block that you can put into the hole.

Hands On: Chandrin Collision Detection

The other aspect of the Chandrin's behavior is to determine if it has banged into Casper, and take appropriate action if it has.

Here's the script that does this:

Make sense?

Taking Stock: Chandrin Collision Detection

We see a few new concepts here:

You can have multiple independent scripts attached to a Sprite. For the Chadrin Sprite, the collision detection script coexists with the random motion script that we looked at earlier. This is, in fact, a good way to program a Sprite's behavior: you have different scripts that define different aspects of the Sprite's behavior, and the Sprite's actual behavior is an aggregation of the behavior of its different scripts.

If a Sprite needs to communicate with another Sprite, it broadcasts a message. It is then the responsibility of the other Sprite to respond appropriately to this message.

A Sprite cannot really make another Sprite disappear - because each Sprite is responsible for its own appearance. So to make another Sprite Disappear, a Sprite has to send the other Sprite an agreed upon message.

Hands On: The other sprites

Casper

Here's what we want Casper to do:

Start out at a predetermined location in the world.

Move around in the game world in response to arrow key presses.

Collect the Wizard Hat if he runs into it

Disappear if he's been hit by the Chandrin.

Wizard Hat

Here's what we want the Wizard Hat to do:

Start out at a predetermined location in the world

Disappear when it has been collected by Casper

Stage

We need some way to stop the game when either Casper has been destroyed by the Chandrin or the Hat has been collected by Casper. It seems right to give the responsibilty for this to the Stage. The other Sprites can trigger the ending of the game as required by broadcasting a message, and the Stage can respond to this message by stopping all scripts.

Take a stab at implementing this functionality before you move on to the next page: