-
Notifications
You must be signed in to change notification settings - Fork 4
/
ct_draw.py
203 lines (197 loc) · 4.71 KB
/
ct_draw.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
import curses as crs
"""
Data for piece geometry and color
"""
pieceSchema = {
'C': {
"color": 3,
"dims": [4, 2],
"sprites": {
"" : [
[1, 1, 1, 1],
[1, 1, 1, 1]]
}
}, 'S': {
"color": 2,
"dims": [6, 3],
"sprites": {
'H': [
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0]],
'V': [
[1, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0]]
}
}, 'Z': {
"color": 1,
"dims": [6, 3],
"sprites": {
'H': [
[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1]],
'V': [
[0, 0, 1, 1, 0, 0],
[1, 1, 1, 1, 0, 0],
[1, 1, 0, 0, 0, 0]]
}
}, 'L': {
"color": 7,
"dims": [6, 3],
"sprites": {
'H': [
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1]],
'V': [
[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0]],
'HP': [
[1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0]],
'VP': [
[0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0]]
}
}, 'R': {
"color": 4,
"dims": [6, 3],
"sprites": {
'H': [
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[1, 1, 1, 1, 0, 0]],
'V': [
[1, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0]],
'HP': [
[0, 0, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0]],
'VP': [
[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1]]
}
}, 'I': {
"color": 6,
"dims": [8, 4],
"sprites": {
'H': [
[0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0]],
'V': [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0]]
}
}, 'T': {
"color": 5,
"dims": [6, 3],
"sprites": {
'H': [
[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 0]],
'V': [
[0, 0, 1, 1, 0, 0],
[1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0]],
'HP': [
[0, 0, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0]],
'VP': [
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 0]]
}
}
}
"""
Draws an alignment grid in the board window
"""
def drawGrid(board):
for x in range(1, 21):
for y in range(1, 21):
board.addch(y, x, '.' if x % 2 != 0 else ' ')
"""
Erases the alignment grid in the board window
"""
def undrawGrid(board):
for x in range(1, 21):
for y in range(1, 21):
board.addch(y, x, " ")
"""
Draws the bottom border in the board window
"""
def drawBoardBorder(board):
board.addch(21, 0, crs.ACS_LTEE)
for x in range(1, 21):
board.addch(21, x, crs.ACS_HLINE)
board.addch(21, 21, crs.ACS_RTEE)
"""
Checks if a character is in the play area of wBoard
"""
def isCharInBounds(y, x):
return y > 0 and y < 21 and x > 0 and x < 21
"""
Draws a piece to a window (use wBoard or wNextP only!)
In order to allow for erasure a.k.a. undrawing of pieces,
the characters argument must either be a string or list,
and the character variable is actually a function;
When undrawing, the grid pattern is ". ", which are
two distinct characters, thus a two-member tuple
with indices 0 and 1 (False and True) can be used and
kept in sync with the x position of the cursor
The check is equals 0, hence even, because the board
starts at an x value of 1. Thus, if ['.', ' '] is passed
in for characters arg. (or ". " since strings are subscriptable),
the odd character cells will get a '.' from int(False)
and even cells will get a ' ' from int(True)
See README.md or check the bottom of this file for details on block behavoir
"""
def drawPiece(y, x, orient, piece, window, characters):
doColor = False
if len(characters) == 1:
character = lambda x : characters[0]
doColor = True
elif len(characters) == 2:
character = lambda x : characters[int(x % 2 == 0)]
else:
return None
for i in range(x, x + pieceSchema[piece]["dims"][0]):
for j in range(y, y + pieceSchema[piece]["dims"][1]):
if isCharInBounds(j, i) \
and bool(
pieceSchema[piece]["sprites"][orient][j - y][i - x]
):
window.addch(
j, i, character(i),
crs.color_pair(pieceSchema[piece]["color"]) if doColor
else crs.color_pair(0)
)
"""
Redraws characters in a window.
Given a target character (B), and a region from
(xi, yi) to (xf, yf) where xi < xf and yi < yf,
any instances of the target char. in the window
will be overwritten with the result character (A)
"""
def changeTexture(yi, xi, yf, xf, characterA, characterB, window):
if not (xi <= xf and yi <= yf):
return None
for i in range(xi, xf + 1):
for j in range(yi, yf + 1):
if type(characterB) is str and window.inch(j, i) == ord(characterB) \
or type(characterB) is int and window.inch(j, i) % 0x100 == characterB % 0x100:
window.addch(j, i, characterA)