-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
534 lines (401 loc) · 17 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import pygame
import os
import random
def _not_looping(key: int):
print(f"This function should not be called! Key: {key}")
DEFAULT_COLOR = (50, 50, 50)
class App:
"""Subclass this to make a game! Override function `ready` if you want something to run before the loop starts. Also, be sure to set the `loop_func` later."""
def __init__(
self, *,
title: str = "game",
logo_filename: str = None,
size: tuple = (960, 720),
framerate: int = 60,
show_fps: bool = True,
run_dir: str = None,
asset_subdir: str = "assets",
image_subdir: str = "images",
sound_subdir: str = "sounds",
font_subdir: str = "fonts",
):
self.title = title
self.logo_filename = logo_filename
self.width, self.height = self.size = size
self.framerate = framerate
self.show_fps = show_fps
if run_dir:
if os.path.isdir(run_dir):
self.run_dir = run_dir
else:
raise FileNotFoundError("Provided `run_dir` does not exist")
else:
self.run_dir = os.path.dirname(__file__)
self.asset_dir = os.path.join(self.run_dir, asset_subdir)
self.image_subdir = image_subdir
self.sound_subdir = sound_subdir
self.font_subdir = font_subdir
self.texts = []
self.blits = []
self.teams = []
# used for string-referencing scenes and sounds that have not been initialized yet
self.scenes = {}
self.sounds = {}
self.first_scene = None # YOU NEED TO SET THIS!
self.current_bgm = None
self.SCENE_QUIT = SceneQuit(self)
self.scenes["QUIT"] = self.SCENE_QUIT
self.loop_func = _not_looping
def get_asset(self, *relative_path):
return os.path.join(self.asset_dir, *relative_path)
def load_image(self, filename: str) -> pygame.Surface:
return pygame.image.load(os.path.join(self.asset_dir, self.image_subdir, filename))
def load_font(self, filename: str, size: int = 50) -> pygame.font.Font:
return pygame.font.Font(os.path.join(self.asset_dir, self.font_subdir, filename), size)
def load_sound(self, filename: str, name: str = None, *, vol: float = None) -> pygame.mixer.Sound:
sound = pygame.mixer.Sound(os.path.join(self.asset_dir, self.sound_subdir, filename))
if name: self.sounds[name] = sound
if vol: sound.set_volume(vol)
return sound
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key:
return event.key
def text(self, content: str, color: tuple = None, background: tuple = None, font: pygame.font.Font = None, persist: bool = True):
for text in self.texts:
if text.content == content and ((not color) or text.color == color) and ((not background) or text.background == background) and ((not font) or text.font == font):
return text
new_text = Text(self, content, color=color, background=background, font=font)
if persist:
self.texts.append(new_text)
return new_text
def get_sound(self, sound):
if isinstance(sound, pygame.mixer.Sound):
return sound
elif isinstance(sound, str):
if sound in self.sounds:
return self.sounds[sound]
else:
raise Exception(f"Sound '{sound}' not found in `sounds`")
else:
raise TypeError(f"Argument for `get_sound` must be of type `str` or `pygame.mixer.Sound`, not '{type(sound)}' (you said '{sound}')")
def play_music(self, sound):
sound = self.get_sound(sound)
if isinstance(self.current_bgm, pygame.mixer.Sound):
self.current_bgm.stop()
self.current_bgm = sound
sound.play(-1)
def play_scene(self, scene, ready: bool = True):
if isinstance(scene, str):
if scene in self.scenes:
scene = self.scenes[scene]
else:
raise Exception(f"Scene '{scene}' not found in `scenes`")
if isinstance(scene, Scene):
if ready:
scene.ready()
if hasattr(scene, "_ready"):
scene._ready()
self.loop_func = scene.loop
else:
raise TypeError(f"Argument for `play_scene` must be of type `str` or `Scene`, not '{type(scene)}' (you said '{scene}')")
def ready(self):
"""Override this function with your game's init stuff"""
pass
def opt_quit(self, key):
self.running = False
def run(self):
"""This starts the game, DO NOT override this function!"""
pygame.init()
pygame.mixer.init()
# init default assets
self.font_main = self.font_default = pygame.font.Font(None, 42)
if self.logo_filename:
self.logo = self.load_image(self.logo_filename)
pygame.display.set_icon(self.logo)
pygame.display.set_caption(self.title)
# oh lol!
self.ready()
self.screen = pygame.display.set_mode(self.size)
self.clock = pygame.time.Clock()
self.running = True
self.play_scene(self.first_scene)
# GAME LOOP
while self.running:
key = self.check_events()
self.loop_func(key)
if self.show_fps:
fps = int(self.clock.get_fps())
color = tuple([random.randint(0, 255) for _ in range(3)])
fps_text = self.font_main.render(str(fps), True, color, (0, 0, 0))
self.screen.blit(fps_text, (5, 5))
self.clock.tick(self.framerate)
pygame.display.flip()
class Team:
def __init__(self, app: App, name: str, members: list):
self.app = app
self.name = name
self.members = members
self.enable()
def enable(self):
if self not in self.app.teams:
self.app.teams.append(self)
return True
return False
def disable(self):
if self in self.app.teams:
self.app.teams.remove(self)
return True
return False
def add_member(self, member):
self.members.append(member)
def add_members(self, members: list):
for member in members:
self.add_member(member)
class Scene:
"""Subclass this to make a scene. Override `ready` and `loop`."""
def __init__(self, app: App, name: str = None):
self.app = app
# this stores the object inside the App's scene list, allowing it to be accessed as a string, meaning no errors unless you code it wrong!
if name: self.app.scenes[name] = self
self.blits = []
self.initialized = False
def ready(self):
"""Override this function with your Scene's init process"""
pass
def loop(self, key: int):
"""Override this function with your Scene's loop, including argument `key` of type `int`"""
pass
class Menu(Scene):
def __init__(
self, app: App, name: str, names: tuple[str], scenes: tuple[str|Scene], *,
sound_change: pygame.mixer.Sound = None, sound_select: pygame.mixer.Sound = None, sound_exit: pygame.mixer.Sound = None,
key_forward: int|tuple[int] = pygame.K_DOWN, key_backward: int|tuple[int] = pygame.K_UP, key_select: int|tuple[int] = pygame.K_RETURN,
key_exit: int|tuple[int] = None, parent: str|Scene = None
):
super().__init__(app, name)
self.names = names
self.scenes = scenes
self.sound_change = sound_change
self.sound_select = sound_select
self.sound_exit = sound_exit
self.key_forward = key_forward
self.key_backward = key_backward
self.key_select = key_select
self.key_exit = key_exit
self.parent = parent
if len(self.names) != len(self.scenes):
raise Exception("`names` and `scenes` must be tuples of the same length")
def _ready(self):
"""Override `ready` instead!"""
self.selected = 0
def ready(self):
"""Override this to run things (like music) before your menu!"""
pass
def loop(self, key: int):
"""Don't override this unless you know what you're doing!"""
if key == self.key_forward:
if self.sound_change:
self.app.get_sound(self.sound_change).play()
self.selected += 1
elif key == self.key_backward:
if self.sound_change:
self.app.get_sound(self.sound_change).play()
self.selected -= 1
elif key == self.key_select:
if self.sound_select:
self.app.get_sound(self.sound_select).play()
self.app.play_scene(self.scenes[self.selected])
self.selected = 0
elif key == self.key_exit and self.parent:
if self.sound_exit:
self.app.get_sound(self.sound_exit).play()
self.app.play_scene(self.parent)
self.selected = 0
if self.selected > len(self.names) - 1:
self.selected = 0
elif self.selected < 0:
self.selected = len(self.names) - 1
self.render()
def render(self):
"""Override this to render the menu, or leave as default"""
self.app.screen.fill((250, 250, 250))
for i in range(len(self.names)):
if i == self.selected:
color = (0, 150, 40)
else:
color = (0, 0, 0)
text = self.app.text(self.names[i], color=color)
text.blit((20, 20))
class ObstacleScene(Scene):
def __init__(self, app: App, name: str, obstacles: list):
super().__init__(app, name)
self.obstacles = obstacles
def load(self):
for obstacle in self.obstacles:
obstacle.enable()
def unload(self):
for obstacle in self.obstacles:
obstacle.disable()
class SceneQuit(Scene):
def __init__(self, app: App):
self.app = app
def loop(self):
self.app.running = False
class Positional:
"""Class used for items that have their own x and y positions"""
def __init__(self, app: App, *, pos: tuple = (1, 1)):
# if isinstance(app, Scene):
# app = app.app
# ^^^ this might be too nice, keep it commented for now
self.app = app
self.x, self.y = pos
self.hidden = False
def blit(self, pos: tuple = None):
"""Blits the Positional object to the screen where it is"""
if not self.hidden:
if not pos:
pos = self.x, self.y
self.app.screen.blit(self.object, pos)
class Text(Positional):
"""Text object that can be reused"""
def __init__(self, app: App, content: str, *, pos: tuple = (1, 1), color: tuple = (30, 10, 100), background: tuple = None, font: pygame.font.Font = None, antialias: bool = True):
super().__init__(app, pos=pos)
self.content = content
self.color = color
self.background = background
if font:
self.font = font
else:
self.font = self.app.font_main
self.object = self.font.render(self.content, antialias, self.color, self.background)
class Sprite(Positional):
"""Sprites have their own x and y positions and have a function to limit them to the screen edges"""
def __init__(self, app: App, *, image: str = None, color: tuple = DEFAULT_COLOR, size: tuple = (20, 20), pos: tuple = (1, 1)):
super().__init__(app, pos=pos)
if image:
self.object = self.image = self.app.load_image(image)
self.width = self.image.get_width()
self.height = self.image.get_height()
elif color and size:
self.rect = pygame.Rect(pos[0], pos[1], size[0], size[1])
self.object = pygame.Surface((self.rect.w, self.rect.h))
self.width, self.height = size
else:
raise Exception("Sprite must be initialized with either an `image` or a `color` and `size`")
self.size = self.width, self.height
self.max_x = self.app.width - self.width
self.max_y = self.app.height - self.height
def get_right(self):
return self.x + self.width
def get_bottom(self):
return self.y + self.height
def restrict(self):
"""If the Sprite has gone beyond the screen edges, teleport it back to the edge"""
if self.x < 0:
self.x = 0
elif self.x > self.max_x:
self.x = self.max_x
if self.y < 0:
self.y = 0
elif self.y > self.max_y:
self.y = self.max_y
def move(self, xd: int = 0, yd: int = 0, *, check_collision: bool = True):
new_x = self.x + xd
new_y = self.y + yd
new_r = self.get_right() + xd
new_b = self.get_bottom() + yd
change = True
if check_collision:
for team in self.app.teams:
if self in team.members:
for member in team.members:
if isinstance(member, Obstacle):
obs = member
obs_r = obs.get_right()
obs_b = obs.get_bottom()
if (
((new_x < obs_r and new_x > obs.x) or
(new_r > obs.x and new_r < obs_r)) and
((new_y > obs.y and new_y < obs_b) or
(new_b > obs.y and new_b < obs_b))
):
change = False
if change:
self.x = new_x
self.y = new_y
def emit(self, projectile: "Projectile"):
"""Emits Projectiles"""
projectile.x, projectile.y = self.x, self.y
class Projectile(Sprite):
"""Projectiles are Sprites that move on their own, with the `velocity` parameter"""
def __init__(self, app: App, *, image: str, pos: tuple = (1, 1), velocity: tuple = (0, 0), bounce: bool = False):
super().__init__(app, image=image, pos=pos)
self.vel_x, self.vel_y = velocity
self.vel_x_start, self.vel_y_start = self.vel_x, self.vel_y
self.bounce = bounce
def step(self):
"""Moves the Projectile in a step using its `velocity`"""
self.x += self.vel_x
self.y += self.vel_y
if self.bounce:
self.restrict()
if self.x == 0:
self.vel_x = abs(self.vel_x)
elif self.x == self.max_x:
self.vel_x = -abs(self.vel_x)
if self.y == 0:
self.vel_y = abs(self.vel_y)
elif self.y == self.max_y:
self.vel_y = -abs(self.vel_y)
else:
if self.x > self.max_x+5 or self.x < -self.width-5 or self.y > self.max_y+5 or self.y < -self.height-5:
del self
class Bullet(Projectile):
"""Bullets are used by players and enemies"""
def __init__(self, app: App, shooter: Sprite, *, image: str, pos: tuple = None, velocity: tuple = (0, 0), bounce: bool = False, targets: tuple = None, hits: int = 1, damage: int = None):
if not pos:
pos = shooter.x, shooter.y
super().__init__(app, image=image, pos=pos, velocity=velocity, bounce=bounce)
self.shooter = shooter
self.targets = targets
self.damage = damage
self.hits = hits
def check(self):
"""Checks if the Bullet hit something"""
for blit in self.app.blits:
if self.x - 5 < blit.x < self.x + 5 and self.y - 5 < blit.y < self.y + 5:
if (not self.targets or blit in self.targets) and blit != self.shooter and blit != self:
if hasattr(blit, "take_damage"):
blit.take_damage(self.damage)
if self.hits > -1:
self.hits -= 1
if self.hits <= 0:
del self
class Obstacle(Sprite):
"""Obstacles are solid objects that block other Sprites"""
def __init__(self, scene: Scene, *, color: tuple = DEFAULT_COLOR, size: tuple = (20, 20), pos: tuple = (1, 1), ):
super().__init__(scene.app, color=color, size=size, pos=pos)
def KEYS_move(keys: list, player: Sprite, step: int):
if keys[pygame.K_UP]:
#player.y -= step
player.move(0, -step)
if keys[pygame.K_DOWN]:
#player.y += step
player.move(0, step)
if keys[pygame.K_LEFT]:
#player.x -= step
player.move(-step)
if keys[pygame.K_RIGHT]:
#player.x += step
player.move(step)
def AUTO_blit(blits: list):
for item in blits:
if isinstance(item, Bullet):
item.step()
item.check()
if hasattr(item, "blit"):
item.blit()