diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-16 12:53:03 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-16 12:53:03 +0200 |
commit | 77d104cced553b8dfc9896cb456bd039807ed6c6 (patch) | |
tree | 834731b22475fe09ec61168ec220ff6ae98259bc | |
parent | a23b6647b0ca2cd9f2ccbbe12360c3fa0f96a554 (diff) |
Images class
-rw-r--r-- | src/sliceitoff/images/__init__.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/images/images.py | 22 | ||||
-rw-r--r-- | src/sliceitoff/main.py | 12 | ||||
-rw-r--r-- | src/sliceitoff/status/status.py | 8 |
4 files changed, 26 insertions, 18 deletions
diff --git a/src/sliceitoff/images/__init__.py b/src/sliceitoff/images/__init__.py index 16d361a..d87dfa0 100644 --- a/src/sliceitoff/images/__init__.py +++ b/src/sliceitoff/images/__init__.py @@ -1 +1 @@ -from .images import images, load_images +from .images import Images diff --git a/src/sliceitoff/images/images.py b/src/sliceitoff/images/images.py index b2d93a2..999d92b 100644 --- a/src/sliceitoff/images/images.py +++ b/src/sliceitoff/images/images.py @@ -5,10 +5,12 @@ from display import Scaling DEBUG = os.getenv("DEBUG") -images = {} +class Images: + surfaces = {} -def load_images(): - if not images: + @staticmethod + def reload_images(): + __class__.surfaces = {} with open("assets/images.lst") as image_list_file: for line in image_list_file: name, *path = line.strip().split() @@ -16,6 +18,14 @@ def load_images(): if DEBUG: print(f"Loading images {name = }, {filename = }") image = pygame.image.load(filename) - images[name] = pygame.transform.smoothscale_by( - pygame.Surface.convert_alpha(image), - Scaling.factor) + rgba = pygame.Surface.convert_alpha(image) + scaled = pygame.transform.smoothscale_by(rgba, Scaling.factor) + __class__.surfaces[name] = scaled + return True + + @staticmethod + def load_images(): + if __class__.surfaces: + return False + return __class__.reload_images() +
\ No newline at end of file diff --git a/src/sliceitoff/main.py b/src/sliceitoff/main.py index 3632f41..d4fb0bc 100644 --- a/src/sliceitoff/main.py +++ b/src/sliceitoff/main.py @@ -7,19 +7,19 @@ from time import sleep from display import Display from status import Status from field import Field -from images import images, load_images +from images import Images def main(): pg_init() display = Display() - load_images() + Images.load_images() status = Status() field = Field() - field.slice(20_000,20_000,True,1_000) - field.slice(10_000,20_000,False,1_000) + field.slice(20_000, 20_000, True, 1_000) + field.slice(10_000, 20_000, False, 1_000) display.update( [ @@ -30,6 +30,4 @@ def main(): sleep(2) pg_quit() - - -main()
\ No newline at end of file +main() diff --git a/src/sliceitoff/status/status.py b/src/sliceitoff/status/status.py index ed36711..b2bde21 100644 --- a/src/sliceitoff/status/status.py +++ b/src/sliceitoff/status/status.py @@ -2,7 +2,7 @@ import os import pygame from display import Scaling -from images import images +from images import Images class LetterSprite(pygame.sprite.Sprite): def __init__(self, image, pos): @@ -21,11 +21,11 @@ class Status(): def __update_sprites(self): self.sprites.empty() - y = 28000 + y = 28_000 score_str="{:010d}".format(self.score) - for letter, x in zip(score_str, range(30000,40000,1000)): + for letter, x in zip(score_str, range(30_000,40_000,1_000)): self.sprites.add(LetterSprite( - images[f"letter_{letter}"], + Images.surfaces[f"letter_{letter}"], Scaling.scale_coordinates((x,y)) )) self.updated = False |