-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
300 lines (228 loc) · 9.31 KB
/
engine.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
import pygame, time, math
from collections import defaultdict
class Game():
def start():
Time.start()
def update():
Time.update()
Timer.update()
Animation.update()
obj.update_all()
def render(surface):
obj.render_all(surface)
class Time():
delta_time = float()
last_time = float()
dt = float()
speed = 1
def start():
Time.last_time = time.time()
def update():
Time.dt = time.time() - Time.last_time
Time.last_time = time.time()
Time.delta_time = Time.dt * Time.speed
class obj():
objects = []
world_camera = (0, 0)
tagged = defaultdict(list)
named = defaultdict(list)
orderd = defaultdict(lambda: defaultdict(list))
def __init__(self, name, x, y, w, h, sprite=None, order=0, order_layer=None, tags=[], show=True, collider=False, color=None, camera=True):
self.name = name
self.x = x
self.y = y
self.width = w
self.height = h
self.components = []
self.order = order
self.order_layer = order_layer
self.tags = tags
self.sprite = sprite
self.current_animation = None
self.show = show
self.color = color
self.camera = camera
obj.objects.append(self)
obj.orderd[order_layer][order].append(self)
for tag in self.tags:
obj.tagged[tag].append(self)
obj.named[self.name].append(self)
def check_collison(self,other):
return self.x < other.x + other.width and self.x + self.width > other.x and self.y < other.y + other.height and self.y + self.height > other.y
def distance_to(self, other):
return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
def direction_to(self, other):
rads = math.atan2(-(other.y - self.y), other.x - self.x)
rads %= 2 * math.pi
return rads
def draw_self_rect(self, surface):
if self.on_screen(surface):
pygame.draw.rect(surface, self.color, pygame.Rect(
self.x - obj.world_camera[0] - self.width/2, self.y - obj.world_camera[1] - self.height/2, self.width, self.height))
def update_sprite(self):
if self.current_animation != None:
self.sprite = self.current_animation.sprite
def draw_self(self, surface):
if self.on_screen(surface):
self.update_sprite()
if self.sprite != None:
surface.blit(
self.sprite, (self.x - obj.world_camera[0] - self.width/2, self.y - obj.world_camera[1] - self.height/2))
def change_order(self, order):
if self.order != order:
obj.orderd[self.order_layer][self.order].remove(self)
obj.orderd[self.order_layer][order].append(self)
self.order = order
def get_rect(self):
return pygame.Rect(self.x - self.width/2, self.y - self.height/2, self.width, self.height)
def on_screen(self, surface):
w, h = surface.get_size()
vertically = 0 < self.y - \
obj.world_camera[1] + self.height/2 and self.y - \
obj.world_camera[1] - self.height/2 < h
horizontally = 0 < self.x - \
obj.world_camera[0] + self.width/2 and self.x - \
obj.world_camera[0] - self.width/2 < w
return horizontally and vertically
def world_pos(self):
return (self.x - obj.world_camera[0],self.y - obj.world_camera[1])
def get_objects_by_tag(tag):
return obj.tagged[tag]
def get_objects_by_tags(tags):
full_list = []
for tag in tags:
for i in obj.tagged[tag]:
full_list.append(i)
return full_list
def get_objects_by_name(name):
return obj.named[name]
def kill(self):
if self not in obj.objects:
return
for co in self.components:
co.kill()
obj.objects.remove(self)
for tag in self.tags:
obj.tagged[tag].remove(self)
obj.named[self.name].remove(self)
obj.orderd[self.order_layer][self.order].remove(self)
del self
def update(self):
pass
def on_drawing(self, surface):
pass
def render_all(surface):
for layer in obj.orderd:
for order in sorted(obj.orderd[layer]):
for object in obj.orderd[layer][order]:
if object.show == True:
if object.color == None:
object.draw_self(surface)
else:
object.draw_self_rect(surface)
for object in obj.objects:
object.on_drawing(surface)
def update_all():
for object in obj.objects:
object.update()
class ui():
def draw_bar(surface, pos, size, border_color, bar_color, progress, vertical = False ,padding=3, border_size=1):
progress = progress if progress > 0 else 0
progress = progress if progress < 1 else 1
if vertical:
inner_size = (size[0] - padding, (size[1] - padding * 2) * progress)
inner_pos = (pos[0] + padding, pos[1] + size[1] - ((size[1] - padding * 2) * progress) + padding)
else:
inner_size = ((size[0] - padding * 2) * progress, size[1] - padding * 2)
inner_pos = (pos[0] + padding, pos[1] + padding)
pygame.draw.rect(surface, bar_color, (inner_pos, inner_size))
pygame.draw.rect(surface, border_color, (pos, size), border_size)
def draw_button(surface, pos, size, text, background_color="white", text_color="black", border_color="black", border_size=1, font=None):
rect = pygame.Rect(pos, size)
pygame.draw.rect(surface, background_color, rect)
pygame.draw.rect(surface, border_color, (pos, size), border_size)
if isinstance(text, str):
blit = font.render(text, True, text_color)
s = blit.get_size()
elif isinstance(text, pygame.Surface):
blit = text
s = blit.get_rect()
s = (s.w, s.h)
surface.blit(blit, (pos[0] + rect.w/2 - s[0] /
2, pos[1] + rect.h/2 - s[0]/2))
return rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]
def draw_panel(surface, pos, size, color="white", border_color="black", border_size=1, padding=2):
inner_pos = (pos[0] + padding, pos[1] + padding)
inner_size = ((size[0] - padding * 2), size[1] - padding * 2)
pygame.draw.rect(surface, color, (inner_pos, inner_size))
pygame.draw.rect(surface, border_color, (pos, size), border_size)
def draw_text(surface, text, color, pos, font, shadow=True, shadow_pos=(1,1), shadow_color = "black", anti_alias=True, line_spacing=20):
lines = text.split("|")
line_spacing = line_spacing if len(lines) > 1 else 0
for i, line in enumerate(lines):
text_surf = font.render(line, anti_alias, color)
size = text_surf.get_size()
if shadow:
shadow_surf = font.render(line, anti_alias, shadow_color)
surface.blit(shadow_surf, (pos[0] - size[0]/2 + shadow_pos[0], pos[1] - len(lines) * line_spacing/2 - size[1]/2 + shadow_pos[1] + i * line_spacing))
surface.blit(text_surf, (pos[0] - size[0]/2, pos[1] - len(lines) * line_spacing/2 - size[1]/2 + i * line_spacing))
class Timer():
timers = []
def __init__(self, duration, func):
self.duration = duration
self.time = duration
self.func = func
self.reseted_time = 0
Timer.timers.append(self)
def update():
for timer in Timer.timers:
if timer.reseted_time == 0:
if timer.time > 0:
timer.time -= Time.delta_time
else:
if timer.time != -1:
timer.func()
timer.time = -1
else:
timer.time = timer.reseted_time
timer.reseted_time = 0
def reset(self, time=None, func=None):
self.reseted_time = self.duration if time == None else time
self.func = self.func if func == None else func
def kill(self):
if self not in Timer.timers:
return
Timer.timers.remove(self)
del self
class Animation():
animations = []
def __init__(self, frames, fps):
self.frames = frames
self.sprite = self.frames[0]
self.current_frame = 0
self.fps = fps
self.time = 0
Animation.animations.append(self)
def next_frame(self):
self.current_frame += 1
if self.current_frame == len(self.frames):
self.current_frame = 0
self.sprite = self.frames[self.current_frame]
def update():
for animation in Animation.animations:
animation.time -= Time.delta_time
if animation.time <= 0:
animation.time = 1 / animation.fps
animation.next_frame()
def kill(self):
if self not in Animation.animations:
return
Animation.animations.remove(self)
del self
def get_frames(sprite,w,h):
frames = []
cells = int(sprite.get_width() / w)
for i in range(0,cells):
frame = sprite.subsurface(pygame.Rect(i * w,0,w,h))
frames.append(frame)
return frames