-
Notifications
You must be signed in to change notification settings - Fork 4
/
test2.py
99 lines (85 loc) · 2.68 KB
/
test2.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from etherlight import Etherlight
import time
from random import randint
import copy
from threading import Thread
etherlight = Etherlight("10.0.10.6")
FIELD_LENGTH = 24
class DinoGame:
def __int__(self):
print("init")
self.field = [' ' for x in range(FIELD_LENGTH)]
self.jump = 0
self.will_jump = False
self.old_display = self.get_display()
self.running = False
def get_display(self):
display = copy.deepcopy([[' ' for x in range(FIELD_LENGTH)], self.field])
if '-' not in self.field:
if self.jump > 0:
display[0][1] = 'D'
else:
display[1][1] = 'D'
return display
def update(self):
if '-' in self.field:
self.running = False
time.sleep(1)
for i in range(48):
etherlight.set_led_color(i + 1, [255, 0, 0])
etherlight.flush()
return
self.field.pop(0)
self.field.append(' ')
space = randint(3, 8)
try:
last = self.field[::-1].index("x")
except ValueError:
last = FIELD_LENGTH
if last > space:
self.field[-1] = 'x'
if self.will_jump:
self.jump = 2
self.will_jump = False
if self.jump > 0:
self.jump -= 1
if self.field[1] == 'x' and self.jump == 0:
self.field[1] = '-'
def do_jump(self):
self.jump = 2
self.draw()
def draw(self, force_all=False):
display = self.get_display()
print("=====")
print(display[0])
print(display[1])
print("=====")
for y in range(2):
for x in range(FIELD_LENGTH):
if display[y][x] != self.old_display[y][x] or force_all:
led = 2 * x + y + 1
if display[y][x] == ' ':
etherlight.set_led_color(led, [0, 0, 0])
elif display[y][x] == 'x':
etherlight.set_led_color(led, [0, 255, 0])
elif display[y][x] == 'D':
etherlight.set_led_color(led, [0, 0, 255])
elif display[y][x] == '-':
etherlight.set_led_color(led, [255, 0, 0])
etherlight.flush()
self.old_display = copy.deepcopy(display)
def run(self):
self.draw(True)
time.sleep(2)
self.running = True
while self.running:
self.update()
self.draw()
time.sleep(0.3)
dinogame = DinoGame()
dinogame.__int__()
thread = Thread(target=dinogame.run)
thread.start()
while True:
input()
dinogame.do_jump()