needmoreeasy EN 한국어
Learn

19 — Game: how fast are you?

★★★☆☆ (3/5)a game, and timing
Prerequisites
15 — Time
You will end up with
a program that measures how fast you react

The stopwatch from 15 — Time is enough for a game. Start the clock the moment the signal appears, and read it when the key comes down.

Steps

  1. Starting the clock and reading it is the whole idea:

    start the timer
    ask answer Press enter
    show elapsed
    
    run it →

    elapsed is the seconds since the clock was started.

  2. Give the signal late. Asking straight away lets someone press early, so pause first:

    show Get ready
    wait 3 seconds
    show Press enter now
    
    run it →
  3. Keeping the reading in a name lets you use it more than once:

    start the timer
    ask answer Enter
    set taken to elapsed
    show taken
    
    run it →
  4. Say how fast that was:

    set taken to 0.4
    if taken is less than 1
        show very fast
    else if taken is less than 2
        show not bad
    else
        show a little slow
    end
    
    run it →
  5. All of it together:

    show A reaction test
    show Get ready
    wait 3 seconds
    show Press enter now
    start the timer
    ask answer Enter
    set taken to elapsed
    show taken
    if taken is less than 1
        show very fast
    else if taken is less than 2
        show not bad
    else
        show a little slow
    end
    
    run it →

    The clock gives the time up to the moment elapsed is read, so where you start it is exactly where the measuring begins.

Try it yourself

Measure three times and show the best of them. Put each reading in a list and ask for the smallest of times — the list is from 05 — Set and the smallest is from 17 — Word guess.

What you learned

  • start the timer starts the clock and elapsed gives the seconds since.
  • set taken to elapsed keeps a reading so it can be used again.
  • A wait before the signal is what stops an early press.
  • The reading is an ordinary number, so a condition takes it as it is.

This page on GitHub