forked from YungGinger22/SREM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.py
46 lines (38 loc) · 1.1 KB
/
assets.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
import os
import pygame.image
images = {} # The cache of images
images_dir = "pixel"
images_dir = "svg"
def load_image(path):
filepath = "assets" + os.sep + images_dir + os.sep + path
if not filepath in images:
# If the image is not cached load it and add to the cache:
try:
image = pygame.image.load(filepath).convert_alpha()
images[filepath] = image
return image
except Exception as e:
print("Cant load image: " + filepath)
raise e
else:
# Else just return the cached image:
return images[filepath]
fonts = {}
def load_font(path, size):
filepath = "assets" + os.sep + path
key = filepath + str(size)
if not key in fonts:
font = pygame.font.Font(filepath, size)
fonts[key] = font
return font
else:
return fonts[key]
sounds = {}
def load_sound(path):
filepath = "assets" + os.sep + path
if not path in sounds:
sound = pygame.mixer.Sound(filepath)
sounds[filepath] = sound
return sound
else:
return sounds[filepath]