summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-16 15:11:12 +0200
committerViljami Ilola <+@hix.fi>2024-03-16 15:11:12 +0200
commit3d1e8a068f98a32e0146d8e9ab58dea49fbb4c74 (patch)
tree013814df21967af7dc9dfb390fd6de9770799616
parent77d104cced553b8dfc9896cb456bd039807ed6c6 (diff)
controls and gamelogic draft
-rw-r--r--src/sliceitoff/display/scaling.py15
-rw-r--r--src/sliceitoff/field/field.py12
-rw-r--r--src/sliceitoff/game/__init__.py1
-rw-r--r--src/sliceitoff/game/game.py20
-rw-r--r--src/sliceitoff/main.py34
-rw-r--r--src/sliceitoff/player/__init__.py1
-rw-r--r--src/sliceitoff/player/player.py58
-rw-r--r--src/sliceitoff/status/status.py2
8 files changed, 121 insertions, 22 deletions
diff --git a/src/sliceitoff/display/scaling.py b/src/sliceitoff/display/scaling.py
index 6be00da..aad6c18 100644
--- a/src/sliceitoff/display/scaling.py
+++ b/src/sliceitoff/display/scaling.py
@@ -17,12 +17,24 @@ class Scaling():
area[3] * __class__.factor)
@staticmethod
- def scale_coordinates(coords: tuple ) -> tuple:
+ def scale_to_display(coords: tuple ) -> tuple:
+ """ Converts internal coordinates to display coodinates """
return (
coords[0] * __class__.factor + __class__.left,
coords[1] * __class__.factor + __class__.top)
@staticmethod
+ 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 = min(x, INTERNAL_WIDTH - 1)
+ y = coords[1] - __class__.top
+ y = max(y, 0) / __class__.factor
+ y = min(y, INTERNAL_HEIGHT - 1)
+ return (x, y)
+
+ @staticmethod
def update_scaling(size: tuple) -> None:
if size[0] / size[1] <= INTERNAL_WIDTH / INTERNAL_HEIGHT:
__class__.factor = size[0] / INTERNAL_WIDTH
@@ -32,5 +44,4 @@ class Scaling():
__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/field/field.py b/src/sliceitoff/field/field.py
index 0681c64..13b5c6a 100644
--- a/src/sliceitoff/field/field.py
+++ b/src/sliceitoff/field/field.py
@@ -26,12 +26,12 @@ class Field():
return False
return True
- def slice(self, x: int, y: int, direction: bool, thickness: int) -> bool:
+ def slice(self, pos: tuple, 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):
+ if self.coordinates_inside_area(area, pos[0], pos[1]):
break
else:
return False
@@ -43,8 +43,8 @@ class Field():
# create new areas if there is any space
if direction:
x1 = ax
- x2 = x - thickness
- x3 = x + thickness
+ x2 = pos[0] - thickness
+ x3 = pos[0] + thickness
x4 = ax + aw
if x2 > x1:
@@ -53,8 +53,8 @@ class Field():
self.areas.append( (x3, ay, x4-x3, ah) )
else:
y1 = ay
- y2 = y - thickness
- y3 = y + thickness
+ y2 = pos[1] - thickness
+ y3 = pos[1] + thickness
y4 = ay + ah
if y2 > y1:
diff --git a/src/sliceitoff/game/__init__.py b/src/sliceitoff/game/__init__.py
new file mode 100644
index 0000000..a2771db
--- /dev/null
+++ b/src/sliceitoff/game/__init__.py
@@ -0,0 +1 @@
+from .game import Game
diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py
new file mode 100644
index 0000000..789edd7
--- /dev/null
+++ b/src/sliceitoff/game/game.py
@@ -0,0 +1,20 @@
+import pygame
+
+class Game:
+ def __init__(self, player = None, field = None):
+ self.player = player
+ self.field = field
+
+ def step(self):
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ return True
+ if event.type == pygame.MOUSEMOTION:
+ self.player.set_position(pygame.mouse.get_pos())
+ if event.type == pygame.MOUSEBUTTONDOWN:
+ self.player.set_position(pygame.mouse.get_pos())
+ if event.button == 1:
+ self.player.set_lazer()
+ if event.button == 3:
+ self.player.set_direction()
+ return False
diff --git a/src/sliceitoff/main.py b/src/sliceitoff/main.py
index d4fb0bc..1e84d28 100644
--- a/src/sliceitoff/main.py
+++ b/src/sliceitoff/main.py
@@ -1,33 +1,41 @@
-from pygame import (
- init as pg_init,
- quit as pg_quit)
+import pygame
from time import sleep
from display import Display
from status import Status
+from player import Player
from field import Field
from images import Images
+from game import Game
+
def main():
- pg_init()
+ pygame.init()
display = Display()
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)
+ player = Player(field = field)
+ game = Game(player=player)
- display.update(
- [
- status.get_sprites(),
- field.get_sprites()
- ])
+ clock = pygame.time.Clock()
+
+ for _ in range(600):
+ if game.step():
+ break
+ display.update(
+ [
+ status.get_sprites(),
+ field.get_sprites(),
+ player.get_sprites()
+ ])
+ clock.tick(60)
+
sleep(2)
- pg_quit()
+ pygame.quit()
main()
diff --git a/src/sliceitoff/player/__init__.py b/src/sliceitoff/player/__init__.py
new file mode 100644
index 0000000..bf1fc91
--- /dev/null
+++ b/src/sliceitoff/player/__init__.py
@@ -0,0 +1 @@
+from .player import Player \ No newline at end of file
diff --git a/src/sliceitoff/player/player.py b/src/sliceitoff/player/player.py
new file mode 100644
index 0000000..e9cb569
--- /dev/null
+++ b/src/sliceitoff/player/player.py
@@ -0,0 +1,58 @@
+import os
+import pygame
+
+from display import Scaling
+from images import Images
+
+class PlayerSprite(pygame.sprite.Sprite):
+ def __init__(self, image, pos):
+ super().__init__()
+ self.image = image
+ x, y = self.image.get_size()
+ x = pos[0] - x // 2
+ y = pos[1] - y // 2
+ self.rect = self.image.get_rect().move(x,y)
+
+class Player():
+ def __init__(self, field = None):
+ self.field = field
+ self.position = (20_000, 14_000)
+ self.direction = False
+ self.lazer = False
+ self.sprites = pygame.sprite.Group()
+ self.updated = True
+
+ def __update_sprites(self):
+ self.sprites.empty()
+ image = Images.surfaces['player_00']
+ if self.direction:
+ image = pygame.transform.rotate(image, 90)
+ self.sprites.add(PlayerSprite(
+ image,
+ Scaling.scale_to_display(self.position) ))
+ if not self.lazer:
+ self.updated = False
+
+ def set_position(self, pos):
+ if not self.lazer:
+ self.position = Scaling.scale_to_internal(pos)
+ self.updated = True
+
+ def get_sprites(self):
+ if self.updated:
+ self.__update_sprites()
+ return self.sprites
+
+ def set_lazer(self):
+ # self.lazer = True
+ # self.updated = True
+ self.field.slice(self.position, self.direction, 1_000)
+
+ def set_direction(self):
+ if not self.lazer:
+ self.direction = not self.direction
+ self.updated = True
+
+ def __del__(self):
+ pass
+ \ No newline at end of file
diff --git a/src/sliceitoff/status/status.py b/src/sliceitoff/status/status.py
index b2bde21..bf0e864 100644
--- a/src/sliceitoff/status/status.py
+++ b/src/sliceitoff/status/status.py
@@ -26,7 +26,7 @@ class Status():
for letter, x in zip(score_str, range(30_000,40_000,1_000)):
self.sprites.add(LetterSprite(
Images.surfaces[f"letter_{letter}"],
- Scaling.scale_coordinates((x,y)) ))
+ Scaling.scale_to_display((x,y)) ))
self.updated = False
def add_score(self, score_to_add):