forked from ufosc/TERMINALMONOPOLY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenspace.py
274 lines (227 loc) · 11.3 KB
/
screenspace.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# This file contains the logic for the terminal screen
#TODO rewrite into classes: player & banker
# Terminal total width and height: 150x40
# This matches the default Windows terminal size nicely.
# os.get_terminal_size() should be looked into.
WIDTH = 150
HEIGHT = 40
import os
from style import COLORS
class Player:
### Player Printing below ###
# Each quadrant is half the width and height of the screen
global rows, cols, quadrant1, quadrant2, quadrant3, quadrant4, active_terminal
rows = HEIGHT//2
cols = WIDTH//2
# Create the quadrants as 2D lists
quadrant1 = ['1' * cols] * rows
quadrant2 = ['2' * cols] * rows
quadrant3 = ['3' * cols] * rows
quadrant4 = ['4' * cols] * rows
active_terminal = 1
def print_board(gameboard: list[str]) -> None:
"""
Used in printing the gameboard for the player. Overwrites the current screen to display the gameboard.
Parameters:
gameboard (list[str]): A representation of the gameboard as a list of strings.
Returns: None
"""
# clear_screen()
# Resets cursor position to top left
print("\033[1A" * (HEIGHT + 4), end='\r')
for y in range(len(gameboard)):
print(gameboard[y])
def update_quadrant(n: int, data: str) -> None:
"""
Creates a list of lines from the data string, and pads each line with spaces to match the width of the screen.
Parameters:
n (int): Quadrant number (1-4)
data (str): String data to update quadrant. Separate lines must be indicated by \\n.
Returns: None
"""
line_list = data.split('\n')
for i in range(len(line_list)):
line_list[i] = line_list[i] + ' ' * (cols - len(line_list[i]))
for i in range(len(line_list), rows):
line_list.append(' ' * cols)
match n:
case 1:
global quadrant1
quadrant1 = line_list
case 2:
global quadrant2
quadrant2 = line_list
case 3:
global quadrant3
quadrant3 = line_list
case 4:
global quadrant4
quadrant4 = line_list
def update_quadrant_strictly(n: int, data: str):
"""
Same as update_quadrant, but does not pad the lines with spaces.
Could be useful for color formatting where update_quadrant fails.
Parameters:
n (int): Quadrant number (1-4)
data (str): Data to update quadrant. String must be exactly the right length. (i.e. 75*20)
Returns: None
"""
line_list = data.split('\n')
match n:
case 1:
global quadrant1
quadrant1 = line_list
case 2:
global quadrant2
quadrant2 = line_list
case 3:
global quadrant3
quadrant3 = line_list
case 4:
global quadrant4
quadrant4 = line_list
def update_active_terminal(n: int):
"""
Updates the active terminal to the given number.
Parameters:
n (int): The terminal number to set as active.
Returns: None
"""
global active_terminal
active_terminal = n
def overwrite(text: str = ""):
"""
Writes text over 2nd to last line of the terminal (input line).
Use this method regularly.
Parameters:
text (str): The text to overwrite with. Default is empty string.
Returns: None
"""
print(f'\033[1A\r{text}', end='')
def clear_screen():
"""
Naively clears the terminal screen.
Parameters: None
Returns: None
"""
print(COLORS['RESET'],end='')
os.system('cls' if os.name == 'nt' else 'clear')
def print_screen() -> None:
"""
This function overwrites the previous terminal's display.
Because Terminal Monopoly is not supposed to
repeatedly print lines after lines (there should be no scrollbar in the terminal), this function overwrites
all needed information.
The class variables quadrant1, quadrant2, etc. are iterated through to print each character. Because
splitting the terminal is entirely artificial to this program, it stops at a hardcoded width value and
begins printing the next quadrant.
Parameters: None
Returns: None
"""
# Resets cursor position to top left
print("\033[1A" * (HEIGHT + 4), end='\r')
# Prints the top border, with ternary conditions if terminal 1 or 2 are active
print(COLORS.backBLACK + COLORS.LIGHTGRAY+(COLORS.GREEN+'╔' if active_terminal == 1 else '╔')+('═' * (cols))+
(COLORS.GREEN if active_terminal == 1 or active_terminal == 2 else COLORS.LIGHTGRAY) +'╦'
+(COLORS.GREEN if active_terminal == 2 else COLORS.LIGHTGRAY)+('═' * (cols))+'╗' + COLORS.LIGHTGRAY + " ") # Additional spaces to fill remaining 3 columns
# Prints the middle rows
for y in range(rows):
print((COLORS.GREEN if active_terminal == 1 else COLORS.LIGHTGRAY)+'║', end=COLORS.RESET)
for x in range(2*cols):
if x < cols:
print(quadrant1[y][x], end='')
elif x == cols:
print((COLORS.GREEN if active_terminal == 1 or active_terminal == 2 else COLORS.LIGHTGRAY)+'║'+COLORS.RESET + quadrant2[y][x - cols], end='')
else:
print(quadrant2[y][x-cols], end='')
print((COLORS.GREEN if active_terminal == 2 else COLORS.LIGHTGRAY)+'║'+COLORS.RESET + " ")
# Middle divider
print((COLORS.GREEN if active_terminal == 1 or active_terminal == 3 else COLORS.LIGHTGRAY)+'╠' + '═' * (cols)
+COLORS.GREEN + '╬' + (COLORS.GREEN if active_terminal == 2 or active_terminal == 4 else COLORS.LIGHTGRAY)+ '═' * (cols) + '╣' + COLORS.RESET + " ")
# Prints the bottom rows
for y in range(rows):
print((COLORS.GREEN if active_terminal == 3 else COLORS.LIGHTGRAY)+'║', end=COLORS.RESET)
for x in range(2 * cols):
if x < cols:
print(quadrant3[y][x], end='')
elif x == cols:
print((COLORS.GREEN if active_terminal == 3 or active_terminal == 4 else COLORS.LIGHTGRAY)+'║'+COLORS.RESET + quadrant4[y][x - cols], end='')
else:
print(quadrant4[y][x - cols], end='')
print((COLORS.GREEN if active_terminal == 4 else COLORS.LIGHTGRAY)+'║'+COLORS.RESET + " ")
# Print final row
print((COLORS.GREEN if active_terminal == 3 else COLORS.LIGHTGRAY)+'╚' + '═' * (cols) +
(COLORS.GREEN if active_terminal == 3 or active_terminal == 4 else COLORS.LIGHTGRAY) +'╩'
+ (COLORS.GREEN if active_terminal == 4 else COLORS.LIGHTGRAY) + '═' * (cols) + '╝'+ COLORS.RESET + " ")
# Fills the rest of the terminal
print(' ' * WIDTH, end='\r')
def test_1():
"""Test 1 - Update all quadrants with different characters"""
input("This visual test contains flashing images. Press enter to continue...")
quads = ['', '', '', '']
for row in range(rows):
for col in range(cols):
for i in range(0,4):
quads[i] += str(i)
Player.update_quadrant(i+1, quads[i])
Player.print_screen()
for i in range(4):
quads[i] += '\n'
class Banker:
### Banker Printing below ###
# Columns are used when printing gameboard with player information.
global col_len, col1, col2, col3, left_data, right_data
col_len = (150 - 80)//4
col1 = ["1" * col_len] * HEIGHT
col2 = ["2" * col_len] * HEIGHT
col3 = ["3" * col_len] * HEIGHT
col3 = ["4" * col_len] * HEIGHT
left_data = list[str]
right_data = list[str]
def append_print_data(data: str, side: str):
if(side == 'left'):
left_data.append(data)
else:
right_data.append(data)
def left_print_data() -> list[str]:
pass
def right_print_data() -> list[str]:
pass
def print_terminal(left_data: list[str], right_data: list[str]):
# Resets cursor position to top left
print("\033[1A" * (HEIGHT + 4), end='\r')
# Prints the top border, with ternary conditions if terminal 1 or 2 are active
print(COLORS.backBLACK + COLORS.LIGHTGRAY+(COLORS.GREEN+'╔' if active_terminal == 1 else '╔')+('═' * (cols))+
(COLORS.GREEN if active_terminal == 1 or active_terminal == 2 else COLORS.LIGHTGRAY) +'╦'
+(COLORS.GREEN if active_terminal == 2 else COLORS.LIGHTGRAY)+('═' * (cols))+'╗' + COLORS.LIGHTGRAY + " ") # Additional spaces to fill remaining 3 columns
# Prints the middle rows
for y in range(rows):
print((COLORS.GREEN if active_terminal == 1 else COLORS.LIGHTGRAY)+'║', end=COLORS.RESET)
for x in range(2*cols):
if x < cols:
print(quadrant1[y][x], end='')
elif x == cols:
print((COLORS.GREEN if active_terminal == 1 or active_terminal == 2 else COLORS.LIGHTGRAY)+'║'+COLORS.RESET + quadrant2[y][x - cols], end='')
else:
print(quadrant2[y][x-cols], end='')
print((COLORS.GREEN if active_terminal == 2 else COLORS.LIGHTGRAY)+'║'+COLORS.RESET + " ")
# Middle divider
print((COLORS.GREEN if active_terminal == 1 or active_terminal == 3 else COLORS.LIGHTGRAY)+'╠' + '═' * (cols)
+COLORS.GREEN + '╬' + (COLORS.GREEN if active_terminal == 2 or active_terminal == 4 else COLORS.LIGHTGRAY)+ '═' * (cols) + '╣' + COLORS.RESET + " ")
# Prints the bottom rows
for y in range(rows):
print((COLORS.GREEN if active_terminal == 3 else COLORS.LIGHTGRAY)+'║', end=COLORS.RESET)
for x in range(2 * cols):
if x < cols:
print(quadrant3[y][x], end='')
elif x == cols:
print((COLORS.GREEN if active_terminal == 3 or active_terminal == 4 else COLORS.LIGHTGRAY)+'║'+COLORS.RESET + quadrant4[y][x - cols], end='')
else:
print(quadrant4[y][x - cols], end='')
print((COLORS.GREEN if active_terminal == 4 else COLORS.LIGHTGRAY)+'║'+COLORS.RESET + " ")
# Print final row
print((COLORS.GREEN if active_terminal == 3 else COLORS.LIGHTGRAY)+'╚' + '═' * (cols) +
(COLORS.GREEN if active_terminal == 3 or active_terminal == 4 else COLORS.LIGHTGRAY) +'╩'
+ (COLORS.GREEN if active_terminal == 4 else COLORS.LIGHTGRAY) + '═' * (cols) + '╝'+ COLORS.RESET + " ")
# Fills the rest of the terminal
print(' ' * WIDTH, end='\r')