-
Notifications
You must be signed in to change notification settings - Fork 0
/
flappy.py
78 lines (67 loc) · 1.58 KB
/
flappy.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from sense_hat import SenseHat
from time import sleep
from random import randint
from threading import Thread
sense = SenseHat()
sense.clear()
##Globals
game_over = False
RED = (255,0,0)
BLACK = (0,0,0)
BLUE = (0,0,255)
y = 4
speed = +0.5
def draw_column():
global game_over
x = 7
gap = randint(2,6)
while x >= 0 and not game_over:
for led in range(8):
sense.set_pixel(x,led,RED)
sense.set_pixel(x,gap,BLACK)
sense.set_pixel(x,gap-1,BLACK)
sense.set_pixel(x,gap+1,BLACK)
sleep(0.5)
for i in range(8):
sense.set_pixel(x,i,BLACK)
if collision(x,gap):
game_over = True
x -= 1
def draw_columns():
while not game_over:
column = Thread(target=draw_column)
column.start()
sleep(2)
def get_shake():
global speed
while not game_over:
accel = sense.get_accelerometer_raw()
x = round(accel['x'])
y = round(accel['y'])
z = round(accel['z'])
sleep(0.01)
if x != 0 or y != 0 or z != 1:
speed = -1
else:
speed = +1
def collision(x,gap):
if x == 3:
if y < gap -1 or y > gap +1:
return True
return False
columns = Thread(target=draw_columns)
columns.start()
shake = Thread(target=get_shake)
shake.start()
while not game_over:
sense.set_pixel(3,y,BLUE)
sleep(0.1)
sense.set_pixel(3,y,BLACK)
y += speed
if y > 7:
y = 7
if y < 0:
y = 0
shake.join()
columns.join()
sense.show_message("You Lose", text_colour=(255,0,0))