Skip to content

活動

Hot potato game

中等級數 | MakeCode, Python | LED 螢幕, 按鈕, 揚聲器 | 變數, 輸入輸出, 迭代, 隨機化

步驟1:製作

它是什麼?

Turn your micro:bit into a ‘hot potato’ by coding it to become a random timer. To play the game, pass the ‘micro:bit potato’ on to the next person before the timer goes off.

How to play it

Press button A and pass the ‘hot potato micro:bit’ around a circle of players. If it makes the sad sound and turns to a cross while you are holding it, then you are out. The last person left is the winner.

運作方式

This project uses button A as in input to start a series of events.

First the ‘timer’ variable is set to a random number between 5 and 15, and a chessboard image appears on the LEDs.

Then the ‘timer’ variable starts counting down, changing by –1, once a second, until it gets to 0.

The 'while loop' helps us shorten the code. While the ‘timer’ variable is above zero, the section of code counting down keeps repeating, but as soon as the ‘timer’ variable reaches zero, the loop stops.

Put the code on a micro:bit and attach a battery pack to turn it into a ‘hot potato micro:bit’.

你需要的東西

  • micro:bit (或,MakeCode simulator)
  • MakeCode 或, Python 編輯器
  • battery pack

步驟2:編碼

1from microbit import *
2import music, random
3
4while True:
5    if button_a.is_pressed():
6        timer = random.randint(5, 15)
7        display.show(Image.CHESSBOARD)
8        while timer > 0:
9            timer -= 1
10            sleep(1000)
11        display.show(Image.NO)
12        music.play(music.WAWAWAWAA, wait=False)
13        audio.play(Sound.SAD)
14        

步驟3:進階

  • Change the duration of the timer to suit different tasks or change from random to a specific length of time.
  • Modify the program so a different icon or your own picture appears when you press button A.
  • Change the sound to a happy/positive sound to indicate when someone has ‘won’ something.
  • Use it in different contexts, for examples, to practise spellings or times tables, or use as a class countdown.