Tag Archives: timer

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.

create a timer in Scratch

Create a timer in Scratch to set a time limit for a game.

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:

change my variable by 1 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.
repeat until 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.

Code used to create a timer in Scratch.

Code used to create a timer in Scratch.

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

  1. Open the saved game project in Scratch.
  2. Select the sprite that you will use to track the time (it might be the player).
  3. From the Variables palette, click Make a Variable.
  4. Type timer as the variable name. Click OK.
  5. Create a timer variable.

    Create a timer variable.

  6. Select the timer variable to display it.
  7. 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.
  8. Set the time limit using an Operator block.

    Set the time limit using an Operator block.

    Tip to Setting the Operator Coding Block:

    • View the Operator palette.
    • Drag __=50 into the if-then block if-then.
    • Edit the number of seconds to the time limit. For example, 15.
    • View the Variable palette. Add the timer. timer

    Test the Timer

  9. 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

  10. Play the game again. Does the timer start at zero? NO, it doesn’t.
  11. Select the sprite that tracks the time.
  12. From the Variables palette, add set my variable to 0 to the start of a script. Click the Variable arrow and select timer.
  13. Add a coding block to reset the timer.

    Add a coding block to reset the 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?

  14. 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.


TechnoCode lessons