/
tilty.py
55 lines (48 loc) · 1.4 KB
/
tilty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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)