diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-16 11:11:56 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-16 11:11:56 +0200 |
commit | 85adc89f53763b4390f7e51161a05ab394143152 (patch) | |
tree | b63de708a75d7ea3fd961f8d63ca2af381cb66c5 | |
parent | 5d921239e0577988e2b7814025149d9160ecbdda (diff) |
drafting game components
-rw-r--r-- | src/sliceitoff/display/__init__.py | 7 | ||||
-rw-r--r-- | src/sliceitoff/display/display.py | 24 | ||||
-rw-r--r-- | src/sliceitoff/display/static.py | 11 | ||||
-rw-r--r-- | src/sliceitoff/field/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/field/field.py | 43 | ||||
-rw-r--r-- | src/sliceitoff/images/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/images/images.py | 19 | ||||
-rw-r--r-- | src/sliceitoff/main.py | 24 | ||||
-rw-r--r-- | src/sliceitoff/status/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/status/status.py | 45 |
10 files changed, 172 insertions, 4 deletions
diff --git a/src/sliceitoff/display/__init__.py b/src/sliceitoff/display/__init__.py index 6332319..296f519 100644 --- a/src/sliceitoff/display/__init__.py +++ b/src/sliceitoff/display/__init__.py @@ -1 +1,8 @@ from .display import Display +from .static import ( + Scaling, + INTERNAL_WIDTH, + INTERNAL_HEIGHT, + INTERNAL_ASPECT) + + diff --git a/src/sliceitoff/display/display.py b/src/sliceitoff/display/display.py index 151bb11..af61d49 100644 --- a/src/sliceitoff/display/display.py +++ b/src/sliceitoff/display/display.py @@ -1,16 +1,36 @@ import pygame +from .static import ( + Scaling, + INTERNAL_WIDTH, + INTERNAL_HEIGHT, + INTERNAL_ASPECT) + class Display(): def __init__(self): pygame.display.init() mode_info = pygame.display.Info() self.screen = pygame.display.set_mode( (mode_info.current_w, mode_info.current_h), - pygame.FULLSCREEN ) + pygame.FULLSCREEN | pygame.SCALED ) + + def get_scaling(self): + w, h = self.screen.get_size() + if w/h <= INTERNAL_ASPECT: + scale = w / INTERNAL_WIDTH + left = 0 + top = int((h - INTERNAL_HEIGHT * scale) / 2) + else : + scale = h / INTERNAL_HEIGHT + left = int((w - INTERNAL_WIDTH * scale) / 2) + top = 0 + return Scaling(scale, left, top) def update(self, groups = None): """ Updates the screen: clear, blit gropus and flip """ - self.screen.fill() + self.screen.fill("magenta") + for group in groups: + group.draw(self.screen) pygame.display.flip() def __del__(self): diff --git a/src/sliceitoff/display/static.py b/src/sliceitoff/display/static.py new file mode 100644 index 0000000..43dc385 --- /dev/null +++ b/src/sliceitoff/display/static.py @@ -0,0 +1,11 @@ +from dataclasses import dataclass + +INTERNAL_WIDTH = 40000 +INTERNAL_HEIGHT = 30000 +INTERNAL_ASPECT = INTERNAL_WIDTH / INTERNAL_HEIGHT + +@dataclass +class Scaling(): + scale: float + left: int + top: int diff --git a/src/sliceitoff/field/__init__.py b/src/sliceitoff/field/__init__.py new file mode 100644 index 0000000..2ae0139 --- /dev/null +++ b/src/sliceitoff/field/__init__.py @@ -0,0 +1 @@ +from .field import Field diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py new file mode 100644 index 0000000..c3d1d04 --- /dev/null +++ b/src/sliceitoff/field/field.py @@ -0,0 +1,43 @@ +import pygame +from display import Scaling + +class FieldSprite(pygame.sprite.Sprite): + def __init__(self, rect: pygame.Rect): + super().__init__() + self.rect = rect + self.image = pygame.Surface(self.rect.size) + self.image.fill("green") + + +class Field(): + def __init__(self, scaling: Scaling = None): + self.scaling = scaling if scaling else Scaling(50,0,0) + self.sprites = pygame.sprite.Group() + self.updated = True + self.areas = [(0,0,40_000,28_000)] + + def area_to_rect(self, area): + x, y, w, h = area + return pygame.Rect( + x * self.scaling.scale + self.scaling.left, + y * self.scaling.scale + self.scaling.top, + w * self.scaling.scale, + h * self.scaling.scale) + + def __update_sprites(self): + self.sprites.empty() + for area in self.areas: + self.sprites.add(FieldSprite(self.area_to_rect(area))) + + def add_score(self, score_to_add): + self.updated = True + self.score += score_to_add + + def get_sprites(self): + if self.updated: + self.__update_sprites() + return self.sprites + + def __del__(self): + pass +
\ No newline at end of file diff --git a/src/sliceitoff/images/__init__.py b/src/sliceitoff/images/__init__.py new file mode 100644 index 0000000..16d361a --- /dev/null +++ b/src/sliceitoff/images/__init__.py @@ -0,0 +1 @@ +from .images import images, load_images diff --git a/src/sliceitoff/images/images.py b/src/sliceitoff/images/images.py new file mode 100644 index 0000000..102ca0f --- /dev/null +++ b/src/sliceitoff/images/images.py @@ -0,0 +1,19 @@ +import os +import pygame + +DEBUG = os.getenv("DEBUG") + +images = {} + +def load_images(scale): + if not images: + with open("assets/images.lst") as image_list_file: + for line in image_list_file: + name, *path = line.strip().split() + filename = os.path.join(*path) + if DEBUG: + print(f"Loading images {name = }, {filename = }") + image = pygame.image.load(filename) + images[name] = pygame.transform.smoothscale_by( + pygame.Surface.convert_alpha(image), + scale) diff --git a/src/sliceitoff/main.py b/src/sliceitoff/main.py index 5f1f3ce..6733466 100644 --- a/src/sliceitoff/main.py +++ b/src/sliceitoff/main.py @@ -5,11 +5,31 @@ from pygame import ( from time import sleep from display import Display +from status import Status +from field import Field +from images import images, load_images def main(): pg_init() - d = Display() - sleep(4) + + display = Display() + scaling = display.get_scaling() + load_images(scaling.scale) + + status = Status(scaling=scaling) + field = Field(scaling=scaling) + + print(scaling) + + display.update( + [ + status.get_sprites(), + field.get_sprites() + ]) + + sleep(2) pg_quit() + + main()
\ No newline at end of file diff --git a/src/sliceitoff/status/__init__.py b/src/sliceitoff/status/__init__.py new file mode 100644 index 0000000..2a4c93e --- /dev/null +++ b/src/sliceitoff/status/__init__.py @@ -0,0 +1 @@ +from .status import Status
\ No newline at end of file diff --git a/src/sliceitoff/status/status.py b/src/sliceitoff/status/status.py new file mode 100644 index 0000000..afff7f7 --- /dev/null +++ b/src/sliceitoff/status/status.py @@ -0,0 +1,45 @@ +import os +import pygame + +from display import Scaling +from images import images + +class LetterSprite(pygame.sprite.Sprite): + def __init__(self, image, x, y): + super().__init__() + self.image = image + self.rect = self.image.get_rect().move(x,y) + + +class Status(): + def __init__(self, scaling: Scaling = None): + self.scaling = scaling if scaling else Scaling(50,0,0) + self.score = 6234823999 + self.health = 3 + self.updated = True + self.sprites = pygame.sprite.Group() + + + def __update_sprites(self): + self.sprites.empty() + y = 28000 + score_str="{:010d}".format(self.score) + for letter, x in zip(score_str, range(30000,40000,1000)): + self.sprites.add(LetterSprite( + images[f"letter_{letter}"], + x*self.scaling.scale+self.scaling.left, + y*self.scaling.scale+self.scaling.top)) + self.updated = False + + def add_score(self, score_to_add): + self.updated = True + self.score += score_to_add + + def get_sprites(self): + if self.updated: + self.__update_sprites() + return self.sprites + + def __del__(self): + pass +
\ No newline at end of file |