Skip to content

活动

Hot and cold game

中级 | MakeCode, Python | LED显示器, 按钮, 无线电 | 数据类型, 通信

第 1 步:制作

它是什么?

Make this fun two-player game using the BBC micro:bit’s radio feature.

学习目标

By making this project you will learn about networks, how information is sent between electronic devices by radio, and how computers store information in different data types such as how words are stored in strings.

工作原理

  • This is a game for two people. One person hides an object and gives the seeker clues: ‘hot’ when they are close to the object, ‘colder’ when they are moving further away, and ‘warmer’ when they are getting closer.
  • Usually, this game is played by shouting the words ‘hot’, ‘colder’ and ‘warmer’ but in this version of the game the words are sent by radio from one micro:bit to another and appear on the LED display.
  • There are two programs, one for the hider and one for the seeker.
  • First, each program sets the radio group to 47. Groups are like channels, so any micro:bit using the same group will get the message. 你可以在0-255之间选择任何你喜欢的组号。
  • The hider presses button A to send the message ‘warmer’, presses button B to send the message ‘colder’ and presses buttons A and B together to send the message ‘hot’.
  • The micro:bit can send messages as numbers or text. We’re using text messages in this project, so make sure you use radio blocks that refer to strings rather than numbers or values.
  • In computing, a string is a sequence of characters that can contain letters, numbers, symbols, and spaces.
  • When the seeker's micro:bit receives a radio message, it shows the string received on its LED display. The strings are also displayed on the hider’s micro:bit.

所需材料

  • 2个micro:bit主板
  • MakeCode编辑器
  • battery packs (recommended)

Step 2: code it

Hider code

1# Imports go at the top
2from microbit import *
3import radio
4radio.config(group=47)
5radio.on()
6
7while True:
8    if button_a.is_pressed() and button_b.is_pressed():
9            radio.send('HOT!')
10            display.scroll('HOT!')
11    elif button_a.is_pressed():
12        radio.send('warmer')
13        display.scroll('warmer')
14    elif button_b.is_pressed():
15        radio.send('colder')
16        display.scroll('colder')
17    sleep(100)

Seeker code

1# Imports go at the top
2from microbit import *
3import radio
4radio.config(group=47)
5radio.on()
6
7while True:
8    message = radio.receive()
9    if message:
10        display.scroll(message)
11    sleep(100)

Step 3: improve it

  1. Edit the program so that different words appear instead of 'hot', 'colder' and 'warmer'. For example, you could use the words for 'hot', 'colder' and 'warmer' in a foreign language you are learning.
  2. Add sound effects to the program.
  3. Use this game with a timer or step counter program to see how quickly you found the object.