python - 微型 radio 模块 :bit (python)

标签 python bbc-microbit

这是我的 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/

相关文章:

python - Python 中的类型错误 : unsupported types for : 'int' , 'NoneType'

python - 如何使用 pyunpack 解压 .7z 文件?

Python - 网页抓取 - BeautifulSoup

python - numpy中X[:, :, :, i]是什么意思?

syntax-error - 用微比特在mu上编码时的语法错误

python - 强制微:Bit Shutdown

javascript - 在 BBC micro :bit does not do anything 上用 microPython 写模拟

Python-读取文件后数据未附加到列表和缺少字典键

python - 对于已定义的字符串 Python 等价于 b""的操作

micropython - 为什么我的microbit显示这个动画?