-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow.py
235 lines (206 loc) · 9.69 KB
/
window.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
""" window.py provides graphical drawing support """
import math
import pygame
import pygame.gfxdraw
import features
WHITE = (255, 255, 255)
COLORS = {
'blue': (128, 128, 255),
'green': (0, 196, 0),
'red': (200, 0, 0),
'orange': (200, 128, 0),
'yellow': (210, 210, 0)
}
PLAYER_COLORS = (
(128, 128, 255),
(0, 196, 0),
(200, 0, 0),
(210, 210, 0),
(255, 255, 255),
(150, 75, 0),
)
class Window:
'''draws everything'''
screen = None
cities = None
surface = None
font = None
basis = None
extrema = None
scaling = None
scaled = False
def __init__(self, width, height, scaled=False, basis=([1, 0], [-0.5, math.sqrt(3)/4])):
pygame.init()
self.font = pygame.font.SysFont(None, 24)
self.width = width
self.height = height
self.scaled = scaled
self.screen = pygame.display.set_mode((width, height))
self.surface = pygame.Surface((width, height))
self.basis = basis
self.cities = features.CITIES
temp = features.CORNERS
self.extrema = ((temp[1][1]*self.basis[0][0]+temp[1][0]*self.basis[1][0],
temp[1][1]*self.basis[0][1]+temp[1][0]*self.basis[1][1]),
(temp[0][1]*self.basis[0][0]+temp[0][0]*self.basis[1][0],
temp[0][1]*self.basis[0][1]+temp[0][0]*self.basis[1][1]))
self.scaling = (self.surface.get_size()[0] / abs(self.extrema[0][0] - self.extrema[1][0]),
self.surface.get_size()[1] / abs(self.extrema[0][1] - self.extrema[1][1]))
def get_coords(self, i, j):
'''board coords to screen coords'''
i += 1
j += 1
if not self.scaled:
# Don't show the 'human scale' version of the board; represent it
# as the computer sees it
return (j * int(self.width / (features.LAST_COLUMN + 1)),
i * int((self.height - 50) / (features.LAST_ROW + 1)))
return (int(self.scaling[0] * (j * self.basis[0][0] + i * self.basis[1][0] - self.extrema[1][0])), # pylint: disable=C0301
int(self.scaling[1] * (j * self.basis[0][1] + i * self.basis[1][1])))
def invert_coords(self, x, y):
'''screen coords to row, col point '''
if not self.scaled:
# Don't show the 'human scale' version of the board; represent it
# as the computer sees it
return (int(y / int((self.height - 50) / (features.LAST_ROW + 1)) + 0.25) - 1,
int(x / int(self.width / (features.LAST_COLUMN + 1)) + 0.25) - 1)
row1 = (y / self.scaling[1] - \
((self.basis[0][1] * x) / self.basis[0][0] * self.scaling[0])) / \
(self.basis[1][1] - (self.basis[0][1] * self.basis[1][1]) / self.basis[0][0])
col1 = ((x / self.scaling[0]) - row1 * self.basis[1][0] + self.extrema[1][0]) / \
self.basis[0][0]
return (int(row1 + 0.25) - 1, int(col1 + 0.25) - 1)
def draw_lines(self, board):
""" Draw the lines """
for i in range(0, board.rows):
for j in range(0, board.cols):
for point in board.get_neighbors((i, j), 1, 2):
point1 = self.get_coords(point[0][0], point[0][1])
point2 = self.get_coords(i, j)
color = (128, 128, 128)
if point[1] == 0:
color = (255, 255, 255)
self.draw_thicc_line(point1, point2, abs(7*point[1]-6), color)
def draw_cities(self):
""" Draw the cities """
for color, citygroup in self.cities.items():
for city in citygroup.values():
center = self.get_coords(city[0], city[1])
radius = int(0.25 * min(self.scaling))
pygame.gfxdraw.aacircle(self.surface, center[0], center[1], radius, COLORS[color])
pygame.gfxdraw.filled_circle(self.surface, center[0], center[1], radius,
COLORS[color])
def draw_player_cities(self, hands):
""" Draw the player cities """
for player, hand in enumerate(hands):
for city in hand.values():
radius = int(0.25 * min(self.scaling))
center = self.get_coords(city[0], city[1])
pygame.draw.circle(self.surface, WHITE, center, radius, 2)
box = pygame.Rect(center[0] - radius, center[1] - 1, radius * 2, 3)
pygame.gfxdraw.box(self.surface, box, PLAYER_COLORS[player])
def draw_hubs(self, hubs):
""" draw hubs if placed """
for player, hub in enumerate(hubs):
if hub is None:
continue
radius = int(0.15 * min(self.scaling))
center = self.get_coords(hub[0], hub[1])
pygame.gfxdraw.aacircle(self.surface, center[0], center[1], radius + 1, WHITE)
pygame.draw.circle(self.surface, PLAYER_COLORS[player], center, radius)
#pygame.gfxdraw.line(self.surface, center[0] - radius, center[1] - radius,
# center[0] + radius, center[1] + radius, WHITE)
def draw_thicc_line(self, point0, point1, thickness, color=(255, 255, 255)):
''' utility function to draw anti-aliased thick lines. '''
center_x = (point0[0] + point1[0]) / 2
center_y = (point0[1] + point1[1]) / 2
# The +1 is to get out of the domain error
angle = math.atan2(point0[1] - point1[1] + 1,
point0[0] - point1[0])
hlength = math.hypot(point0[0] - point1[0], point0[1] - point1[1]) / 2. - 2
hthick = thickness / 2.
sangle = math.sin(angle)
cangle = math.cos(angle)
upl = (center_x + hlength*cangle - hthick *sangle,
center_y + hthick *cangle + hlength*sangle)
upr = (center_x - hlength*cangle - hthick *sangle,
center_y + hthick *cangle - hlength*sangle)
btl = (center_x + hlength*cangle + hthick *sangle,
center_y - hthick *cangle + hlength*sangle)
btr = (center_x - hlength*cangle + hthick *sangle,
center_y - hthick *cangle - hlength*sangle)
# For some reason, drawing this twice seems to fill in lines better
pygame.gfxdraw.aapolygon(self.surface, (upl, upr, btr, btl), color)
pygame.gfxdraw.aapolygon(self.surface, (upl, upr, btr, btl), color)
#if not a double cost location, fill in the line
if thickness != 8:
pygame.gfxdraw.filled_polygon(self.surface, (upl, upr, btr, btl), color)
def draw_move(self, move):
""" Draw one move """
point1 = self.get_coords(move[0][0], move[0][1])
point2 = self.get_coords(move[1][0], move[1][1])
self.draw_thicc_line(point1, point2, 6)
self.screen.blit(self.surface, (0, 0))
pygame.display.update()
def draw_text(self, box, hoffset):
""" Draw text onto our surface """
size = box.get_size()
dest = pygame.Rect(self.width - size[0], self.height - size[1] - hoffset, size[0], size[1])
self.surface.blit(box, dest)
def draw_turn(self, turn, player, tracks):
""" Draw text boxes describing the turn """
turnbox = self.font.render("Turn {} ".format(turn), True, (255, 255, 255))
playerbox = self.font.render("Player {} ".format(player), True, (255, 255, 255))
trackbox = self.font.render("Tracks Left {} ".format(tracks), True, (255, 255, 255))
maxw = turnbox.get_size()[0]
h = turnbox.get_size()[1] + 3
if playerbox.get_size()[0] > maxw:
maxw = playerbox.get_size()[0]
if trackbox.get_size()[0] > maxw:
maxw = trackbox.get_size()[0]
# Clear a more than large enough space
dest = pygame.Rect(self.width - maxw * 2, self.height - h * 3, maxw * 2, h * 3)
self.surface.fill((0, 0, 0), dest)
self.draw_text(turnbox, 0)
self.draw_text(playerbox, h)
self.draw_text(trackbox, h * 2)
self.screen.blit(self.surface, (0, 0))
pygame.display.update()
def draw_standings(self, standings):
""" Draw the standings """
left = 3
for i, distance in enumerate(standings):
box = self.font.render("Player {0:1}: {1:4}".format(i + 1, distance), True,
PLAYER_COLORS[i])
top = self.height - box.get_size()[1] - 3
if left == 3:
dest = pygame.Rect(left, top, 6 * (box.get_size()[0] + 15), box.get_size()[1])
self.surface.fill((0, 0, 0), dest)
self.surface.blit(box, (left, top))
self.screen.blit(self.surface, (0, 0))
pygame.display.update()
left += box.get_size()[0] + 10
def draw_prompt(self, prompt):
""" Draw a text prompt """
left = 3
box = self.font.render("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", True, WHITE)
top = self.height - 2 * (box.get_size()[1] + 5)
dest = pygame.Rect(left, top, box.get_size()[0], box.get_size()[1])
self.surface.fill((0, 0, 0), dest)
box = self.font.render(prompt, True, WHITE)
self.surface.blit(box, (left, top))
self.screen.blit(self.surface, (0, 0))
pygame.display.update()
def clear(self):
""" Clear the whole board """
dest = pygame.Rect(0, 0, self.width, self.height)
self.surface.fill((0, 0, 0), dest)
self.screen.blit(self.surface, (0, 0))
pygame.display.update()
def draw_initial(self, board, hands):
""" Draw the whole board """
self.draw_lines(board)
self.draw_cities()
self.draw_player_cities(hands)
self.screen.blit(self.surface, (0, 0))
pygame.display.update()