-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.py
81 lines (59 loc) · 1.8 KB
/
level.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
import sys
import threading
from collections import deque
from time import sleep
class GameThread(threading.Thread):
def __init__(self):
super().__init__()
self.shift = 1
self.state = deque([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0])
self.previous = self.state.copy()
self.speed = 0.2
self.enter = False
def run(self):
self.print_line()
print()
while self.game_not_ended():
self.go_to_next_line()
self.change_direction()
self.shift_line()
self.print_line()
sleep(self.speed)
def game_not_ended(self):
return any(self.state)
def change_direction(self):
if self.shift == 1 and self.state[-1] == 1:
self.shift = -1
elif self.state[0] == 1:
self.shift = 1
def shift_line(self):
self.state.rotate(self.shift)
def print_line(self):
line = ""
for i in self.state:
line += "*" if i == 1 else " "
print(line, end="\r", flush=True)
def go_to_next_line(self):
if self.enter:
self.enter = False
state = self.state
self.state = deque([a and b for a, b in zip(self.state, self.previous)])
self.previous = state
def enter_pressed(self):
self.enter = True
class InputThread(threading.Thread):
def __init__(self, game):
super().__init__()
self.game = game
def run(self):
while self.game.game_not_ended():
char = sys.stdin.read(1)
if char == "\n":
self.game.enter_pressed()
game_thread = GameThread()
input_thread = InputThread(game_thread)
game_thread.start()
input_thread.start()
game_thread.join()
input_thread.join()
print("well done!")