You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
PROGRAM CODEBUG WITH PYTHON 3<br />
Zap_the_rat.py<br />
Tutorial<br />
Language<br />
>PYTHON<br />
# Zap the Rat - Python3 Game for Tethered CodeBug<br />
# Tony Goodhew - 5 Oct 2015<br />
import codebug_tether<br />
import time<br />
cb = codebug_tether.CodeBug()<br />
cb.clear()<br />
Above The final score: seven missed opportunities to zap the rats<br />
>STEP-02<br />
Planning the display<br />
The circuit will be all the outside edge LEDs, with the<br />
rats starting their run from the bottom-left corner:<br />
(0,0). The zap cell will be at (4,2), with a permanently<br />
lit LED at (3,2) to mark it. Hits can be indicated by a<br />
column of lit LEDs rising from (1,1) to (1,3). Button<br />
‘B’ will be used to zap the rat. The final score can be<br />
displayed with either a character ‘O’ to indicate no<br />
missed zap opportunities - quite difficult to achieve -<br />
or lit LEDs (0 -25) for each miss. For a ‘hit’ to register,<br />
the button must be pressed while the zap position LED<br />
is lit; holding the button down won't work.<br />
>STEP-03<br />
Setting up the CodeBug<br />
To run the CodeBug in tethered mode, you'll<br />
need to download and install a program called<br />
codebug_tether.cbg. Once installed in the normal<br />
way, the CodeBug temporarily becomes a slave device<br />
to your Raspberry Pi and obeys instructions sent from<br />
Python 3. (This is rather like using Nanpy to control<br />
an Arduino from a Pi.)<br />
Two additional Python libraries, python3-serial<br />
and python3-codebug-tether, need to be installed<br />
on the Pi. Point your browser to magpi.cc/1MeIWdR<br />
and follow the excellent instructions from Thomas<br />
Macpherson-Pope.<br />
Open IDLE 3 and type in the Zap_the_rat.py Python<br />
script. Save it and run it.<br />
>STEP-04<br />
Things to do<br />
There are several modifications and additions you<br />
could make to the script:<br />
> Randomise the rat’s starting position<br />
> Randomise the direction in which the rat runs<br />
around the circuit<br />
> Improve the zero final score to a square, expanding<br />
and contracting from position (2,2) to the edge and<br />
back again five times as a celebration.<br />
# Circuit LED co-ordinates - round the edge from bottom-left<br />
LEDx =(0,0,0,0,0,1,2,3,4,4,4,4,4,3,2,1)<br />
LEDy =(0,1,2,3,4,4,4,4,4,3,2,1,0,0,0,0)<br />
for i in range(0,-66,-1): # Display game title<br />
cb.write_text(i,0,'Zap the Rat', direction="right")<br />
time.sleep(0.1)<br />
score = 0<br />
# Rats zapped!<br />
opps = 0<br />
# Zap opportunities<br />
p = 0 # Position of rat on circuit (0-15)<br />
delay = 0.05 # Initial delay between rat moves<br />
old_sw = 0<br />
# Last value of switch B<br />
running = True<br />
# Loop control variable<br />
cb.clear()<br />
cb.set_pixel(3,2,1)<br />
# Show Zap position<br />
while running:<br />
cb.set_pixel(LEDx[p],LEDy[p],1) # Show rat<br />
if p == 10: opps = opps +1 # Incr opps - in (4,2)<br />
time.sleep(delay - score * 0.007) # Wait - gets faster<br />
sw = cb.get_input('B')<br />
# Read switch B<br />
pixel = cb.get_pixel(4,2) # Rat in Zap position?<br />
cb.set_pixel(LEDx[p],LEDy[p],0) # Hide rat<br />
if old_sw == 0 and sw == 1 and pixel == 1: # Hit?<br />
score = score +1<br />
# Increment hits<br />
cb.set_pixel(1,score,1) # Show hits<br />
if score == 3:<br />
# Game over?<br />
running = False<br />
# Stop looping<br />
p = -1<br />
# Position of next rat<br />
old_sw = sw<br />
# Store switch value<br />
p = p + 1<br />
# Update rat position<br />
if p > 15:<br />
# Rat pointer out of range?<br />
p = 0<br />
# Reset to start position<br />
cb.clear() # Game over - display opportunities<br />
opps = opps - 3 # Remove successful opportunities<br />
print("Missed Opportunities were ",opps)<br />
if opps > 25: # Reduce missed opps to 25 max<br />
opps = 25<br />
if opps == 0:<br />
cb.write_text(0,0,'O') # Zero missed opps<br />
else:<br />
for x in range(0,5): # Display missed opps<br />
for y in range(0,5):<br />
if opps > 0:<br />
cb.set_pixel(x,y,1)<br />
else:<br />
cb.set_pixel(x,y,0)<br />
opps = opps - 1<br />
time.sleep(5)<br />
cb.clear() # Tidy display<br />
raspberrypi.org/magpi January 2016 49