Story 1: Beep when you touchforever and if
The same in Python
# Story 1: Beep when you touch. The wire is on P1, the loop on GND: a touch closes the circuit.
def on_forever():
if input.pin_is_pressed(TouchPin.P1):
basic.show_icon(IconNames.NO)
music.play(music.tone_playable(131, music.beat(BeatFraction.HALF)),
music.PlaybackMode.UNTIL_DONE)
else:
basic.clear_screen()
basic.forever(on_forever)
forever asks again and again: is pin P1 pressed? When the loop touches the wire, the circuit from P1 to
GND is closed and the answer is yes: a cross and a low beep. Else the screen stays dark.
Your turn: touch P1 and GND with two fingers at once. Does it beep a little? Why?
Real worldFactories use the same idea: a closed circuit tells a machine that a door is shut or a part is in place, and a touch sensor stops it in time.
Story 2: Count the touchesa variable
The same in Python
# Story 2: Count the touches. Each touch adds one; button A starts again at 0.
touches = 0
def on_button_pressed_a():
global touches
touches = 0
basic.show_number(touches)
input.on_button_pressed(Button.A, on_button_pressed_a)
basic.show_number(touches)
def on_forever():
global touches
if input.pin_is_pressed(TouchPin.P1):
touches += 1
music.play(music.tone_playable(131, music.beat(BeatFraction.QUARTER)),
music.PlaybackMode.UNTIL_DONE)
basic.show_number(touches)
basic.pause(500)
basic.forever(on_forever)
The variable touches remembers how many times you touched. The pause of 500 ms makes one touch count
once. Button A starts again at 0.
Your turn: play three times. What is your best number?
Story 3: Three livesif inside if
The same in Python
# Story 3: Three lives. Each touch costs a life; at 0 the game is over. Button A gives three new lives.
lives = 3
def on_button_pressed_a():
global lives
lives = 3
basic.show_number(lives)
input.on_button_pressed(Button.A, on_button_pressed_a)
basic.show_number(lives)
def on_forever():
global lives
if lives > 0 and input.pin_is_pressed(TouchPin.P1):
lives += -1
if lives == 0:
basic.show_icon(IconNames.SKULL)
music.play(music.builtin_playable_sound_effect(soundExpression.sad),
music.PlaybackMode.UNTIL_DONE)
else:
music.play(music.tone_playable(131, music.beat(BeatFraction.QUARTER)),
music.PlaybackMode.UNTIL_DONE)
basic.show_number(lives)
basic.pause(500)
basic.forever(on_forever)
You start with 3 lives. Each touch takes one. At 0 the skull comes and a sad sound plays, and touches no longer
count. Button A gives three new lives.
Your turn: make it 5 lives for younger players.
Story 4: Beat the clocktime and a score
The same in Python
# Story 4: Beat the clock. Button A starts the clock, the finish pad on P2 stops it. Every touch adds 5 seconds.
touches = 0
start = 0
playing = False
seconds = 0
score = 0
def on_button_pressed_a():
global touches, start, playing
touches = 0
start = input.running_time()
playing = True
basic.show_arrow(ArrowNames.EAST)
input.on_button_pressed(Button.A, on_button_pressed_a)
def on_forever():
global touches, playing, seconds, score
if playing and input.pin_is_pressed(TouchPin.P1):
touches += 1
music.play(music.tone_playable(131, music.beat(BeatFraction.QUARTER)),
music.PlaybackMode.UNTIL_DONE)
basic.pause(500)
if playing and input.pin_is_pressed(TouchPin.P2):
playing = False
seconds = Math.idiv(input.running_time() - start, 1000)
score = seconds + touches * 5
music.play(music.builtin_playable_sound_effect(soundExpression.happy),
music.PlaybackMode.IN_BACKGROUND)
basic.show_number(score)
basic.forever(on_forever)
Press A: the clock starts and playing becomes true. Touch the foil at the end of the wire (on P2) with the loop: the
clock stops. Your score is the seconds plus 5 for every touch. Lower is better.
Your turn: is it better to go fast or to go carefully? Try both.