这是我的 microbit 在 python 中的代码
所以我想知道如何将 target_x 和 target_y 从 microbit 1 发送到第二个 microbit ?
微比特1:
radio.on()
target_x = random.randint(0,4)
target_y = random.randint(0,4)
if button_a.was.pressed():
radio.send()
微比特2:
radio.on()
order = radio.receive()
microbit.display.set_pixel(target_x,target_y,7)
所以我想知道如何将 target_x 和 target_y 从 microbit 1 发送到第二个 microbit ?
感谢您的回答
最佳答案
我使用两个微位测试了下面的代码。我在接收器上添加了一个“异常(exception),尝试”子句,以防消息损坏。为了建立可靠的无线接口(interface),应该实现更多的错误检查,但这回答了问题。
radio_send_randints.py
''' transmit random x and y on button push '''
import random
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
if button_a.was_pressed():
target_x = random.randint(0,4)
target_y = random.randint(4)
message = "{},{}".format(target_x, target_y)
radio.send(message)
sleep(100)
radio_receive_randints.py
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
incoming = radio.receive()
if incoming:
try:
target_x, target_y = incoming.split(',')
except:
continue
display.set_pixel(int(target_x), int(target_y), 7)
关于python - 微型 radio 模块 :bit (python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47340920/