-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
350 lines (265 loc) · 9.43 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
import math as m
import pygame
from pygame import gfxdraw
pygame.init()
canclick = True
colors = {"white": (255, 255, 255),
"black": (0, 0, 0),
"red": (255, 0, 0),
"green": (0, 255, 0),
"blue": (0, 0, 255)}
def longTextnewLines(text, maxchars): # Puts ~ where new line should be
chars = 0
newstring = ""
for c in text:
chars += 1
if chars > maxchars and c == " ":
newstring += "~"
chars = 0
else:
newstring += c
return newstring
def bigNumbertoString(n):
if n >= 1000000000:
return str(round(n / 1000000000, 2)) + "B"
elif n >= 1000000:
return str(round(n / 1000000, 2)) + "M"
elif n >= 1000:
return str(round(n / 1000, 2)) + "K"
else:
return str(round(n, 2))
def draw_line_as_polygon(gameDisplay, startpos, endpos, width,
color, aa=True): # Wide lines look ugly compared to polygons this draws a polygon as a line
startx, starty = startpos
endx, endy = endpos
angle = m.atan2(endy - starty, endx - startx)
perpangle = angle - m.pi / 2
coords = [(startx + m.cos(perpangle) * width, starty + m.sin(perpangle) * width),
(startx + m.cos(perpangle) * -1 * width, starty + m.sin(perpangle) * -1 * width),
(endx + m.cos(perpangle) * -1 * width, endy + m.sin(perpangle) * -1 * width),
(endx + m.cos(perpangle) * width, endy + m.sin(perpangle) * width)]
pygame.draw.polygon(gameDisplay, color, coords)
if aa:
gfxdraw.aapolygon(gameDisplay, coords, color)
class Button:
def __init__(self, surface, rect, text, text_color, text_size, action, color, highlighted_color, arg=None):
self.surface = surface
self.action = action
self.rect = rect
self.text = text
self.text_color = text_color
self.text_size = text_size
self.action = action
self.color = color
self.highlighted_color = highlighted_color
self.arg = arg
self.font = pygame.font.SysFont(None, text_size)
self.text = self.font.render(text, True, text_color)
self.textrect = self.text.get_rect()
self.buttoncenter = (rect[0] + rect[2] / 2, rect[1] + rect[3] / 2)
self.textcenter = (rect[0] + self.textrect[2] / 2, rect[1] + self.textrect[3] / 2)
self.surface.blit(self.text, (
rect[0] - (self.textcenter[0] - self.buttoncenter[0]), rect[1] - (self.textcenter[1] - self.buttoncenter[1])))
def update(self, mouse=None, newx=None, newy=None):
global canclick
# print(canclick)
if newx is not None:
self.rect[0] = newx
if newy is not None:
self.rect[1] = newy
if mouse is not None:
mouse = [pygame.mouse.get_pos(), pygame.mouse.get_pressed()[0]]
mousex, mousey, click = mouse[0][0], mouse[0][1], mouse[1]
if not click:
canclick = True
if self.rect[0] < mousex < self.rect[0] + self.rect[2] and mousey > self.rect[1] and mousey < self.rect[1] + self.rect[3]:
if self.color != "none":
pygame.draw.rect(self.surface, self.highlighted_color, self.rect)
if click and canclick:
if self.arg is None:
self.action()
else:
self.action(self.arg)
canclick = False
else:
if self.color != "none":
pygame.draw.rect(self.surface, self.color, self.rect)
self.surface.blit(self.text, (self.rect[0] - (self.textcenter[0] - self.buttoncenter[0]),
self.rect[1] - (self.textcenter[1] - self.buttoncenter[1])))
def button(surface, rect, text, text_color, text_size, action, color, highlighted_color, arg=None, mouse=None):
global canclick
shouldr = False
if mouse == None:
mouse = [pygame.mouse.get_pos(), pygame.mouse.get_pressed()[0]]
mousex, mousey, click = mouse[0][0], mouse[0][1], mouse[1]
if not click:
canclick = True
if rect[0] < mousex < rect[0] + rect[2] and mousey > rect[1] and mousey < rect[1] + rect[3]:
if color != "none":
pygame.draw.rect(surface, highlighted_color, rect)
if click and canclick:
shouldr = True
if arg is None:
action()
else:
action(arg)
canclick = False
else:
if color != "none":
pygame.draw.rect(surface, color, rect)
font = pygame.font.SysFont(None, text_size)
text = font.render(text, True, text_color)
textrect = text.get_rect()
buttoncenter = (rect[0] + rect[2] / 2, rect[1] + rect[3] / 2)
textcenter = (rect[0] + textrect[2] / 2, rect[1] + textrect[3] / 2)
surface.blit(text, (rect[0] - (textcenter[0] - buttoncenter[0]), rect[1] - (textcenter[1] - buttoncenter[1])))
return shouldr
def text(surface, coor, text, text_color, text_size, align="center"):
font = pygame.font.SysFont(None, text_size)
text = font.render(text, True, text_color)
x, y = coor[0], coor[1]
r = text.get_rect()
if align == "center":
x -= r.width / 2
y -= r.height / 2
surface.blit(text, (x, y))
def long_text(surface, coor, newLinedtext, text_color, text_size, maxchar,
align="center"): # Runs slow may need to optimize
parts = []
partsrendertext = []
part = ""
for c in newLinedtext:
if c == "~":
parts.append(part)
part = ""
else:
part += c
parts.append(part)
font = pygame.font.SysFont(None, text_size)
for p in range(len(parts)):
partsrendertext.append(font.render(parts[p], True, text_color))
x, y = coor[0], coor[1]
r = partsrendertext[0].get_rect()
if align == "center":
x -= r.width / 2
y -= r.height / 2
for p in range(len(parts)):
surface.blit(partsrendertext[p], (x, y + r.height * p))
return len(parts) * r.height
slidersid = []
sliderslok = []
def slider(surface, coor, lower, upper, value):
if coor in slidersid:
guy = slidersid.index(coor)
locked = sliderslok[guy]
else:
slidersid.append(coor)
sliderslok.append(False)
guy = len(slidersid) - 1
sliderval = value
x, y = coor[0], coor[1]
width = 300
height = 30
scale = (upper - lower) / 300
# print(scale)
sliderx = (sliderval - lower) / scale + x
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(surface, (100, 100, 100), [x, y, width, height])
if click[0] and mouse[0] > x and mouse[0] < x + width and mouse[1] > y and mouse[1] < y + height:
if True in sliderslok:
pass
else:
sliderslok[guy] = True
if click[0] is False:
sliderslok[guy] = False
if sliderslok[guy] is True:
sliderx = mouse[0]
if sliderx > x + width:
sliderx = x + width
elif sliderx < x:
sliderx = x
sliderval = (sliderx - x) * scale + lower
# print(sliderval)
pygame.draw.rect(surface, (150, 150, 150), [sliderx - 12.5, y - 25 + height / 2, 25, 50])
return sliderval
def initialinput():
class Key:
a = False
d = False
w = False
s = False
e = False
q = False
space = False
lshift = False
return Key()
def basicinput(event, keystates):
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
keystates.a = True
if event.key == pygame.K_d:
keystates.d = True
if event.key == pygame.K_w:
keystates.w = True
if event.key == pygame.K_s:
keystates.s = True
if event.key == pygame.K_LSHIFT:
keystates.lshift = True
if event.key == pygame.K_e:
keystates.e = True
if event.key == pygame.K_q:
keystates.q = True
if event.key == pygame.K_SPACE:
keystates.space = True
if event.key == pygame.K_ESCAPE:
pygame.quit()
# quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
keystates.a = False
if event.key == pygame.K_d:
keystates.d = False
if event.key == pygame.K_w:
keystates.w = False
if event.key == pygame.K_s:
keystates.s = False
if event.key == pygame.K_LSHIFT:
keystates.lshift = False
if event.key == pygame.K_e:
keystates.e = False
if event.key == pygame.K_q:
keystates.q = False
if event.key == pygame.K_SPACE:
keystates.space = False
return keystates
def sigmoid(x):
x = x / 10
y = round(1 / (1 + 2 ** (-1 * x)), 5)
return (y)
def cprint(a):
for v in a:
print(v)
def arsort(l):
return (l[0])
def cdistance(x1, y1, x2, y2):
d = m.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
return (d)
def bounce(v, normal, sc):
outgoing = 2 * normal - m.pi - v[1]
a = outgoing
b = v[0] / sc
return ([b, a])
def veladd(v1, v2):
tx = 0
ty = 0
tx += v1[0] * m.cos(v1[1])
ty += v1[0] * m.sin(v1[1])
tx += v2[0] * m.cos(v2[1])
ty += v2[0] * m.sin(v2[1])
newdir = m.atan2(ty, tx)
newvel = m.sqrt((tx) ** 2 + (ty) ** 2)
return ([newvel, newdir])