-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.py
64 lines (42 loc) · 1.86 KB
/
support.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
import os
import pygame
from pathlib import Path
BASE_IMG_PATH = 'data/images/'
Sound = pygame.mixer.Sound
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 copy(self):
return Animation(self.images, self.img_duration, self.loop)
def update(self):
num_frames = self.img_duration * len(self.images)
self.frame = (self.frame + 1) % num_frames if self.loop else min(self.frame + 1, num_frames - 1)
self.done = self.frame >= num_frames - 1 if not self.loop else False
def current_sprite(self):
return self.images[int(self.frame / self.img_duration)]
def load_image(path):
sprite = pygame.image.load(BASE_IMG_PATH + path)
sprite.set_colorkey((0, 0, 0))
return sprite
def load_images(path):
return [load_image(path + '/' + name).convert_alpha() for name in sorted(os.listdir(BASE_IMG_PATH + path))]
def load_images_entities(path, trim_left=0, trim_right=0, scale_factor=0.85):
sprites = []
for sprite_path in sorted(Path(BASE_IMG_PATH + path).glob("*")):
sprite = pygame.image.load(str(sprite_path)).convert_alpha()
sprite.set_colorkey((0, 0, 0))
sprite = sprite.subsurface(trim_left, 0, sprite.get_width() - trim_left - trim_right, sprite.get_height())
sprite = pygame.transform.scale(sprite, (int(sprite.get_width() * scale_factor), int(sprite.get_height() * scale_factor)))
sprites.append(sprite)
return sprites
def antialiasing(a, b, t):
return a + (b - a) * t
def render_scroll(coord):
return tuple((int(i) for i in coord))
def volume_adjusting(sfx: dict[str, Sound], volume_settings: dict[str, float]):
for sound, volume in volume_settings.items():
sfx[sound].set_volume(volume)