Scratch Advanced – Week 10 – Christmas Game – Final Episode

What are we going to learn this week.

1. Global Variables – A  Variable that all Sprites can see.

2. More requirements

3. Ninja Belts

And that is about it for new things. We are primarily going to be concerned with getting the Present working in the Christmas Game and the final display of what you have caught.

Now, we will start in reverse and look at the “more requirements” part first. Remember we had a requirement for the game that a present should fall and if we catch it, it should be put under the tree. Well we can write that out a little better as another set of Requirements, so here goes:

More Requirements:

The present should:

1. There should be Ten presents.

2. The presents should fall in the two minutes at random times.

3. Each present should fall from the Sleigh to the ground.

4. If the present touches the fire, then it is caught, and should be moved under the tree, anywhere under the tree, but not all on top of each other.

5. Each present should be a different size and colour.

6. If a present is caught we need to keep track of how many we have caught.

So, what we can do is create one present that meets all of the above requirements and then copy it.

For number 6, it looks like we need a Global Variable so all the Present Sprites can update it. So lets make that..

And while we are on the subject of Global Variables, if the Present Sprite needs to drop from the Sleigh, it needs to know where it is, so the Sleigh needs to update a Global Variable with it’s X value all the time.

If you select the Stage, when you create a variable it is automatically a Global Variable. You don’t get the option to create it “for this Sprite only”

We need one for the X Position of the Sleigh and one to record the number of Presents.  Don’t forget to update the Sleigh Sprite so that it updates the X Position variable. Something like this:

SleighCode

Notice that I don’t have the Green Flag to Start the Game, I have used a GameStart broadcast, this is Broadcast from the Stage where I have all my initialisation code as well.

While we are creating the Global Variables, we can also create the two lists that we need to store the names and descriptions, so two lists as well.

This is what I have to set the lists at the begining of the Game:

StageCode

Now back to the Present and the requirements.

This is the code that I have that meets the requirements… Random Colour, Random time delay, after the delay, go the X Position of the Sleigh…

Present01

Now we have to sort out how to recognise if the present has been caught by the fireplace. I picked one of the colours in the fire, and used the if touching code to find out if it has been caught. If it is caught, then we move it to the Tree.

We don’t just move the Present to the same point though otherwise they would all land on top of each other. I pick a random point under the Tree. Like this:

 

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s