# Tilty MicroBit game

I am lucky enough to have 3 teacher BBC MicroBits to play with, and I am exploring using the awesome Mu Python editor – this allows you easily to program the MicroBit in Python totally offline, with absolutely no internet access at all. This means you can still play with your MicroBit come the zombie apocalypse, assuming we still have electricity. (I’ve seen bits of Zombieland, so I know the electricity stays on in these situations.)

Here’s my first attempt at writing a game. It’s really simple but has TWENTY levels (sort of). You tilt your MicroBit to colour in all the pixels. When you fill in all 25, you move on to the next level… but the game gets harder to control as the delay in the loop gets shorter. It’s pretty easy but quite maddening if you get one pixel that you can’t fill in.

Hook up a speaker to pins 0 and GND if you want to hear the sounds.

You could improve the game in lots of ways, for example by having random events to wipe out certain pixels so you have to fill them in again, or by having obstacles you must NOT touch. Tinker, enjoy!

(You can also find the code on GitHub here: https://github.com/blogmywiki/tilty)

```# A simple MicroBit game by Giles Booth
# Tilt and colour in all the squares
# If you win level 20, press reset button to play again

from microbit import display, accelerometer, sleep, Image
from music import play, POWER_UP, NYAN

def get_xy():
yaccel = accelerometer.get_y() * accelerometer_sensitivity
xaccel = accelerometer.get_x() * accelerometer_sensitivity
return yaccel, xaccel

def count_lit_pixels():
pixel_count = 0
for xx in range (5):
for yy in range (5):
if display.get_pixel(xx, yy) != 0:
pixel_count += 1
return pixel_count

pause = 100
level = 1
accelerometer_sensitivity=1/300

#set initial position
x, y = 2, 2
yaccel, xaccel = get_xy()
y = max(0, min(4, int(y + yaccel)))
x = max(0, min(4, int(x + xaccel)))

while pause > 0:
yaccel, xaccel = get_xy()
newy = max(0, min(4, int(y + yaccel)))
newx = max(0, min(4, int(x + xaccel)))
if newy != y or newx != x:
display.set_pixel(x, y, 1)
x, y = newx, newy
display.set_pixel(x, y, 9)
else:
display.set_pixel(newx, newy, 9)
pixels = count_lit_pixels()
if pixels == 25:
play(POWER_UP, wait=False)
level += 1
pause -= 5
sleep(200)
display.show(str(level))
sleep(1000)
display.clear()
sleep(pause)

play(NYAN, wait=False)
display.show('WIN!')
sleep(200)
display.show(Image.HEART)```
This entry was posted in computers, education and tagged , , , . Bookmark the permalink.