-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.py
433 lines (327 loc) · 12.9 KB
/
graphics.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import pygame
import os
import random
import constants
import geometry
pygame.init()
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
LEFT = 1
UP = 2
RIGHT = 3
DOWN = 4
def draw_arrow(surface, color, rect, direction, width=0):
"""Draws a triangle that fits into the rect, pointing in the given
direction.
"""
x, y, w, h = rect
# points start from the tip of the arrow, and travel clockwise
if direction == LEFT:
points = ((x, y + h / 2), (x + w, y), (x + w, y + h))
elif direction == UP:
points = ((x + w / 2, y), (x + w, y + h), (x, y + h))
elif direction == RIGHT:
points = ((x + w, y + h / 2), (x, y + h), (x, y))
elif direction == DOWN:
points = ((x + w / 2, y + h), (x, y), (x + w, y))
else:
return
pygame.draw.polygon(surface, color, points, width)
def screen_position(position):
"""All calculations are from (0, 0), while all drawing is done
from SCREEN_TOP_LEFT. This converts a calculated position to a drawable
position.
"""
x = position[0] + constants.SCREEN_LEFT
y = position[1] + constants.SCREEN_TOP
return int(x), int(y)
def new_surface(size):
surface = pygame.Surface(size)
surface.set_colorkey(constants.TRANSPARENT)
surface.fill(constants.TRANSPARENT)
return surface
def load_image(path, multiplier=1):
image = pygame.image.load(os.path.join("images", path + ".png"))
if multiplier > 1:
width = image.get_width() * multiplier
height = image.get_height() * multiplier
image = pygame.transform.scale(image, (width, height))
image.convert()
image.set_colorkey(constants.TRANSPARENT)
return image
def border(surface, color, thickness):
"""Draws a border outlining the inside of a surface."""
width = surface.get_width() - thickness * 2
height = surface.get_height() - thickness * 2
rect = (thickness, thickness, width, height)
pygame.draw.rect(surface, color, rect, thickness)
def draw_grid(surface, color, columns, rows, cell_width, cell_height):
for row in range(rows + 1):
start = (0, row * cell_height - 1)
end = (columns * cell_width, row * cell_height - 1)
pygame.draw.line(surface, color, start, end, 2)
for column in range(columns + 1):
start = (column * cell_width - 1, 0)
end = (column * cell_width - 1, rows * cell_height)
pygame.draw.line(surface, color, start, end, 2)
def draw_tile_grid(surface, color):
columns = constants.LEVEL_WIDTH
rows = constants.LEVEL_HEIGHT
width = constants.TILE_WIDTH
height = constants.TILE_HEIGHT
draw_grid(surface, color, columns, rows, width, height)
class Shaker:
"""Shakes the screen. This was way too inefficient and laggy, so
I'm not actually going to use it. Instead, I just manually adjust
the position of everything I draw.
"""
def __init__(self):
self.temp_surface = new_surface(constants.FULL_SIZE)
self.power = 0
def shake(self, surface, background_color=constants.BLACK):
if self.power == 0:
return
x = random.randint(-self.power, self.power)
y = random.randint(-self.power, self.power)
self.temp_surface.blit(surface, (0, 0))
surface.fill(background_color)
surface.blit(self.temp_surface, (x, y))
self.power = 0
def set_power(self, power):
self.power = power
shaker = Shaker()
class Fader:
def __init__(self):
self.surface = new_surface(constants.FULL_SIZE)
self.surface.fill(constants.BLACK)
self.alpha = 255
self.darkness = 0
self.target_darkness = 0
self.speed = 20
def fade_to(self, alpha):
self.target_darkness = 255 - alpha
def set_alpha(self, alpha):
self.alpha = alpha
self.darkness = 255 - alpha
self.target_darkness = 255 - alpha
self.surface.set_alpha(self.darkness)
def update(self):
if self.darkness < self.target_darkness:
self.darkness += self.speed
if self.darkness > self.target_darkness:
self.darkness = self.target_darkness
elif self.darkness > self.target_darkness:
self.darkness -= self.speed
if self.darkness < self.target_darkness:
self.darkness = self.target_darkness
self.surface.set_alpha(self.darkness)
self.alpha = 255 - self.darkness
def draw(self, surface):
if self.alpha != 255:
surface.blit(self.surface, (0, 0))
fader = Fader()
class Spritesheet:
"""Stores a spritesheet made of all of a thing's animations."""
def __init__(self, sheet_path, frame_w, frame_h, frame_counts, multiplier=1):
self.surface = load_image(sheet_path)
self.full_w = self.surface.get_width() * multiplier
self.full_h = self.surface.get_height() * multiplier
if multiplier > 1:
dimensions = self.full_w, self.full_h
self.surface = pygame.transform.scale(self.surface, dimensions)
self.surface.set_colorkey(constants.TRANSPARENT)
self.frame_w = multiplier * frame_w
self.frame_h = multiplier * frame_h
self.anim_count = int(self.full_w / frame_w)
self.frame_counts = frame_counts
self.z_height = 0
def get_frame(self, anim_id, frame):
"""Returns a subsurface containing the specified frame of animation."""
if anim_id >= self.anim_count:
print("get_frame() tried to return a non-existant animation!")
elif frame >= self.frame_counts[anim_id]:
print("get_frame() tried to return a non-existant frame!")
x = self.frame_w * anim_id
y = self.frame_h * frame
return self.surface.subsurface((x, y, self.frame_w, self.frame_h))
class SpriteInstance:
def __init__(self, sheet):
self.current_frame = 0
self.current_anim = 0
self.delay = 0
self.sheet = sheet
def set_frame(self, frame):
self.current_frame = frame
def get_now_frame(self):
"""Returns a subsurface containing the current frame."""
return self.sheet.get_frame(self.current_anim, self.current_frame)
def next_frame(self):
self.current_frame += 1
if self.current_frame >= self.sheet.frame_counts[self.current_anim]:
self.current_frame = 0
def prev_frame(self):
self.current_frame -= 1
if self.current_frame <= -1:
self.current_frame = self.sheet.frame_counts[self.current_anim] - 1
def change_anim(self, anim_id):
if anim_id >= self.sheet.anim_count:
print("change_anim() tried to change to a nonexistant animation.")
elif anim_id != self.current_anim:
self.current_anim = anim_id
self.current_frame = 0
self.delay = 0
def delay_next(self, delay):
"""delays flipping to the next animation frame for some frames
note: must be called every frame of the delay"""
if not self.delay:
self.delay = delay
else:
self.delay -= 1
if self.delay == 0:
self.next_frame()
class Button:
"""Level Editor uses its own Button class - this one is going to be
more generally applicable."""
UNPRESSED = 0
PRESSED = 1
def __init__(self, position, sprite_sheet):
self.sprite = SpriteInstance(sprite_sheet)
self.pressed = False
self.x = position[0]
self.y = position[1]
self.width = self.sprite.sheet.frame_w
self.height = self.sprite.sheet.frame_h
def draw(self, surface):
surface.blit(self.sprite.get_now_frame(), (self.x, self.y))
def touching_point(self, point):
"""Returns whether the button touches a given point or not."""
if self.x <= point[0] <= self.x + self.width:
if self.y <= point[1] <= self.y + self.height:
return True
return False
def press(self):
self.pressed = True
self.sprite.current_anim = self.PRESSED
def unpress(self):
self.pressed = False
self.sprite.current_anim = self.UNPRESSED
class CircleButton:
def __init__(self, position, radius):
self.x = int(position[0])
self.y = int(position[1])
self.radius = radius
def touching_point(self, point):
if geometry.distance((self.x, self.y), point) < self.radius:
return True
return False
ripples = []
class Ripple:
def __init__(self, position, color, final_radius=20, expansion_rate=2.0):
self.position = (int(position[0]), int(position[1]))
self.radius = 1.0
self.half_radius = final_radius // 2
self.final_radius = final_radius
self.width = 1.0
self.expansion_rate = expansion_rate
self.color = color
self.done = False
def update(self, slowmo=1.0):
if self.radius <= self.half_radius:
self.width += self.expansion_rate / 2 / slowmo
elif self.radius >= self.final_radius:
self.done = True
else:
self.width -= self.expansion_rate / 2 / slowmo
if self.width < 1.0:
self.width = 1.0
self.radius += self.expansion_rate / slowmo
def draw(self, surface, offset=(0, 0)):
position = (self.position[0] + offset[0], self.position[1] + offset[1])
radius = int(self.radius)
width = int(self.width)
pygame.draw.circle(surface, self.color, position, radius, width)
def create_ripple(position, color, final_radius=60, expansion_rate=2.0):
ripples.append(Ripple(position, color, final_radius, expansion_rate))
def update_ripples(slowmo=1.0):
ripple_num = len(ripples)
for ripple in reversed(ripples):
ripple_num -= 1
ripple.update(slowmo)
if ripple.done:
del ripples[ripple_num]
def draw_ripples(surface, offset=(0, 0)):
for ripple in ripples:
ripple.draw(surface, offset)
def clear_ripples():
ripples.clear()
def scale(surface, multiplier):
width = int(surface.get_width() * multiplier)
height = int(surface.get_height() * multiplier)
return pygame.transform.scale(surface, (width, height))
font_numbers = load_image("numbers")
font_uppercase = load_image("uppercase")
font_lowercase = load_image("lowercase")
font_symbols = load_image("symbols")
font_numbers_black = load_image("numbers_black")
font_uppercase_black = load_image("uppercase_black")
font_lowercase_black = load_image("lowercase_black")
font_symbols_black = load_image("symbols_black")
# these are in order of appearance in the image
valid_symbols = ('!', ',', '.', '?', '/', "'")
valid_symbols_ascii = [ord(symbol) for symbol in valid_symbols]
char_width = 9
char_spacing = int(char_width + char_width / 9 * 2)
def textify(string, black=False, multiplier=1):
if black:
numbers = font_numbers_black
uppercase = font_uppercase_black
lowercase = font_lowercase_black
symbols = font_symbols_black
else:
numbers = font_numbers
uppercase = font_uppercase
lowercase = font_lowercase
symbols = font_symbols
text_width = char_width
if multiplier != 1:
numbers = scale(numbers, multiplier)
uppercase = scale(uppercase, multiplier)
lowercase = scale(lowercase, multiplier)
symbols = scale(symbols, multiplier)
text_width *= multiplier
text_surface = new_surface((len(string) * char_spacing, 21))
for index, character in enumerate(string):
position = (index * char_spacing, 0)
ascii_value = ord(character)
if 48 <= ascii_value <= 57:
rect = ((ascii_value - 48) * text_width, 0, text_width, 15)
text_surface.blit(numbers, position, rect)
elif 65 <= ascii_value <= 90:
rect = ((ascii_value - 65) * text_width, 0, text_width, 15)
text_surface.blit(uppercase, position, rect)
elif 97 <= ascii_value <= 122:
rect = ((ascii_value - 97) * text_width, 0, text_width, 21)
text_surface.blit(lowercase, position, rect)
else:
index = 0
for symbol in valid_symbols_ascii:
if symbol == ascii_value:
break
index += 1
else:
continue
rect = (index * text_width, 0, text_width, 21)
text_surface.blit(symbols, position, rect)
return text_surface
def text_wall(list_of_strings):
max_length = 0
for string in list_of_strings:
max_length = max(max_length, len(string))
width = max_length * char_spacing
height = len(list_of_strings) * 21
final_surface = new_surface((width, height))
for string_num, string in enumerate(list_of_strings):
text_surface = textify(string)
x = (width - text_surface.get_width()) // 2
y = string_num * 21
final_surface.blit(text_surface, (x, y))
return final_surface