In Scratch, you can tell stories and express your creativity using code.
You can also add the ability to collect points and keep score to make your project feel like a game.
In this video, you will add code to collect points whenever your hero touches an object.
This starter project includes several hidden objects.
Select the object sprite, then drag out a “show” block from the looks menu to make it visible on the stage.
Next, make the object start at the right side of the stage.
From the motion menu, drag out a “set x to” block.
The number shows the sprite’s position on the stage.
Next, program your object to move left.
From the Motion menu, drag out a “change x by” block.
Set the value to a negative number.
Click the block.
The object moves to the left. But to make it move across the stage, you have to keep clicking the block.
To fix this, place a “repeat” block from the Control menu around the “change x by” block.
Change the value in the block to a bigger number to make the object move across the stage.
Then, connect the loop and “set x to” block stacks. Try it out.
To add suspense, make the object appear in a random location.
Add a “go to” block to the block stack.
Make sure “random position” is selected in the dropdown.
Test it again.
The object starts at a random location and moves across the stage each time the block stack is clicked.
To make the object move across the stage automatically, add a forever loop from the Control menu.
Tinker with the values until the project works the way you want.
Next, add a scoreboard, so your hero gets a point every time they touch the object.
To do this, go to the variables menu and make a variable.
A variable stores a number or value for a program to use,
in this case the number of times the hero touches the object.
Name your variable “score.”
A scoreboard appears on the stage!
Then, tell the variable to advance the score by 1.
Add a “change by 1” block from the Variables menu,
and select “score” from the dropdown menu.
To advance the score when your hero touches the object, drag out an “if/then” block from the control menu.
Place it around the “change score by” block.
To make the score go up by one when the hero touches the object,
add a “touching” block from the Sensing menu.
Choose “Heroes” from the dropdown.
Add a “forever” loop around the block stack.
Then, add a “when flag clicked” event to both block stacks.
Click the green flag to test.
The score goes up when the hero touches the object, but it advances many times instead of once.
To fix this, add a “hide” block to tell the object to disappear after it has touched the hero once.
Test your code.
Now the scoreboard changes by 1 when the hero scores.
Awesome! Now, reset the score for each new game.
Add a “set score to 0” block from variables to the top of the block stack.
Change the value to “score” in the dropdown.
Finally, to further customize your project,
change the object, or add a sound from the Sound library.
Once you’ve programmed your game,
try programming your hero to move when arrow keys are pressed in “Move Your Hero.”
Then, come back to this page to check out more videos! You can try any video you like.
Make your hero move up and down when arrow keys are pressed.
Program buildings to move across the screen, so your hero appears to fly.
Set the scene for your hero by adding sound to your project.
Help your hero do great things with this spinning move.
Create a game by programming your hero to collect objects.
Add another character to help your hero.
Use the Paint Editor in Scratch to draw your own hero.
Para hacer esta actividad en español, haz clic aquí.
Sign in to track your progress and earn badges