How to Create a Timer in Scratch, Game Design
Want to know how to create a timer in Scratch to set a time limit for a game? Follow the instructions to create a counter that tracks each second that passes. When a specific time is reached, the timer stops counting and the game ends.
About the Scripts and Your Game
The directions listed below explain how to add a timer to an existing Scratch project. You may need to adjust the scripts slightly to suit your game.
About the Timer Variable
The script requires the use of a variable. A variable is a factor that can change. Create a timer variable. It will be used to hold the number of seconds.
Scratch Coding Blocks Used to Create a Timer
Scratch has several coding blocks that you will use to build the script:
|Increase the (timer) variable by a specified amount.|
|Reset the (timer) variable to zero when a new game begins.|
|Display the timer variable to see the counter.|
|Do an action until the something specific happens (time limit is reached).|
|An operator that tests if the first value (seconds that have passed) equals the second value (time limit).|
About the Timer Script
Before making the variable and building the script it is important to understand how the timer script works. Study the script below.
Does the timer equal 15? If no, wait 1 second and then increase the timer by 1 second. Repeat until the timer equals 15.
Game Design with a Timer Video
Watch the video to understand how a timer can be used to set a time limit for a game. If this is a coding project that you would like to make, TechnoCode has a game design coding unit. The instructions explain how to build scripts to have a player touch targets to earn points. The lessons also include a planning sheet, game consultant checklist, coding journal log, Scratch quiz, and marking sheet.
How to Create a Timer in Scratch
Create a Timer Variable
- Open the saved game project in Scratch.
- Select the sprite that you will use to track the time (it might be the player).
- From the Variables palette, click Make a Variable.
- Type timer as the variable name. Click OK.
- Select the timer variable to display it.
Build the Script using the Timer Variable
- Use your skills to build this script:
How do you want the script to start? Notice that in the sample script below, it begins with when I receive play game. This is a broadcasted message that you may have already created. If the broadcast message was not made, you can use the when Go clicked Hat block. To learn about broadcasting read the post, How to Broadcast a Message in Scratch.
Tip to Setting the Operator Coding Block:
- View the Operator palette.
- Drag __=50 into the if-then block .
- Edit the number of seconds to the time limit. For example, 15.
- View the Variable palette. Add the timer.
Test the Timer
- Play the game. When done, click Stop.
- Does the timer increase when one second passes?
- Does the timer stop when it reaches the time limit?
Set the Timer to Zero When a New Game Begins
- Play the game again. Does the timer start at zero? NO, it doesn’t.
- Select the sprite that tracks the time.
- From the Variables palette, add set my variable to 0 to the start of a script. Click the Variable arrow and select timer.
WHERE SHOULD THE CODING BLOCK GO? Notice where the set timer to 0 block is located in the script above. It was placed after the when Go clicked Hat block of an existing script. Look at your scripts. Where should it be placed in your game?
- Play the game again. The timer should start at 0.
Programming Lessons – Coding Units
Interested in Scratch lessons? Learn more about TechnoCode published by TechnoKids Inc. TechnoCode is a technology project that includes a teacher guide, workbook, and resource files. Use the instructional materials to teach a coding unit to students in Grades 6-9.