2 years ago

Program CodeBug with Python 3

Move on from Blocky programming and control your CodeBug with all the power of Python…

The CodeBug is simple to set up and start programming with the Scratch-like Blocky interface on the website. Once you have mastered this, you’ll probably want to move on to the next stage and take full control with a more powerful language like Python 3. This allows you to include more ambitious data structures, such as lists and tuples, and build larger projects with procedures. Your expert started to wonder how good a game you could build with just a square of 25 LEDs and a switch. This is his first attempt.

The full article can be found in The MagPi 41 and was written by Tony Goodhew

You’ll need

CodeBug

USB Micro B cable (included)

Python libraries: python3-serial & python3-codebug-tether

CodeBug script: codebug-tether.cbg

STEP-01 Game rules

We need a simple game in which you press a button when a target is in a certain position. Back in the late 1970s, your expert used to set a ‘Zap the Rat’ game as homework while teaching Commodore PET or Ohio Scientific Basic. You have to click a button as a moving ‘rat’ target passes a certain ‘zapping point’ on a circuit. In this version, the speed increases each time you successfully hit a rat, and you need to hit three rats with as few attempts as possible. It would be nice to display the hits while the game is running, and a final score to show how many times you missed the target. Can all of this be fitted on a 5×5 LED display?

STEP-02 Planning the display

The circuit will be all the outside edge LEDs, with the rats starting their run from the bottom-left corner: (0,0). The zap cell will be at (4,2), with a permanently lit LED at (3,2) to mark it. Hits can be indicated by a column of lit LEDs rising from (1,1) to (1,3). Button ‘B’ will be used to zap the rat. The final score can be displayed with either a character ‘O’ to indicate no missed zap opportunities – quite difficult to achieve – or lit LEDs (0 -25) for each miss. For a ‘hit’ to register, the button must be pressed while the zap position LED is lit; holding the button down won’t work.

STEP-03 Setting up the CodeBug

To run the CodeBug in tethered mode, you’ll need to download and install a program called codebug_tether.cbg. Once installed in the normal way, the CodeBug temporarily becomes a slave device to your Raspberry Pi and obeys instructions sent from Python 3. (This is rather like using Nanpy to control an Arduino from a Pi.)

Two additional Python libraries, python3-serial and python3-codebug-tether, need to be installed on the Pi. Point your browser to the Codebug site and follow the excellent instructions from Thomas Macpherson-Pope.

Open IDLE 3 and type in the Zap_the_rat.py Python script. Save it and run it.

Zap those rats

Zap those rats

STEP-04 Things to do

There are several modifications and additions you could make to the script:

• Randomise the rat’s starting position

• Randomise the direction in which the rat runs around the circuit

• Improve the zero final score to a square, expanding and contracting from position (2,2) to the edge and back again five times as a celebration.

Code listing