My Pygame Game Engine
1## Imports
2
3import pygame
4
5## All constraints that can be used
6
7# Rectangle Constraints
8
9class rect_constraints():
10 pass
11
12class center_constraint(rect_constraints):
13 pass
14
15class aspect_constraint(rect_constraints):
16 def __init__(self, aspect_ratio) -> None:
17 self.value = aspect_ratio
18
19class percentage_constraint(rect_constraints):
20 def __init__(self, percentage):
21 self.value = percentage
22
23class pixel_constraint(rect_constraints):
24 def __init__(self, pixel):
25 self.value = pixel
26
27# Text constraints
28
29class text_size_constraint:
30 pass
31
32class resize_constraint:
33 pass
34
35class samesize_constraint:
36 pass
37
38## Gui Rectangle Class
39
40class gui_rect:
41
42 # Start Vars for everything
43
44 x_constraint = None
45 y_constraint = None
46 width_constraint = None
47 height_constraint = None
48
49 x = 0.5
50 y = 0.5
51
52 tween = False
53 tween_x = 0
54 tween_y = 0
55 tween_mult = 1
56
57 tween_s = False
58 tween_width = 0
59 tween_height = 0
60 tween_s_mult = 1
61
62 tween_c = False
63 tween_color_rgb = (0, 0, 0)
64 tween_color_mult = 1
65
66 tmp_width = 0
67 tmp_height = 0
68
69 width = 0
70 height = 0
71
72 border_radius = 0
73 outline = 0
74
75 color = (0, 0, 0)
76
77 parent = None
78
79 image = 0
80 image_rotation = 0
81
82 x_constraint = pixel_constraint(0)
83 y_constraint = pixel_constraint(0)
84 width_constraint = pixel_constraint(0)
85 height_constraint = pixel_constraint(0)
86
87 ## Set Constraints
88
89 # Position Constraints
90
91 def set_x_constraint(self, x_constraint):
92 self.x_constraint = x_constraint
93
94 def set_y_constraint(self, y_constraint):
95 self.y_constraint = y_constraint
96
97 # Size Constraints
98
99 def set_width_constraint(self, width_constraint):
100 self.width_constraint = width_constraint
101
102 def set_height_constraint(self, height_constraint):
103 self.height_constraint = height_constraint
104
105 # Misc to rect
106
107 def set_border_radius(self, radius):
108 self.border_radius = radius
109
110 def set_outline_radius(self, outline):
111 self.outline = outline
112
113 def set_draw_color(self, rgb):
114 self.color = rgb
115
116 # Tools
117
118 def is_touching_mouse(self):
119 mx, my = pygame.mouse.get_pos()
120 if self.game.math.in_rect(mx, my, self.x, self.y , self.width, self.height):
121 return True
122 return False
123
124 def set_pos(self, x, y, mult):
125 self.tween = True
126 self.tween_x = x
127 self.tween_y = y
128 self.tween_mult = mult
129
130 def set_size(self, width, height, mult):
131 self.tween_s = True
132 self.tween_width = width
133 self.tween_height = height
134 self.tween_s_mult = mult
135
136 def set_color(self, colorrgb, mult):
137 self.tween_c = True
138 self.tween_color_rgb = colorrgb
139 self.tween_color_mult = mult
140
141 def __init__(self, game, parent=None) -> None:
142 self.game = game
143 self.parent = parent
144 self.visible = True
145 self.show_rect = True
146 self.show_image = True
147
148 self.has_initialized = False
149
150 def initialize(self):
151 if self.parent == None:
152 self.parent = self.game.renderer.screen_rect
153
154 ## Update thingy
155
156 def update(self):
157
158 if not self.has_initialized:
159 self.initialize()
160
161 # Tween Position
162
163 tmp_x = 1
164 tmp_y = 1
165 class_use = type(self.tween_x)
166 if class_use == center_constraint:
167 tmp_x = self.parent.x + (self.parent.width / 2) - (int(self.width) / 2)
168 elif class_use == percentage_constraint:
169 tmp_x = self.parent.x + (self.parent.width * self.tween_x.value)
170 elif class_use == pixel_constraint:
171 tmp_x = self.parent.x + self.tween_x.value
172 class_use = type(self.tween_y)
173 if class_use == center_constraint:
174 tmp_y = self.parent.y + (self.parent.height / 2) - (int(self.height) / 2)
175 elif class_use == percentage_constraint:
176 tmp_y = self.parent.y + (self.parent.height * self.tween_y.value)
177 elif class_use == pixel_constraint:
178 tmp_y = self.parent.y + self.tween_y.value
179
180 self.x += (tmp_x - self.x) / max((self.tween_mult * self.game.delta_time) * 600, 0.1)
181 self.y += (tmp_y - self.y) / max((self.tween_mult * self.game.delta_time) * 600, 0.1)
182
183 # Tween Size
184
185 class_use = type(self.tween_width)
186 if class_use == percentage_constraint:
187 self.tmp_width = self.parent.width * self.tween_width.value
188 elif class_use == aspect_constraint:
189 self.tmp_width = self.tmp_height * self.tween_width.value
190 elif class_use == pixel_constraint:
191 self.tmp_width = self.tween_width.value
192
193 class_use = type(self.tween_height)
194 if class_use == percentage_constraint:
195 self.tmp_height = self.parent.height * self.tween_height.value
196 elif class_use == aspect_constraint:
197 self.tmp_height = self.tmp_width * self.tween_height.value
198 elif class_use == pixel_constraint:
199 self.tmp_height = self.tween_height.value
200
201 self.width += (self.tmp_width - self.width) / self.tween_s_mult
202 self.height += (self.tmp_height - self.height) / self.tween_s_mult
203
204 self.color = (self.color[0] + (self.tween_color_rgb[0] - self.color[0]) / self.tween_color_mult,
205 self.color[1] + (self.tween_color_rgb[1] - self.color[1]) / self.tween_color_mult,
206 self.color[2] + (self.tween_color_rgb[2] - self.color[2]) / self.tween_color_mult)
207
208 def render(self):
209 if self.visible:
210 if self.show_rect:
211 pygame.draw.rect(self.game.renderer.main_surface, self.color, ((self.x, self.y), (self.width, self.height)), self.outline, self.border_radius)
212 if self.image != 0 and self.show_image:
213 image = pygame.transform.scale(self.image, (int(self.width), int(self.height)))
214 img, rect = self.game.math.rotate_center(image, self.image_rotation, self.x + self.width / 2, self.y + self.height / 2)
215 self.game.renderer.main_surface.blit(img, rect)
216
217class gui_text:
218 def set_x_constraint(self, constraint):
219 self.x_constraint = constraint
220
221 def set_y_constraint(self, constraint):
222 self.y_constraint = constraint
223
224 def set_size_constraint(self, constraint):
225 self.size_constraint = constraint
226
227 def set_color(self, rgb):
228 self.color = rgb
229
230 def __init__(self, game) -> None:
231 self.game = game
232
233 self.text = ""
234 self.draw_text = self.text
235 self.parent = None
236
237 self.x, self.y = 0, 0
238
239 self.x_constraint = None
240 self.y_constraint = None
241 self.size_constraint = None
242
243 self.size = 12
244
245 self.color = (0, 0, 0)
246
247 def update(self):
248 if self.parent == None:
249 print('Parent of text field must be set')
250 return
251
252 self.draw_text = self.text
253 self.draw_text += '\n'
254 self.draw_text = self.draw_text.split('\n')
255 self.draw_text = self.draw_text[0: -1]
256
257 self.longest_length = 0
258 for i in self.draw_text:
259 length = self.game.font_handler.render(i, 'default', self.size, (0, 0, 0)).get_width()
260 self.longest_length = max(self.longest_length, length)
261
262 class_use = type(self.x_constraint)
263 if class_use == center_constraint:
264 self.x = self.parent.x + (self.parent.width / 2)
265 elif class_use == percentage_constraint:
266 self.x = self.parent.x + (self.parent.width * self.x_constraint.value)
267 elif class_use == pixel_constraint:
268 self.x = self.parent.x + self.x_constraint.value
269
270 class_use = type(self.y_constraint)
271 if class_use == center_constraint:
272 self.y = self.parent.y + (self.parent.height / 2) - (self.size * (len(self.draw_text) - 1)) / 2
273 elif class_use == percentage_constraint:
274 self.y = self.parent.y + (self.parent.height * self.y_constraint.value)
275 elif class_use == pixel_constraint:
276 self.y = self.parent.y + self.y_constraint.value
277
278 class_use = type(self.size_constraint)
279 if class_use == resize_constraint:
280 pass
281 if class_use == percentage_constraint:
282 self.size = int((self.parent.height * self.size_constraint.value) / len(self.draw_text))
283
284 def render(self):
285 for i, item in enumerate(self.draw_text):
286 #pygame.draw.circle(self.game.renderer.main_surface, (255, 0, 0), (self.x ,self.y) ,4)
287 img, rect = self.game.math.rotate_center(self.game.font_handler.render(item, 'default', self.size, self.color), 0, self.x, ((self.y + self.size * i)))
288 self.game.renderer.main_surface.blit(img, rect)
289
290class gui_toggle_button:
291 def __init__(self, game) -> None:
292 self.game = game
293 self.rect = gui_rect(game)
294
295 self.hover = False
296 self.toggled = False
297 self.input_state = ""
298
299 def update(self):
300 self.rect.update()
301 if self.rect.is_touching_mouse():
302 self.hover = True
303 else:
304 self.hover = False
305
306 if self.hover and self.game.input.is_mouse_button_just_pressed(self.input_state):
307 self.toggled = not self.toggled
308
309 def render(self):
310 self.rect.render()
311
312class gui_press_button:
313 def __init__(self, game) -> None:
314 self.game = game
315 self.rect = gui_rect(game)
316
317 self.hover = False
318 self.pressed = False
319
320 def update(self):
321 self.rect.update()
322 if self.rect.is_touching_mouse():
323 self.hover = True
324 else:
325 self.hover = False
326
327 if self.hover and self.game.input.is_mouse_button_just_pressed():
328 self.pressed = True
329 else:
330 self.pressed = False
331
332 def render(self):
333 self.rect.render()
334
335class gui_slider:
336 def __init__(self, game, val_start, val_end) -> None:
337 self.game = game
338 self.slider_body = gui_rect(game)
339 self.slider_head = gui_rect(game)
340
341 self.value = 0
342 self.value_range = (val_start, val_end)
343
344 def update(self):
345 self.slider_body.update()
346 self.slider_head.update()
347
348 slider_bigger_bounding_box = ((self.slider_body.x, self.slider_body.y - 10), (self.slider_body.width, self.slider_body.height + 30))
349
350 self.slider_head.x = max(self.slider_body.x, min(self.slider_head.x, self.slider_body.x + self.slider_body.width))
351
352 if self.slider_head.tween:
353 self.value = (self.slider_head.x - self.slider_body.x) / (self.slider_body.width / self.value_range[1]) * 1.01
354
355 mx, my = pygame.mouse.get_pos()
356 if self.game.math.in_rect(mx, my, slider_bigger_bounding_box[0][0], slider_bigger_bounding_box[0][1], slider_bigger_bounding_box[1][0], slider_bigger_bounding_box[1][1]) and self.game.input.is_mouse_button_pressed():
357 self.slider_head.tween_to(pixel_constraint(mx - self.slider_head.parent.x - self.slider_head.width / 2), center_constraint(), 3)
358
359 def render(self):
360 self.slider_body.render()
361 self.slider_head.render()