这个游戏需要两个或更多的microbit才能进行,还需要蜂鸣器、电源等小配件(社区的电源板非常适合)。
游戏的目的是通过无线方式和有限的提示寻找隐藏的宝藏(microbit)。
安装
为每个microbit的引脚3上安装扬声器。我在给我的peizos焊接,然后用胶带以防止短路。通过压力连接到microbit,用小橡皮筋把电池盒固定。
编程
我使用uflash,但是也可以使用其它软件,如浏览器、mu等。将main.py下载到microbit。
代码: 全选
import microbit
import music
import radio
def siren(steps=16, up_only=False):
for freq in range(880, 1760, steps):
music.pitch(freq, 6)
if up_only: return
for freq in range(1760, 880, -steps):
music.pitch(freq, 6)
class Debouncer:
def __init__(self, cooldown_ms):
self.cooldown = cooldown_ms
self.last_bounced = -cooldown_ms
def debounce(self):
return (microbit.running_time() - self.last_bounced) < self.cooldown
def bounce(self):
self.last_bounced = microbit.running_time()
def attempt(self):
if self.debounce():
return False
else:
self.bounce()
return True
class PingPonger:
def __init__(self,
pong_limit=3,
cooldown=500,
pong_function=siren):
self.pong_limit = pong_limit
self.pong_function = pong_function
self.debouncer = Debouncer(cooldown_ms=cooldown)
self.recharge()
def recharge(self):
self.pongs = [1] * self.pong_limit
def ping(self):
"""
Sends a ping, which any devices with pongs left will respond to.
"""
radio.send_bytes(b'ping')
def pong_on_ping(self):
msg = radio.receive_bytes()
if msg is None: return
if msg == b'ping':
self.pong()
else:
raise ValueError(msg)
def pong(self):
"""
If any pongs remain, spend one.
"""
if self.debouncer.attempt() and self.pongs:
self.pongs.pop() # Spend one
self.pong_function()
#microbit.display.scroll("q")
def list_map_to_img(lm):
return microbit.Image(':'.join(lm))
digits = list(map(list_map_to_img, [
[
'09900',
'90090',
'90090',
'90090',
'09900',
],
[
'00900',
'09900',
'00900',
'00900',
'09990',
],
[
'09900',
'90090',
'00900',
'09000',
'99990',
],
[
'09900',
'90090',
'00900',
'90090',
'09900',
],
[
'00090',
'00990',
'09090',
'99999',
'00090',
],
[
'99990',
'90000',
'09900',
'00090',
'99900',
],
]))
radio.on()
me_pp = PingPonger(
pong_limit=5,
cooldown=2000,
pong_function=lambda:siren(steps=32, up_only=True))
me_pp.pong_function()
while True:
me_pp.pong_on_ping()
if microbit.button_a.was_pressed():
#microbit.display.scroll("A")
me_pp.ping()
if microbit.button_b.was_pressed():
#microbit.display.scroll("B")
me_pp.recharge()
microbit.display.show(digits[len(me_pp.pongs)] * (0.5 if (me_pp.debouncer.debounce()) else 1))
任何设备都可以按“A”键发送“Macro”提示。
当一个设备用尽了它的“Polo”响应时,按下它的“B”按钮就会刷新它。
根据main.py文件的参数,设备将有一定数量的“Polo”对每个“Macro”的另一个microbit,默认参数值是5。“Polo”的声音是一个简单的警报器,它可以被配置为更快或更短的。
最后,有一个“反跳”参数,有助于防止过快消耗完“Polo”。
最后用它来进行寻宝,或者蒙住眼睛,或者别的什么游戏。
改进
改进是非常受欢迎的!如果你能编程(或愿意学习microbit编程),请分享代码。如果你不能或不愿意做编程,我仍然愿意接受建议。