a. Zahara: Basic Version

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:

  1. Create a new project called, let's say, Zahara1 - by clicking on the newproject.gif button.
  2. Set the background of your stage to 'stars'. To do this, carry out the following steps:
    1. Click on stage.gif in the Sprites List (bottom right).
    2. Go to the stage-backgrounds.gif tab (top).
    3. Import the background called stars.
  3. Create a Sprite for Casper:
    1. Click on the new-sprite.gif button (bottom-right).
    2. Find and load the ghost1 file from the Costumes\Fantasy folder. This will be the Sprite's costume.
    3. Click on the sounds.gif tab (top).
    4. Import the Slurp file from the Sounds\Human folder. This is the sound that Casper will make when he collects the Wizard Hat.
  4. 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).
  5. 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:

  1. Move around randomly in the game world, after starting out at a predetermined location.
  2. 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 control.gif, motion.gif, and numbers.gif 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 green-arrow.gif. 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 when-flag-clicked.gif 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 forever.gif loop, thus controlling the flow of the script.
  • Sequential Flow: this is the set of sequential steps (in blue): sequential.gif 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


Here's what we want Casper to do:

  1. Start out at a predetermined location in the world.
  2. Move around in the game world in response to arrow key presses.
  3. Collect the Wizard Hat if he runs into it
  4. Disappear if he's been hit by the Chandrin.

Wizard Hat

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

  1. Start out at a predetermined location in the world
  2. Disappear when it has been collected by Casper


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:

left.gif top.gif right.gif

Local Navigation Map:

Current Page: a. Zahara: Basic Version

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License