The aim of this lesson is to introduce the next level of skills. Make sure that you have a solid understanding of the skills introduced in the Scratch Basic Skills section. These skills are more complicated and build upon some of the previous skills. Remember, the purpose of Scratch is to become familiar with programming concepts. It’s highly recommended to make sure you understand the contents of these lessons.
The Game Development Mini-Project requires you to include each of these skills in addition to the ones covered in the Scratch Basic Skills lesson. Make sure to become familiar with each of these areas and the associated code blocks!
Variables are like containers/placeholders for values. Variables are a foundational programming concept that enables the creation of advanced scripts. Scratch is great for learning to create and manipulate variables.
You’ve likely encountered variables before. Here are some examples:
x = 5
name = "Sarah"
gameLoading = true
Code language: JavaScript (javascript)
Variables are found in the Variables category in Scratch. Clicking on Variables brings up the variables blocks:
Next, click on the Make a Variable button to create your variable.
After clicking that button, you’ll need to name your variable. For now, leave the For all sprites option clicked. This creates a global variable that you can use anywhere in your project. The alternative is a local variable. The difference between these options is outside of the scope of this course.
Your variable is now available for use! This image shows a variable named “health” that can now be used in other scripts. This name matches what you provide in the previous step.
Here’s an example of using variables in a script:
Broadcasts send messages across all sprites and backdrops in your project. This is an effective way to communicate across sprites and create sequences of events. The broadcast is an Event block that pairs with a Listener block. The sprite sending the message can be completely independent of the sprite receiving the message.
Broadcasting is useful if a script needs to be activated after the start of the program without a user prompt such as a keypress or mouse click.
Listeners listen for messages sent from broadcasts. Broadcasts need to be paired with one or more Listeners. As soon as the listener receives the message from the broadcast, the connected script runs.
Multiple sprites can have the same Listener and independently trigger scripts. This is an effective way to trigger multiple actions across several sprites.
Let’s take a look at example scripts. This first script is the broadcast:
This script is the listener:
When the space key is pressed, the broadcast sends the message “vanish!” across the project. The listener then reacts to this message, triggering an action.
Collisions are an intersection of two or more objects. Collision detection is the name for the game mechanic that detects intersecting objects. This is often a part of a game’s physics engine.
Collision detection must be contained inside a forever loop. The loop always runs and asks “is this sprite touching ___?” There are several sensor options for collision detection. Two of the most common are color collisions and sprite collisions.
Color collisions are collisions detected using color. Each sprite can be scripted to trigger an event if it’s touching a certain color. Here’s an example script:
The collision detection is created by using a conditional statement combined with the “touching color [ ]” which is Boolean (true or false) value.
This script returns a true or false value. It returns true if the sprite is touching the color, and false if not. If it returns true then a collision is detected and the script is triggered.
Sprite collisions are collisions detected using a sprite. Each sprite can be scripted to trigger an event if it’s touching another sprite. Here’s an example script:
The collision detection is created by using a conditional statement combined with the “touching sprite [ ]” which is Boolean (true or false) value. Select the name of the sprite you want to detect from the dropdown menu. In the above example, the sprite is named “Crystal.”
This script returns a true or false value. It returns true if the sprite is touching the selected sprite, and false if not. If it returns true then a collision is detected and the script is triggered.
Create some examples yourself. If you want to see the Scratch demo project where the examples are from, click this link: Example: Beginner Collisions.
If you need more detailed help with each of these sections visit Rhea’s Scratch Demos to see example scripts.