使用 micro:bit 玩 Scratch 1.4小遊戲 (跨平台 Mac, Windows, Raspberry Pi篇)

目的:透過 Scratch 1.4 小遊戲來引發學習電腦的樂趣,並透過 micro:bit 來控制動作(Gestures)...

準備環境:
1.MacBook 或 Windows 或 Raspberry Pi(此篇用Windows10筆電示範)
2.micro:bit板(需要USB線連接)
3.Scratch 1.4(離線版,請至 https://scratch.mit.edu/scratch_1.4/ 下載安裝)

執行步驟:
1.先檢查 作業系統OS 是否更新...
2.安裝 Python2.7.13(Mac, Raspberry Pi已預含, 不用另外安裝),
請至Python官網 https://www.python.org/downloads/ 下載 python-2.7.13.msi 並執行安裝...
*在此解釋為何我在此次用 Python 2.7 呢?!
是因為我的 Windows10筆電 是同時安裝 Python 2 及 3 的環境(有關Py2 及 Py3的糾結不再贅述)...
而在第一次安裝python所需套件時卻發現 Py3 的 pip3 無法安裝,如下圖...|||

3.安裝Python套件管理程式 pip2 ( 請參攷 https://read01.com/zh-tw/ADkLQO.html#.WbnO8qhL9PY )
4.準備 Windows10的 USB Virtual Serial Port
請至 arm mbed 官網 https://developer.mbed.org/handbook/Windows-serial-configuration 下載 driver 並執行 mbedWinSerial_16466.exe 再到 電腦管理 確認 是那一個 COM埠 如下圖...

我的電腦是在 COM12 (每一台電腦會有不同)...
如果是其它的平台,分別對應如下...

  • Windows - mbed Serial Port (COMx)
  • Mac OS X - Use the command ls /dev/tty.usbmodem*
  • Linux - Use the command ls /dev/ttyACM*

5.編寫 Python - Scratch 1.4 遠端感應器 程式...(可惜Scratch 2已沒有此功能了)
參攷 Raspberry Pi Learning Resources 教材 ( http://computingchampions.co.uk/wp-content/uploads/2017/07/mbgc.pdf ),
源碼連結:https://drive.google.com/file/d/0B_4eUrknq7N1aXYwTEVzUGFqa28/view?usp=sharing

*要安裝程式所需的套件,如下:
Windows系統 / 命令提示字元 按下 滑鼠右鍵 選擇 以系統管理員身分執行
執行 pyserial, scratch 套件安裝,如下圖...


6.安裝 Mu (micropython編輯器),請至官網 ( https://codewith.mu/#download ) 下載...
7.編寫 micro:bit 的 MicroPython 程式,執行 mu-0.9.13.win.exe , 程式如下圖...



源碼連結:https://drive.google.com/file/d/0B_4eUrknq7N1cEV5dFdhNmZzd1k/view?usp=sharing

插上 USB線 接好 micro:bit 並直接在 Mu 中編寫程式完成後,按下 Flash 直接將 .hex(intel-hex file)上傳...

3.準備積木(僅支援Scratch 1.4)
參攷一:蔡老師的Scratch分身教學( https://storage.googleapis.com/coding4fun-class-video/scratch-1-files/Fish%20Chomp%20-%20Starter.sb2 )
參攷二:Raspberry Pi Learning Resources - micro:bit game controller
with Scratch教材( http://computingchampions.co.uk/wp-content/uploads/2017/07/mbgc.pdf )

利用 Python 2.7 IDLE 執行 Python程式,如下圖...
按下 F5 執行,正常運作如下圖...
如遇到下圖錯誤,需要 啟動 Scratch 1.4 遠端感應器,如下圖...

構想:利用 micro:bit 的 Gestures傳感器(x, y)來移動大魚去吃小魚(只能USB連接micro:bit使用)...
程式如下圖...
1.大魚角色:

2.小魚角色:


源碼連結:https://drive.google.com/file/d/0B_4eUrknq7N1UE5mUUZJdE03Z2c/view?usp=sharing

示範影片:

誌謝(要感謝的人眾多,茲列舉以下代表):
1.BBC micro:bit, http://microbit.org/
2.SCRATCH, https://scratch.mit.edu/
3.Python, https://www.python.org/
4.蔡淑玲老師,Scratch課程教學(8-5, 8-6), http://coding4fun.tw/class/c/scratch_1
5.Raspberry Pi Learning Resources,http://computingchampions.co.uk/wp-content/uploads/2017/07/mbgc.pdf
......

留言