7 seconds game

{Introduction }

The goal of this game is press a button after exactly 7 seconds!

A micro:bit looking at a 7 second stopwatch

This game is inspired from the flipping panckakes game.

{Step 1}

The player starts the timer by pressing button A. We’ll run the code run code when ||input:button A is pressed||.

input.onButtonPressed(Button.A, function () {

})

{Step 2}

We need to remember the time when the button was pressed so that we can compute the elapsed time later on. Add code to store the ||input:running time|| in a ||variables:start|| variable.

let start = 0
input.onButtonPressed(Button.A, function () {
    // @highlight
    start = input.runningTime()
})

{Step 3}

Show something on the screen so that the user knows that the timer has started…

let start = 0
input.onButtonPressed(Button.A, function () {
    start = input.runningTime()
    // @highlight
    basic.showIcon(IconNames.Chessboard)
})

{Step 4}

The player stops the timer by pressing button B. Add the code to run code when ||input:button B is pressed||.

input.onButtonPressed(Button.B, function () {

})

{Step 5}

Compute the elapsed time as ||input:running time|| ||math:minus|| ||variables:start|| and store it into a new variable ||variables:elapsed||.

let start = 0
let elapsed = 0
input.onButtonPressed(Button.B, function () {
    // @highlight
    elapsed = input.runningTime() - start
})

{Step 6}

Compute the ||variables:score|| of the game as the ||math:absolute value|| of the ||math:difference|| of ||variables:elapsed|| time from 7 seconds, which is 7000 milliseconds.

let start = 0
let elapsed = 0
let score = 0
input.onButtonPressed(Button.B, function () {
    elapsed = input.runningTime() - start
    // @highlight
    score = Math.abs(elapsed - 7000)
})

{Step 7}

Display the score on the screen and your game is ready!

let start = 0
let elapsed = 0
let score = 0
input.onButtonPressed(Button.B, function () {
    elapsed = input.runningTime() - start
    score = Math.abs(elapsed - 7000)
    // @highlight
    basic.showNumber(score)
})
input.onButtonPressed(Button.A, function () {})