From 78c665af8b860063658d1782f75e072cfe88c82b Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Mon, 18 Mar 2024 14:16:37 +0200 Subject: area percent calculation --- src/sliceitoff/__main__.py | 2 +- src/sliceitoff/display/scaling.py | 4 ++-- src/sliceitoff/field/field.py | 31 ++++++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/sliceitoff/__main__.py b/src/sliceitoff/__main__.py index c4366e0..6845d94 100644 --- a/src/sliceitoff/__main__.py +++ b/src/sliceitoff/__main__.py @@ -19,7 +19,7 @@ def sliceitoff(): display = Display() Images.load_images( Path(__file__).parent.resolve() ) - level1 = Level(display = display, level = 100, score = 10) + level1 = Level(display = display, level = 10, score = 10) clock = pygame.time.Clock() dt = 0 diff --git a/src/sliceitoff/display/scaling.py b/src/sliceitoff/display/scaling.py index aad6c18..af307d1 100644 --- a/src/sliceitoff/display/scaling.py +++ b/src/sliceitoff/display/scaling.py @@ -27,10 +27,10 @@ class Scaling(): def scale_to_internal(coords: tuple ) -> tuple: """ Converts display coordinates to internal coodinates """ x = coords[0] - __class__.left - x = max(x, 0) / __class__.factor + x = max(x, 0) // __class__.factor x = min(x, INTERNAL_WIDTH - 1) y = coords[1] - __class__.top - y = max(y, 0) / __class__.factor + y = max(y, 0) // __class__.factor y = min(y, INTERNAL_HEIGHT - 1) return (x, y) diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py index 979590e..5881ed9 100644 --- a/src/sliceitoff/field/field.py +++ b/src/sliceitoff/field/field.py @@ -1,5 +1,9 @@ +import os + import pygame -from display import Scaling +from display import Scaling, INTERNAL_WIDTH, INTERNAL_HEIGHT + +DEBUG = os.getenv("DEBUG") class FieldSprite(pygame.sprite.Sprite): def __init__(self, area: tuple ): @@ -14,11 +18,17 @@ class FieldSprite(pygame.sprite.Sprite): class Field(): + initial_area = (400_000, 280_000) + def __init__(self): self.sprites = pygame.sprite.Group() - self.sprites.add(FieldSprite( (0, 0, 400_000, 280_000) )) + self.sprites.add(FieldSprite( (0, 0, *__class__.initial_area) )) + self.area_full = __class__.initial_area[0] * __class__.initial_area[1] + self.area_current = self.area_full + self.percentage = 100 def coordinates_inside_area(self, area, x, y) -> bool: + """ Test if coordinates are inside area """ if x < area[0]: return False if y < area[1]: @@ -28,8 +38,21 @@ class Field(): if y >= area[1]+area[3]: return False return True + + def update_area(self): + """ calculates remaining area and remaining percentage """ + self.area_current = sum( s.area[2]*s.area[3] for s in self.sprites ) + self.percentage = 100 * self.area_current // self.area_full + if DEBUG: + print( + f"FIELD: {self.area_full}/{self.area_current}, " + f"{self.percentage}") - def slice(self, pos: tuple, direction: bool, thickness: int) -> pygame.Rect: + def slice( + self, + pos: tuple, + direction: bool, + thickness: int) -> pygame.Rect: """ Slice one area into two areas """ # Find the overlapping area @@ -55,6 +78,7 @@ class Field(): self.sprites.add(FieldSprite( (x1, ay, x2-x1, ah) )) if x4 > x3: self.sprites.add(FieldSprite( (x3, ay, x4-x3, ah) )) + self.update_area() return Scaling.area_to_rect((x2, ay, x3-x2, ah)) y1 = ay y2 = pos[1] - thickness @@ -65,6 +89,7 @@ class Field(): self.sprites.add(FieldSprite( (ax, y1, aw, y2-y1) )) if y4 > y3: self.sprites.add(FieldSprite( (ax, y3, aw, y4-y3) )) + self.update_area() return Scaling.area_to_rect((ax, y2, aw, y3-y2)) def update(self, dt): -- cgit v1.2.3