-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics_class.py
370 lines (325 loc) · 15.2 KB
/
graphics_class.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import pygame, time, os
from pygame.locals import *
from constants import *
from interface_class import windows
from unit_class import unitlist
from engine_class import data
from gameboard import board
import math
class ColorShifter():
def __init__(self, color_a, color_b):
self.color_a = color_a
self.color_b = color_b
self.current = color_a[:]
def color_shift(self):
STEPS = 100 # 50 is good, over 255 has no effect
increments = []
new_color = []
for i in range(3):
if self.current[i] != self.color_b[i]:
inc = self.color_a[i] - self.color_b[i]
if abs(inc / STEPS) > abs(self.current[i] - self.color_b[i]):
value = self.color_b[i]
else:
if inc / STEPS != 0:
value = self.current[i] - inc / STEPS
else:
value = self.current[i] - (inc / abs(inc))
new_color.append(value)
else:
new_color.append(self.current[i])
new_color = tuple(new_color)
if new_color == self.color_b:
self.color_a, self.color_b = self.color_b[:], self.color_a[:]
self.current = new_color
selection_box_cs = ColorShifter((150, 150, 0), (255, 255, 0))
highlight_move_cs = ColorShifter((0, 150, 0), (50, 200, 50))
highlight_attack_cs = ColorShifter((150, 0, 0), (200, 50, 50))
class Graphics(object):
def __init__(self):
self.display = pygame.display.set_mode(data.display_size)
self.DIRTFLOOR = pygame.image.load(os.path.join("./art/brown_dirt.png")).convert()
self.STONEWALL = pygame.image.load(os.path.join("./art/grey_wall.png")).convert()
self.HUMAN = pygame.image.load(os.path.join("./art/dc-pl.png")).convert()
self.DAMAGECIRCLE = pygame.image.load(os.path.join("./art/jr_red_circle.png")).convert()
self.TREE_1 = pygame.image.load(os.path.join("./art/tree_1.png")).convert()
self.ROCK_1 = pygame.image.load(os.path.join("./art/rock_1.png")).convert()
def draw(self):
self.draw_start_time = time.time()
self.draw_background()
self.draw_square_images()
self.draw_gameboard()
self.draw_hostiles()
self.draw_units()
self.draw_square_numbers()
self.draw_selected_square_highlight()
self.draw_highlight_move()
self.draw_damage()
self.draw_turn_number()
if data.debug:
self.debug_draw_pathfinding_route()
self.debug_draw_pathfinding_info()
self.debug_draw_pathfinding_final_route()
if data.focus == "move":
rect = data.selected_square.get_rect()
rect.topleft = rect.bottomright
text = ORBITRON(20).render("Moving", 1, WHITE, BLUE)
self.display.blit(text, rect)
self.draw_windows()
self.update()
self.draw_total_time = time.time() - self.draw_start_time
def draw_hostiles(self):
for unit in unitlist:
if unit.faction == data.factions[1]:
color = RED
elif unit.faction == data.factions[0]:
color = GREEN
else:
color = BLUE
rect = unit.square.get_rect()
if unit.move_offset:
rect.left += unit.move_offset[0]
rect.top += unit.move_offset[1]
pygame.draw.rect(self.display, color, rect, 2)
def draw_turn_number(self):
surf = pygame.surface.Surface((200,50))
surf.fill(BLACK)
text = DEJAVUSANS(20).render("Turn: " + str(data.turn_num) + " Side: " + str(data.turn), 1, WHITE)
t_rect = text.get_rect()
t_rect.center = surf.get_rect().center
surf.blit(text, t_rect)
x = self.display.get_rect().width - 200
self.display.blit(surf, (x,0))
def draw_highlight_move(self):
highlight_move_cs.color_shift()
highlight_attack_cs.color_shift()
if data.selected_square.unit:
unit = data.selected_square.unit
if unit.highlight_move:
for i in range(len(unit.move_route)):
if i > unit.move_index:
color = highlight_move_cs.current
radius = 8
square = unit.move_route[i]
center = square.get_rect().center
if square.unit:
color = highlight_attack_cs.current
radius = 12
pygame.draw.circle(self.display, color, center, radius )
def draw_damage(self):
font = DEJAVUSANS(data.adjust_for_zoom(20))
if len(data.damage) > 0:
for dam in data.damage:
if time.time() - dam.start_time >= 3:
del dam
else:
surf = self.scale_image_32(self.DAMAGECIRCLE)
surf.set_colorkey(BLACK)
rect = surf.get_rect()
t_center = rect.center
rect.center = dam.defender.square.get_rect().center
rect.top -= dam.float
text = font.render(str(dam.num),1, WHITE)
t_rect = text.get_rect()
t_rect.center = t_center
surf.blit(text, t_rect)
scaled_surf = pygame.transform.smoothscale(surf, (data.unit_size, data.unit_size))
scaled_surf.set_alpha(180)
self.display.blit(scaled_surf, rect)
dam.float += (25 - dam.float) / 20.0
def draw_background(self):
self.display.fill(DARK_GREY)
def scale_image_64(self, image):
surf = pygame.surface.Surface((data.base_square_size, data.base_square_size))
surf.blit(image,(0,0))
surf_scaled = pygame.surface.Surface((data.square_size, data.square_size))
pygame.transform.scale(surf, (data.square_size, data.square_size), surf_scaled)
surf_scaled.convert_alpha()
return surf_scaled
def scale_image_32(self, image):
surf = pygame.surface.Surface((data.base_unit_size, data.base_unit_size))
surf.blit(image,(0,0))
surf_scaled = pygame.surface.Surface((data.unit_size, data.unit_size))
pygame.transform.scale(surf, (data.unit_size, data.unit_size), surf_scaled)
return surf_scaled
def draw_square_images(self):
br = board.get_rect()
self.board_surf = pygame.surface.Surface((br.width, br.height))
dirt_surf = self.scale_image_64(self.DIRTFLOOR)
wall_surf = self.scale_image_64(self.STONEWALL)
tree_surf = self.scale_image_64(self.TREE_1)
tree_surf.set_colorkey(DC_ALPHA)
rock_surf = self.scale_image_64(self.ROCK_1)
rock_surf.set_colorkey(DC_ALPHA)
for column in range(board.board_size[0]):
for row in range(board.board_size[1]):
square = board.get_square((column, row))
x = square.xy[0] * data.square_size
y = square.xy[1] * data.square_size
if square.image == '#':
self.board_surf.blit(wall_surf,(x,y))
elif square.image == '.':
self.board_surf.blit(dirt_surf,(x,y))
elif square.image == 't':
self.board_surf.blit(dirt_surf,(x,y))
self.board_surf.blit(tree_surf, (x,y))
elif square.image == 'o':
self.board_surf.blit(dirt_surf,(x,y))
self.board_surf.blit(rock_surf,(x,y))
self.display.blit(self.board_surf,(data.camera_offset[0],data.camera_offset[1]))
def draw_square_numbers(self):
font_size = zoom(14)
if data.draw_square_numbers:
font = DEJAVUSANS(font_size)
off = data.camera_offset
color = WHITE
for y in range(board.board_size[1]):
for x in range(board.board_size[0]):
num = str(x) + ", " + str(y)
text = font.render(num, 1, color)
t_rect = text.get_rect()
s_rect = board.get_square((x,y)).get_rect()
t_rect.center = s_rect.center
self.display.blit(text, t_rect)
def draw_gameboard(self):
if data.draw_square_lines:
off = data.camera_offset
color = (180, 255, 255)
for i in range(board.board_size[1]+1):
start = [off[0],i * data.square_size + off[1]]
end = [board.board_size[0] * data.square_size + off[0], i * data.square_size + off[1]]
pygame.draw.line(self.display, color, start, end)
for i in range(board.board_size[0]+1):
start = [i * data.square_size + off[0], off[1]]
end = [i * data.square_size + off[0], board.board_size[1] * data.square_size + off[1]]
pygame.draw.line(self.display, color, start, end)
def draw_selected_square_highlight(self):
selection_box_cs.color_shift()
color = selection_box_cs.current
x, y = data.selected_square.xy[0], data.selected_square.xy[1]
x = x * data.square_size + data.camera_offset[0] + 1
y = y * data.square_size + data.camera_offset[1] + 1
if data.selected_square.unit != None:
if data.selected_square.unit.move_dest != None:
x += data.selected_square.unit.move_offset[0]
y += data.selected_square.unit.move_offset[1]
surf = pygame.surface.Surface((data.square_size - 1, data.square_size - 1))
surf.fill(color)
surf.set_alpha(90)
pygame.draw.rect(surf, color, surf.get_rect())
self.display.blit(surf, (x,y))
def draw_units(self):
UNITSCALE = 2
off = data.camera_offset
for unit in unitlist:
rect = unit.get_rect()
rect.left += off[0]
rect.top += off[1]
surf = pygame.surface.Surface((data.base_unit_size, data.base_unit_size))
surf.blit(self.HUMAN, (-32, 0))
scaled_surf = pygame.surface.Surface((data.unit_size * UNITSCALE, data.unit_size * UNITSCALE))
pygame.transform.scale(surf, (data.unit_size * UNITSCALE, data.unit_size * UNITSCALE), scaled_surf)
new_rect = scaled_surf.get_rect()
rect.height = new_rect.height
rect.width = new_rect.width
rect.center = unit.square.get_rect().center
rect.left += unit.move_offset[0]
rect.top += unit.move_offset[1]
scaled_surf.set_colorkey(DC_ALPHA)
self.display.blit(scaled_surf, rect)
for item in unit.equipped:
c_surf = pygame.surface.Surface((16, 32))
c_surf.blit(self.HUMAN, item.imagedata[0])
c_surf_scaled = pygame.surface.Surface((zoom(16 * UNITSCALE),zoom(32 * UNITSCALE)))
pygame.transform.scale(c_surf, (zoom(16 * UNITSCALE), zoom(32 * UNITSCALE)), c_surf_scaled)
c_surf_scaled.set_colorkey(DC_ALPHA)
rect.left += zoom(item.imagedata[1][0])
rect.top += zoom(item.imagedata[1][1])
self.display.blit(c_surf_scaled, rect)
def draw_windows(self): # draws all windows in windows (list)
for window in windows:
if window.visible:
surface = pygame.surface.Surface(window.rect.size)
surface.fill(window.color)
draw_window_contents(surface, window)
self.display.blit(surface, window.rect)
def update(self):
pygame.display.update()
#### debug draws
def debug_draw_pathfinding_info(self):
for row in board.grid:
for square in row:
if data.zoom >= 70:
if square.path_f:
rect = square.get_rect()
rect.top += 2
rect.left += 2
text = FONT.render("F " + str(square.path_f), 1, YELLOW)
self.display.blit(text, rect)
if square.path_g:
rect = square.get_rect()
text = FONT.render("G " + str(square.path_g), 1, AQUA)
t_rect = text.get_rect()
t_rect.top = rect.bottom - t_rect.height
t_rect.left = rect.left + 2
self.display.blit(text, t_rect)
if square.path_h:
rect = square.get_rect()
text = FONT.render("H " + str(square.path_h), 1, PURPLE)
t_rect = text.get_rect()
t_rect.right = rect.right
t_rect.top = rect.top + 2
self.display.blit(text, t_rect)
def debug_draw_pathfinding_route(self):
# This is broken at the moment. I broke it when rebuilding the path to be usable for movement.
c = 1
size = zoom(32)
gap = zoom(16)
font_size = zoom(18)
for square in data.pathfinding_route:
surf = pygame.surface.Surface((size,size))
surf.set_alpha(127)
rect = square.get_rect()
rect.topleft = (0,0)
pygame.draw.rect(surf, RED, rect)
text = UBUNTUMONO(font_size).render(str(c), 0, WHITE)
rect = text.get_rect()
surf_rect = surf.get_rect()
rect.center = surf_rect.center
surf.blit(text, rect)
dest = square.get_rect()
dest.top += gap
dest.left += gap
self.display.blit(surf, dest)
c += 1
def debug_draw_pathfinding_final_route(self):
if data.astar.draw_final_route:
square = data.astar.goal
base = zoom(32)
border = zoom(4)
size = base + border
while square:
s_rect = square.get_rect()
rect = pygame.rect.Rect(0,0,0,0)
rect.height = size
rect.width = size
rect.center = s_rect.center
pygame.draw.rect(self.display, GREEN, rect, border)
square = square.path_parent
def draw_window_contents(surface, window):
""" goes through the list of strings in window.contents and renders them """
left, top = 10, 10
right = 0
for item in window.contents:
text = window.font.render(item, 1, WHITE)
if text.get_rect().right > right:
right = text.get_rect().right
if text.get_rect().bottom + top > surface.get_rect().bottom:
left += (right + 70)
top = 10
right = 0
destination = (left, top)
surface.blit(text,destination)
top += text.get_rect().height + 10
def zoom(number):
return (number * data.zoom) / 100