This repository was archived by the owner on Sep 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets.py
More file actions
40 lines (32 loc) · 1.42 KB
/
Copy pathassets.py
File metadata and controls
40 lines (32 loc) · 1.42 KB
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
import pygame
import settings
class GameAssets:
def __init__(self) -> None:
self.images = {}
self.backgrounds = self.loadBackgrounds()
def loadImage(self, fileName):
if fileName in self.images:
return self.images[fileName]
imagePath = settings.RESOURCE_DIR / fileName
image = pygame.image.load(str(imagePath)).convert_alpha()
self.images[fileName] = image
return image
def loadScaledImage(self, fileName, targetWidth):
image = self.loadImage(fileName)
scale = targetWidth / image.get_width()
targetHeight = int(image.get_height() * scale)
return pygame.transform.smoothscale(image, (targetWidth, targetHeight))
def loadAnimationFrames(self, fileNames):
return [self.loadImage(fileName) for fileName in fileNames]
def loadBackgrounds(self):
backgrounds = []
for fileName in settings.BACKGROUND_FILES:
imagePath = settings.RESOURCE_DIR / fileName
background = pygame.image.load(str(imagePath)).convert()
if background.get_size() != (settings.WINDOW_WIDTH, settings.WINDOW_HEIGHT):
background = pygame.transform.smoothscale(
background,
(settings.WINDOW_WIDTH, settings.WINDOW_HEIGHT),
)
backgrounds.append(background)
return backgrounds