File: flappybit.py

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (77 lines) | stat: -rw-r--r-- 1,551 bytes parent folder | download | duplicates (3)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'''
Flappy Bit

Control the bit by tilting the micro:bit

Avoid the obstacles

Create your own terrain by editing the terrain list below:
'''

import music
from microbit import (accelerometer,
                      display,
                      sleep,
                      Image,
                      reset,
                      )

display.scroll('Flappy Bit')

bird = 2
terrain = [
    (0, 0),
    (0, 0),
    (0, 0),
    (0, 0),
    (0, 2),
    (0, 0),
    (0, 0),
    (3, 0),
    (3, 0),
    (4, 0),
    (3, 0),
    (0, 0),
    (0, 0),
    (0, 1),
    (2, 0),
    (3, 0),
    (4, 0),
    (3, 0),
    (0, 0),
    (0, 1),
    (2, 0),
]

terrain_multiplier = 5
pos = 0

while True:
    sleep(100)
    if -256 < accelerometer.get_y() < 450:
        bird = max(0, bird - 1)
    elif 568 < accelerometer.get_y() < 1024:
        bird = min(4, bird + 1)

    display.clear()
    display.set_pixel(0, bird, 9)

    pos_terrain = pos // terrain_multiplier
    lost_status = False
    for column, (top, bottom) in enumerate(
            terrain[pos_terrain:pos_terrain + 5]):
        for y in range(top):
            display.set_pixel(column, y, 4)
            if column == 0 and bird == y:
                lost_status = True
        for y in range(bottom):
            display.set_pixel(column, 4 - y, 4)
            if column == 0 and bird == (4 - y):
                lost_status = True
    if lost_status:
        display.show(Image.SAD)
        music.play(music.FUNERAL)
        reset()
    pos += 1
    if pos_terrain > len(terrain):
        pos = 0