准备步骤


  1. 打开KittenCode,并切换到Python3的模式下

image.png

  1. 打开 库管理 => 安装meowbit通讯库

image.png

  1. 用数据线连接掌机与电脑

image.png

案例1 - 掌机方向键控制海龟画图


操作步骤

  1. 复制下面示例代码到KittenCode中运行 ```python from turtle import from meowbit import

board = MeowBit() board.connect()

while 1: if board.sensor.btnValue(‘up’): fd(4) elif board.sensor.btnValue(‘down’): bk(4) elif board.sensor.btnValue(‘right’): rt(90) elif board.sensor.btnValue(‘left’): lt(90)

  1. 2. 点击运行按钮
  2. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/1432841/1607582329699-066421cb-71fb-4a24-bebd-8d9c40842e58.png#crop=0&crop=0&crop=1&crop=1&height=711&id=bd9Db&margin=%5Bobject%20Object%5D&name=image.png&originHeight=635&originWidth=714&originalType=binary&ratio=1&rotation=0&showTitle=false&size=38492&status=done&style=none&title=&width=800)
  3. 3. 成功运行后,海龟画图的窗口将自动弹出,此时可以进行如下操作
  4. 1. 通过喵比特掌机按键,可控制Python中的海龟进行移动
  5. 1. **_上下按键 _**控制海龟前进与后退
  6. 1. **_左右按键 _**控制海龟左转90°与右转90°
  7. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/1432841/1607513098079-a717dab2-8ba0-4c21-90cc-7b39b5f8ade6.png#crop=0&crop=0&crop=1&crop=1&height=500&id=pF6bA&margin=%5Bobject%20Object%5D&name=image.png&originHeight=912&originWidth=1458&originalType=binary&ratio=1&rotation=0&showTitle=false&size=72731&status=done&style=none&title=&width=800)
  8. 4. 要推出程序只需要点击 **_停止按钮_**
  9. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/1432841/1607513565823-b5dae3df-55b9-4ed1-b6b9-3360f5533fe9.png#crop=0&crop=0&crop=1&crop=1&height=460&id=MkXIR&margin=%5Bobject%20Object%5D&name=image.png&originHeight=624&originWidth=407&originalType=binary&ratio=1&rotation=0&showTitle=false&size=26579&status=done&style=none&title=&width=300)
  10. <a name="N8TKN"></a>
  11. ### 代码解释
  12. - 与海龟画图交互,因此需要导入海龟的库
  13. ```python
  14. from turtle import *
  • 与硬件进行交互,因此需要导入喵比特的互动库

    1. from meowbit import *
  • 在Python3中,海龟与硬件都是作为对象,硬件互动涉及到连接的概念,所以需要connect()

    1. board = MeowBit()
    2. board.connect()
  • 掌机按键检测控制海龟运动 ```python

while 1: if board.sensor.btnValue(‘up’): fd(4) elif board.sensor.btnValue(‘down’): bk(4) elif board.sensor.btnValue(‘right’): rt(90) elif board.sensor.btnValue(‘left’): lt(90)

  1. <a name="MSJnI"></a>
  2. ### 要点注意
  3. 1. 通过与硬件模式的编程代码做对比,可以发现Python3的交互体验中,代码的写法发生了改变,以按键为例:
  4. 硬件模式下的 sensor.btnValue('up') => 交互模式下的 board.sensor.btnValue('up')
  5. 2. 硬件与Python进行交互,存在一个打开通讯连接的概念,一定需要注意board对象的connect()操作
  6. ```python
  7. from meowbit import *
  8. board = MeowBit()
  9. board.connect()

案例2 - 终端输入ON与OFF控制屏幕亮灭


操作步骤

  1. 复制下面示例代码到KittenCode中运行 ```python from meowbit import * board = MeowBit() board.connect()

while 1: a=input() if a==’ON’:

  1. #board.led1.on()
  2. board.screen.fill((255,255,255))
  3. if a=='OFF':
  4. #board.led1.off()
  5. board.screen.fill(0)
  1. 2. 终端输入ON或者OFF,既可控制屏幕的亮灭
  2. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/1432841/1607584423664-32fe5fb0-6eb8-48f6-bc96-881befef42b2.png#crop=0&crop=0&crop=1&crop=1&height=743&id=ZjWSS&margin=%5Bobject%20Object%5D&name=image.png&originHeight=743&originWidth=760&originalType=binary&ratio=1&rotation=0&showTitle=false&size=31913&status=done&style=none&title=&width=760)
  3. <a name="aLfIM"></a>
  4. ## 案例3 - ON与OFF控制Robotbit上的电机转动与停止
  5. ---
  6. 1. 将喵比特、Robotbit扩展板、电机3者按下图方式连接(电机接到Robotbit电机接口M1接口的 A+A-,红黑线正反随意)
  7. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1432841/1610510300093-0bd20f8a-95d4-4473-b8ec-5877f9ff5a64.png#crop=0&crop=0&crop=1&crop=1&height=370&id=bPCuK&margin=%5Bobject%20Object%5D&name=image.png&originHeight=482&originWidth=521&originalType=binary&ratio=1&rotation=0&showTitle=false&size=393325&status=done&style=stroke&title=&width=400)
  8. > - 把喵比特插到Robotbit上(保证把Robotbit的电源打开!)
  9. > - 请确保你的Robotbit已经插上,否则代码运行中会提示报错!,表示没有检测到扩展板
  10. 1. 复制下面示例代码到KittenCode中运行
  11. ```python
  12. from meowbit import *
  13. board = MeowBit()
  14. board.connect()
  15. robotbit=board.RobotBit()#请确保你的Robotbit已经插上,否则报错
  16. while 1:
  17. a=input()
  18. if a=='ON':
  19. robotbit.motor(1, 100, 0)
  20. board.led1.on()
  21. if a=='OFF':
  22. robotbit.stopMotor(1)
  23. board.led1.off()
  1. 终端窗口输入ON 或者 OFF 控制电机转动

常见问题与解答


1 - 点运行报错?

  • 报错如下:

image.png

  • 解决办法:这里报错提示找不到串口,请确保:

1、使用的是原装数据线(非充电线)
2、数据线已经连接电脑与掌机
3、掌机的开关已经打开
4、有可能你用了USB集线器,请直接插到电脑的USB口上

:::info 如果以上的问题你都已经做了,再点击点击运行依然报错,可能是掌机的串口驱动没有安装好,可以找CC喵进行技术支持。(Q群444193538) :::

2 - 海龟画图弹出窗口按右上角的 × 无法关闭?

  • 解决办法:这个是官方海龟画图库的Bug,点击KittenCode界面中的停止按钮,即可关闭。

image.png