-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
369 lines (288 loc) · 10.9 KB
/
utils.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
import pygame
import os
from hashlib import md5
import random
import math
import settings
# a function to constrain a value n between n_min and n_max inclusive
def constrain(n, n_min, n_max):
return max(min(n_max, n), n_min)
# a function to map ranges between two values
def interpolate(in_value, in_min, in_max, out_min, out_max):
# check for division by zero
if in_max - in_min == 0:
raise ValueError("Division by zero")
return (in_value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# load a single image
def load_tpng(assets_path) -> pygame.Surface:
img = pygame.image.load("assets/" + assets_path).convert_alpha()
return img
# load all images in a directory
def load_tpng_folder(assets_path):
images = []
# remove trailing slash in case it was added
if assets_path[-1] == "/":
assets_path = assets_path[:-1]
full_path = "assets/" + assets_path
for img_name in sorted(
os.listdir(full_path)
): # sorted is used for OS interoperability
images.append(load_tpng(assets_path + "/" + img_name))
return images
# Draws a grid covering the specified screen, with the optional tile_size and color arguments.
# Default color is an off-white.
def draw_grid(surf, tile_size=16, color=(242, 245, 255)):
width = surf.get_width()
height = surf.get_height()
for x in range(0, int(width // tile_size) + 1):
pygame.draw.line(surf, color, (0, x * tile_size), (width, x * tile_size))
pygame.draw.line(surf, color, (x * tile_size, 0), (x * tile_size, height))
class Button: # todo remove this in favor of classes.button
def __init__(
self,
screen,
pos: tuple[int, int],
size: tuple[int, int],
content: str | pygame.surface.Surface,
# properties for text buttons
font=None,
fontSize=32,
color=(255, 255, 255),
):
self.screen = screen
# If our content is a string, render the text onto a surface with any given settings
if type(content) == str:
self.image = pygame.transform.scale(
self.make_text(content, color, fontSize, font), size
)
# otherwise it should be a surface, so we can just assign it to our image
else:
self.image = content
self.rect = self.image.get_rect()
self.rect.center = pos
self.clicked = False
self.last_pressed = False
self.activating = False
def make_text(
self,
text,
color,
fontSize,
font=None,
stroke=False,
strokeColor=(0, 0, 0),
strokeThickness=1,
):
if font is None:
font = "assets/fonts/" + settings.FONT
if font == "system-ui":
font = None
# if we aren't stroking return the text directly
if not stroke:
return pygame.font.Font(font, fontSize).render(text, 1, color)
# if we are stroking, render the text with the stroke
# first render the text without the stroke
# create a version of the text in the stroke color and blit it to the surface
surf_text = pygame.font.Font(font, fontSize).render(text, 1, color)
surf_text_stroke = pygame.font.Font(font, fontSize).render(text, 1, strokeColor)
# create a transparent surface to draw the text and stroke on
size = (
surf_text.get_width() + strokeThickness * 3,
surf_text.get_height() + strokeThickness * 3,
)
surface = self.make_transparent_surface(size)
# blit the stroke text to the surface
for i in range(strokeThickness * 2 + 1):
for j in range(strokeThickness * 2 + 1):
surface.blit(surf_text_stroke, (i, j))
# blit the text on top of the stroke
surface.blit(surf_text, (strokeThickness, strokeThickness))
# return the surface
return surface
def draw(self):
action = False
mouse_pos = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()[0]
in_button = self.rect.collidepoint(mouse_pos)
# detect our mouse button down and up frame
mouse_down = False
mouse_up = False
if mouse_pressed != self.last_pressed:
mouse_down = mouse_pressed
mouse_up = not mouse_pressed
self.last_pressed = mouse_pressed
if not in_button:
# if the mouse leaves the button deactivate it
self.activating = False
else:
# begin activating on a mouse button down inside the button
if mouse_down:
self.activating = True
# confirm activation on a mouse button up inside the button
if mouse_up and self.activating:
action = True
self.activating = False
# offset button for float/pending activation
offset = self.rect.copy()
if self.activating:
# draw our button indented while activating
offset.x += 2
offset.y += 2
else:
# draw the mouse hover outdent while hovered
if in_button:
offset.x -= 1
offset.y -= 1
self.screen.blit(self.image, offset)
return action
class SpriteSheet:
def __init__(self, asset_path: str, colorkey: tuple = None):
self.asset_path = "assets/" + asset_path
self.sheet = pygame.image.load(self.asset_path).convert_alpha()
if colorkey is not None:
if colorkey == -1:
colorkey = self.sheet.get_at((0, 0))
# check if color is a tuple now
if type(colorkey) is tuple:
self.sheet.set_colorkey(colorkey)
def get_at(self, x, y, width, height):
return self.sheet.subsurface(x, y, width, height)
def dice(self, width, height):
images = []
for y in range(0, self.sheet.get_height(), height):
for x in range(0, self.sheet.get_width(), width):
images.append(self.sheet.subsurface(x, y, width, height))
return images
def dice_to_folder(self, width, height, folder):
if not os.path.exists(folder):
os.makedirs(folder)
else:
print("Folder already exists")
return
count = 0
for y in range(0, self.sheet.get_height(), height):
for x in range(0, self.sheet.get_width(), width):
count += 1
pygame.image.save(
self.sheet.subsurface(x, y, width, height),
folder + "/%d-%d_%d.png" % (count - 1, x, y),
)
# animation class (DaFluffyPotato)
class Animation:
def __init__(self, images, img_dur=5, loop=True):
self.images = images
self.loop = loop
self.img_duration = img_dur
self.done = False
self.frame = 0
def reset(self):
self.done = False
self.frame = 0
def copy(self):
return Animation(self.images, self.img_duration, self.loop)
def update(self):
if self.loop:
self.frame = (self.frame + 1) % (self.img_duration * len(self.images))
else:
self.frame = min(self.frame + 1, self.img_duration * len(self.images) - 1)
if self.frame >= self.img_duration * len(self.images) - 1:
self.done = True
def img(self):
return self.images[int(self.frame / self.img_duration)]
class Seed:
"""
This class represents a Seed object.
Attributes:
__seed (str): The seed value. Default is None which
generates a random seed.
Methods:
get_seed(): Returns the current seed value.
set_seed(seed: str): Sets a new seed value.
"""
def __init__(self, seed: str | None = None):
"""
The constructor for Seed class.
Parameters:
seed (str): The seed value. Default is "Peach".
"""
if seed is None:
# generate a random string to be used as a seed
self.__seed = str(random.random())
else:
self.__seed = seed
def get_seed(self):
"""
The function to get the current seed value.
Returns:
str: The current seed value.
"""
return self.__seed
def set_seed(self, seed: str = "Peach"):
"""
The function to set a new seed value.
Parameters:
seed (str): The new seed value. Default is "Peach".
"""
self.__seed = seed
def __hashed(self, name: str = "default"):
"""
The function to return a hashed value based on the seed and the name.
"""
# generate md5 hash from the seed and the name
result = md5((self.__seed + name).encode()).hexdigest()
# print("Seed: ", self.__seed, "Name: ", name, "Hash: ", result)
return result
def float(self, name: str = "default") -> float:
"""
The function to return a 0-1 float value based on the seed and the name.
"""
result = self.__hashed(name)
# convert the hash to a float value between 0 and 1
return int(result, 16) / 16**32
def bool(self, name: str = "default") -> bool:
"""
The function to return a boolean value based on the seed and the name.
"""
result = self.__hashed(name)
return int(result, 16) % 2 == 0
def int(self, name: str = "default", min: int = 0, max: int = 1_000_000) -> int:
"""
The function to return an integer value based on the seed and the name.
The minimum value is inclusive and the maximum value is exclusive.
By default, the minimum value is 0 and the maximum value is 1,000,000.
"""
result = self.__hashed(name)
return int(result, 16) % (max - min) + min
def choice(self, name: str = "default", choices: list = []) -> any:
"""
The function to return a random choice from a list based on the seed and the name.
"""
if len(choices) == 0:
return None
result = self.__hashed(name)
return choices[int(result, 16) % len(choices)]
def blit_outline(source: pygame.Surface, target: pygame.Surface, dest: tuple):
mask = pygame.mask.from_surface(source)
mask.invert()
mask = mask.to_surface()
mask.set_colorkey((255, 255, 255))
x = dest[0]
y = dest[1]
target.blit(mask, (x - 1, y))
target.blit(mask, (x + 1, y))
target.blit(mask, (x, y - 1))
target.blit(mask, (x, y + 1))
# test our utilities if ran directly
if __name__ == "__main__":
# test the Seed class
seed = Seed("Peach")
for i in range(8):
print(seed.int("testing the seed trial #" + str(i)))
print(seed.bool("testing the seed trial #" + str(i)))
print(seed.float("testing the seed trial #" + str(i)))
for i in range(10):
r = Seed()
sum = 0
for j in range(100000):
sum += r.float(f"test {j}:{i}")
print(sum)