Friday, February 14, 2014

Escape the Room - Working with Data

Happy Valentine's Day!!!

The Key to Success

Image source:

Today in class there was lots of scripting! In the end we were able to take a key or the object used to escape our room, and use it to check to see if we had in fact found four items.

In Scratch we worked with five different sprites and a variable called inventory that we created using Data blocks:

  1. Sprite hidden in our room (key)
  2. Sprite covering the hidden item (water color box)
  3. Door sprite 
  4. Inventory Box
  5. Inventory sprite that goes inside the inventory box (we duplicated this sprite from the hidden key sprite)
We then created a variable called inventory to keep track of what the player of our game clicks on.

Step One

Create some sprites (I used Pixlr to edit found images):
Image sources*:
Door -
Water Color Box -
Key -
Crate -

*These images were solely used for educational purposes and all rights belong to the originating sources. 

Step Two

Door Water Color Box Crate (Inventory)
The (x, y) coordinates are found by looking at the top right of the Scratch UI (User Interface). Its slightly opaque:

Step Three

The scripting for the hidden key is similar to those as above however we will add a Data block that we call inventory to keep track of the items the player discovers in our game.

First lets scripts for the hidden key

First we would like to hide this sprite behind the paint box so we send the key back one layer.

Then when we click this sprite we wish to send a message to our inv_key that it is time to show up in the inventory box.

Later we will duplicate this sprite and rename the new sprite to inv_key (more on that later...)

Now we need to create a variable which will essentially be a counter. Each time the player clicks on a hidden object we will add a value of one to our counter.

In Pseudo-code:
If I click on a hidden object then add 1 to the variable.

Let's create the variable:
Click on Data in the blocks palette
Then press the Make a Variable button.

This dialog box will appear and you will simply type in inventory and then press enter or the OK button.
(There is no need to change any other values.)

This action gives us new blocks to play with! Your hidden key script should now look like this:

We will initially set inventory to zero.

Then when the player clicks on the sprite we will change or add one to inventory

Now let's create the inv_key

Simply right click on the hidden key and select duplicate

Notice the blue circle with "i" in it. We will be selecting this icon on our duplicate key in a few seconds.

Now to rename duplicate key2 simply click on the sprite and then click on the "i". Rename this to inv_key. While we are here make sure to click on the "can drag in player" check box.

Otherwise we will not be able to drag the key around the room or touch the door with it.

Step Four

First lets change some of the scripts that were copied over from hidden key. My (x, y) position will change and I wish for this sprite to be in front of the crate (inventory box). 

I also only want this sprite to show when I receive the broadcast found key

Now let's use our variable! When we start the game or if when I receive found key I wish to run this script:
I chose when I start game...

So If the player touches the door with the inv_key it will call up a script that will check the value of the variable inventory.

The inv_key will then glide magically back down to its original position in the crate

Step Five

Let's move away from the Sprites and now focus on the Stage.
We need to develop a special script that uses our Operators blocks. 
Just to the right of our scripts we should select the stage icon in the Scratch UI. 

When the inv_key touches the door it creates a broadcast check.

If the value of inventory is equal to four then the player wins!
All of the sprites then hide when they receive win and then stage switches to the win backdrop!

However, if the first condition is not met then that means that inventory is not equal to four and the player has not won, or in this case they lose

Final Step

Now let's go back to our inv_key

Let's add the final scripts for when we win, and when we lose.

The first one is simple.

The lose script is a little bit more involved.

Simply pull out these five blocks and add them to the when I receive (lose) block.

First insert the second join block into the first join block that says world.

Now type 4 in the subtraction Operator and then place the inventory block into the other open space.
Since we have four items we can simply subtract what is in inventory to tell the player the total amount of items we still need to find.

Now insert this block into the second hello space of our join blocks.

Finally insert this whole mess into the say (hello) for (2) secs block.

Finally change the value in "hello" to: "You need to find " (notice the space)
and change the value of "world" to: " more item(s)" (notice the space)

You can put whatever you wish in there, but make it clear to the player how many items they have left to find.

Want to check it out?

Well that was a long post!
Hope you enjoy your President's Day!