这是我的python中我的微代码的代码

所以我想知道如何从微比特1发送target_x和target_y到第二微比特?

微比特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)


所以我想知道如何从微比特1发送target_x和target_y到第二微比特?

谢谢你的回答

最佳答案

我使用两个微比特测试了下面的代码。我在接收方上添加了“ except,try”子句,以防消息损坏。为了实现可靠的无线接口,还应该执行更多的错误检查,但这可以回答问题。

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)

09-27 05:37
查看更多