summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-16 12:35:07 +0200
committerViljami Ilola <+@hix.fi>2024-03-16 12:35:07 +0200
commita23b6647b0ca2cd9f2ccbbe12360c3fa0f96a554 (patch)
treebab154622f9767fd340278ab275babb76fc569d5 /src
parent1ad8357f0039292b929e66cf703b4314660aded2 (diff)
scaling as class
Diffstat (limited to 'src')
-rw-r--r--src/sliceitoff/display/__init__.py7
-rw-r--r--src/sliceitoff/display/display.py19
-rw-r--r--src/sliceitoff/display/scaling.py36
-rw-r--r--src/sliceitoff/display/static.py9
-rw-r--r--src/sliceitoff/field/field.py51
-rw-r--r--src/sliceitoff/images/images.py6
-rw-r--r--src/sliceitoff/main.py12
-rw-r--r--src/sliceitoff/status/status.py10
8 files changed, 89 insertions, 61 deletions
diff --git a/src/sliceitoff/display/__init__.py b/src/sliceitoff/display/__init__.py
index 296f519..25b6f18 100644
--- a/src/sliceitoff/display/__init__.py
+++ b/src/sliceitoff/display/__init__.py
@@ -1,8 +1,5 @@
+from .scaling import Scaling
from .display import Display
-from .static import (
- Scaling,
- INTERNAL_WIDTH,
- INTERNAL_HEIGHT,
- INTERNAL_ASPECT)
+from .static import INTERNAL_WIDTH, INTERNAL_HEIGHT
diff --git a/src/sliceitoff/display/display.py b/src/sliceitoff/display/display.py
index af61d49..ae98134 100644
--- a/src/sliceitoff/display/display.py
+++ b/src/sliceitoff/display/display.py
@@ -1,10 +1,6 @@
import pygame
-from .static import (
- Scaling,
- INTERNAL_WIDTH,
- INTERNAL_HEIGHT,
- INTERNAL_ASPECT)
+from .scaling import Scaling
class Display():
def __init__(self):
@@ -13,18 +9,7 @@ class Display():
self.screen = pygame.display.set_mode(
(mode_info.current_w, mode_info.current_h),
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)
+ Scaling.update_scaling(self.screen.get_size())
def update(self, groups = None):
""" Updates the screen: clear, blit gropus and flip """
diff --git a/src/sliceitoff/display/scaling.py b/src/sliceitoff/display/scaling.py
new file mode 100644
index 0000000..6be00da
--- /dev/null
+++ b/src/sliceitoff/display/scaling.py
@@ -0,0 +1,36 @@
+import pygame
+
+from .static import INTERNAL_WIDTH, INTERNAL_HEIGHT
+
+class Scaling():
+ factor = 0.02
+ left = 0
+ top = 0
+
+ @staticmethod
+ def area_to_rect(area: tuple) -> pygame.Rect:
+ """ converts area coordinates to pygame.Rect"""
+ return pygame.Rect(
+ area[0] * __class__.factor + __class__.left,
+ area[1] * __class__.factor + __class__.top,
+ area[2] * __class__.factor,
+ area[3] * __class__.factor)
+
+ @staticmethod
+ def scale_coordinates(coords: tuple ) -> tuple:
+ return (
+ coords[0] * __class__.factor + __class__.left,
+ coords[1] * __class__.factor + __class__.top)
+
+ @staticmethod
+ def update_scaling(size: tuple) -> None:
+ if size[0] / size[1] <= INTERNAL_WIDTH / INTERNAL_HEIGHT:
+ __class__.factor = size[0] / INTERNAL_WIDTH
+ __class__.left = 0
+ __class__.top = (size[1] - INTERNAL_HEIGHT * __class__.factor) // 2
+ else:
+ __class__.factor = size[1] / INTERNAL_HEIGHT
+ __class__.left = (size[0] - INTERNAL_WIDTH * __class__.factor) // 2
+ __class__.top = 0
+
+ \ No newline at end of file
diff --git a/src/sliceitoff/display/static.py b/src/sliceitoff/display/static.py
index 43dc385..6e5d0dc 100644
--- a/src/sliceitoff/display/static.py
+++ b/src/sliceitoff/display/static.py
@@ -1,11 +1,2 @@
-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/field.py b/src/sliceitoff/field/field.py
index c7ebf81..0681c64 100644
--- a/src/sliceitoff/field/field.py
+++ b/src/sliceitoff/field/field.py
@@ -10,21 +10,12 @@ class FieldSprite(pygame.sprite.Sprite):
class Field():
- def __init__(self, scaling: Scaling = None):
- self.scaling = scaling if scaling else Scaling(50,0,0)
+ def __init__(self):
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 coordinates_inside_area(self, area, x, y):
+ def coordinates_inside_area(self, area, x, y) -> bool:
if x < area[0]:
return False
if y < area[1]:
@@ -34,24 +25,52 @@ class Field():
if y >= area[1]+area[3]:
return False
return True
-
- def slice(self, x: int, y: int, direction: bool, thickness: int):
+
+ def slice(self, x: int, y: int, direction: bool, thickness: int) -> bool:
""" Slice one area into two areas """
+
# Find the overlapping area
for area in self.areas:
if self.coordinates_inside_area(area, x, y):
break
else:
return False
+
+ # remove current area
self.areas.remove(area)
- return False
+ ax, ay, aw, ah = area
+
+ # create new areas if there is any space
+ if direction:
+ x1 = ax
+ x2 = x - thickness
+ x3 = x + thickness
+ x4 = ax + aw
+
+ if x2 > x1:
+ self.areas.append( (x1, ay, x2-x1, ah) )
+ if x4 > x3:
+ self.areas.append( (x3, ay, x4-x3, ah) )
+ else:
+ y1 = ay
+ y2 = y - thickness
+ y3 = y + thickness
+ y4 = ay + ah
+
+ if y2 > y1:
+ self.areas.append( (ax, y1, aw, y2-y1) )
+ if y4 > y3:
+ self.areas.append( (ax, y3, aw, y4-y3) )
+ self.updated = True
+ return True
def __update_sprites(self):
self.sprites.empty()
for area in self.areas:
- self.sprites.add(FieldSprite(self.area_to_rect(area)))
+ self.sprites.add(FieldSprite(Scaling.area_to_rect(area)))
+ self.updated = False
- def get_sprites(self):
+ def get_sprites(self) -> pygame.sprite.Group:
if self.updated:
self.__update_sprites()
return self.sprites
diff --git a/src/sliceitoff/images/images.py b/src/sliceitoff/images/images.py
index 102ca0f..b2d93a2 100644
--- a/src/sliceitoff/images/images.py
+++ b/src/sliceitoff/images/images.py
@@ -1,11 +1,13 @@
import os
import pygame
+from display import Scaling
+
DEBUG = os.getenv("DEBUG")
images = {}
-def load_images(scale):
+def load_images():
if not images:
with open("assets/images.lst") as image_list_file:
for line in image_list_file:
@@ -16,4 +18,4 @@ def load_images(scale):
image = pygame.image.load(filename)
images[name] = pygame.transform.smoothscale_by(
pygame.Surface.convert_alpha(image),
- scale)
+ Scaling.factor)
diff --git a/src/sliceitoff/main.py b/src/sliceitoff/main.py
index d604d0b..3632f41 100644
--- a/src/sliceitoff/main.py
+++ b/src/sliceitoff/main.py
@@ -13,13 +13,13 @@ def main():
pg_init()
display = Display()
- scaling = display.get_scaling()
- load_images(scaling.scale)
+ load_images()
- status = Status(scaling=scaling)
- field = Field(scaling=scaling)
-
- print(scaling)
+ status = Status()
+ field = Field()
+
+ field.slice(20_000,20_000,True,1_000)
+ field.slice(10_000,20_000,False,1_000)
display.update(
[
diff --git a/src/sliceitoff/status/status.py b/src/sliceitoff/status/status.py
index afff7f7..ed36711 100644
--- a/src/sliceitoff/status/status.py
+++ b/src/sliceitoff/status/status.py
@@ -5,15 +5,14 @@ from display import Scaling
from images import images
class LetterSprite(pygame.sprite.Sprite):
- def __init__(self, image, x, y):
+ def __init__(self, image, pos):
super().__init__()
self.image = image
- self.rect = self.image.get_rect().move(x,y)
+ self.rect = self.image.get_rect().move(pos)
class Status():
- def __init__(self, scaling: Scaling = None):
- self.scaling = scaling if scaling else Scaling(50,0,0)
+ def __init__(self):
self.score = 6234823999
self.health = 3
self.updated = True
@@ -27,8 +26,7 @@ class Status():
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))
+ Scaling.scale_coordinates((x,y)) ))
self.updated = False
def add_score(self, score_to_add):