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

動機:暨上一篇...使用 micro:bit 玩 Scratch 1.4小遊戲 (跨平台 Mac, Windows, Raspberry Pi篇) ( https://davistseng.blogspot.tw/2017/09/microbit-scratch-14-mac-windows.html ),發現~小魚(多種、多隻)的角色 只能重複的複製積木來達成,就想使用 Scratch 2.0 的分身功能來減化......

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

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

執行步驟
1.先檢查 作業系統OS 是否更新...
2.安裝 Python2.7.13(Mac, Raspberry Pi已預含, 不用另外安裝),
請至Python官網 https://www.python.org/downloads/ 下載 python-2.7.13.msi 並執行安裝...
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.依據謝老師分享的 scratch2-microbit (https://github.com/lyshie/scratch2-microbit) 安裝python套件 blockext, pyserial ( 在 Windows系統 / 命令提示字元 按下 滑鼠右鍵 選擇 以系統管理員身分執行 ),如下圖...

6.安裝 Mu (micropython編輯器),請至官網 ( https://codewith.mu/#download ) 下載...
7.編寫屬於自己的 Scratch2 Extension,參攷 依據謝老師分享的 scratch2-microbit - microbit_ext.py 程式,我稍微改寫了...
源碼連結:https://drive.google.com/file/d/0B_4eUrknq7N1T3FVcjdIRUpBSVE/view?usp=sharing
並執行 # python microbit_ext.py...如下圖...

8.編寫 micro:bit 的 MicroPython 程式,執行 mu-0.9.13.win.exe , 參攷 依據謝老師分享的 scratch2-microbit - mb_hex.py 程式,如下圖...

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

9.準備積木(Scratch 2.0)
參攷一:蔡老師的Scratch分身教學( https://storage.googleapis.com/coding4fun-class-video/scratch-1-files/Fish%20Chomp%20-%20Starter.sb2 )
參攷二:謝老師分享的積木示範程式( https://github.com/lyshie/scratch2-microbit/blob/master/microbit_sample.sb2 )

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

2.小魚角色:


源碼連結:https://drive.google.com/file/d/0B_4eUrknq7N1WWdtYjU2bkRaYzg/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.謝立益老師,https://github.com/lyshie/scratch2-microbit
......

留言