summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-04-08 00:17:53 +0300
committerViljami Ilola <+@hix.fi>2024-04-08 00:17:53 +0300
commit24c9a5fb88810c80c414ec0a932c9405d8d54abc (patch)
tree056bd87fea73cd9579fb8512b179089c3b8ccc7a
parent6ba20c61a04c688120161f6c5fbb56102f8082ef (diff)
refactor field slicer
-rw-r--r--src/sliceitoff/field/field.py53
-rw-r--r--src/sliceitoff/game/show.py2
2 files changed, 29 insertions, 26 deletions
diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py
index 93b4dba..2bf7d12 100644
--- a/src/sliceitoff/field/field.py
+++ b/src/sliceitoff/field/field.py
@@ -81,39 +81,42 @@ class Field(pygame.sprite.LayeredUpdates):
return None
# Save the area information and remove the sprite
- ax, ay, aw, ah = overlap.area
+ overlap_area = overlap.area
overlap.remove(self)
+ slicer = self.vertical_slicer if direction else self.horizontal_slicer
# create new areas if there is any space
- if direction:
- t2 = pos[0] - thickness
- t3 = pos[0] + thickness
- t4 = ax + aw
-
- if t2 > ax:
- self.add(FieldSprite( (ax, ay, t2-ax, ah) ))
- if t4 > t3:
- self.add(FieldSprite( (t3, ay, t4-t3, ah) ))
-
- area = t2, ay, t3-t2, ah
-
- else:
- t2 = pos[1] - thickness
- t3 = pos[1] + thickness
- t4 = ay + ah
-
- if t2 > ay:
- self.add(FieldSprite( (ax, ay, aw, t2-ay) ))
- if t4 > t3:
- self.add(FieldSprite( (ax, t3, aw, t4-t3) ))
-
- area = ax, t2, aw, t3-t2
-
+ area = slicer(pos, overlap_area, thickness)
self.explode(area)
zap_spite = SliceSprite(area)
self.add(zap_spite)
return zap_spite
+ def vertical_slicer(self, pos, area, thickness):
+ """ slices area in 3 parts vertically. returns lazer area """
+ ax, ay, aw, ah = area
+ t2 = pos[0] - thickness
+ t3 = pos[0] + thickness
+ t4 = ax + aw
+ if t2 > ax:
+ self.add(FieldSprite( (ax, ay, t2-ax, ah) ))
+ if t4 > t3:
+ self.add(FieldSprite( (t3, ay, t4-t3, ah) ))
+ return (t2, ay, t3-t2, ah)
+
+ def horizontal_slicer(self, pos, area, thickness):
+ """ slices area in 3 parts horizontally. returns lazer area """
+ ax, ay, aw, ah = area
+ t2 = pos[1] - thickness
+ t3 = pos[1] + thickness
+ t4 = ay + ah
+ if t2 > ay:
+ self.add(FieldSprite( (ax, ay, aw, t2-ay) ))
+ if t4 > t3:
+ self.add(FieldSprite( (ax, t3, aw, t4-t3) ))
+ return (ax, t2, aw, t3-t2)
+
+
def active_sprites(self):
""" Returns all sprites that are not dead """
return [s for s in self.sprites() if not s.dead]
diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py
index eaf3658..496fc4a 100644
--- a/src/sliceitoff/game/show.py
+++ b/src/sliceitoff/game/show.py
@@ -12,7 +12,7 @@ class Show(ExplodeOutGroup):
def update(self, dt = 0, **kwargs):
""" First timeout then fadeout and then inactivity """
if not super().update(dt = dt, **kwargs):
- return
+ return
if anykey():
self.do_fadeout()
if self.timeout <= 0: