-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
87 lines (73 loc) · 2.42 KB
/
display.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
from time import sleep
import numpy as np
from colorama import Back, Fore, Style
class Display:
START = "\033[0;0H"
ERASE = "\033[2J"
def __init__(self, height, width):
self.height = height
self.width = width
self._back = np.array(
[[Back.BLACK for j in range(self.width)] for i in range(self.height)],
dtype="object",
)
self._canvas = np.array(
[[" " for j in range(self.width)] for i in range(self.height)],
dtype="object",
)
def __repr__(self) -> str:
return f"Display({self.height}, {self.width})"
def get_text(self, path="rip.txt"):
text = []
try:
with open(path, "r") as f:
for line in f:
text.append([line.strip("\n")])
except FileNotFoundError as e:
print(e)
return np.array(text, dtype="object")
def put(self, item):
x = max(item.x, 0)
x = min(x, self.height - item.height)
y = max(item.y, 0)
y = min(y, self.width - item.width)
item.draw(self._canvas)
def clrscr(self):
for i in range(self.height):
for j in range(self.width):
self._canvas[i][j] = " "
def alert(self, color=Fore.RED):
rip = self.get_text()
if rip is None:
return
print(self.START)
print(color, end="")
for i in range(rip.shape[0]):
for j in range(rip.shape[1]):
print(rip[i][j], end="")
print("")
sleep(2)
print(Style.RESET_ALL + self.ERASE + self.START + "\n")
def show(self):
"""
Show the canvas on the terminal
"""
# ANSI escape for resetting screen point to top
print("\033[0;0H")
for i in range(self.height):
for j in range(self.width):
print(self._back[i][j] + self._canvas[i][j], end="")
print() # next line
def end_game(self, player):
print(Style.RESET_ALL + self.ERASE + self.START + "\n" * 3)
# arr = []
# try:
# with open(path, 'r') as f:
# for line in f:
# arr.append(list(line.strip('\n')))
# except FileNotFoundError as e:
# return None
# return np.array(arr, dtype='object')
# ascii =
def colored(self, x, y):
return self._canvas[x][y] != " "