summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-16 20:30:32 +0200
committerViljami Ilola <+@hix.fi>2024-03-16 20:30:32 +0200
commit3959cfd0c0cab47f863ea5672350ad280b4b7773 (patch)
tree1aff8e0cad45be393802774b4c011468c8cfc161 /src
parent6a9f8ab2b47ab9dd19b7ae0e507c16bb919de975 (diff)
field remove
Diffstat (limited to 'src')
-rw-r--r--src/sliceitoff/field/field.py68
-rw-r--r--src/sliceitoff/game/game.py3
-rw-r--r--src/sliceitoff/main.py4
-rw-r--r--src/sliceitoff/player/player.py18
4 files changed, 57 insertions, 36 deletions
diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py
index 13b5c6a..c0c482d 100644
--- a/src/sliceitoff/field/field.py
+++ b/src/sliceitoff/field/field.py
@@ -2,18 +2,24 @@ import pygame
from display import Scaling
class FieldSprite(pygame.sprite.Sprite):
- def __init__(self, rect: pygame.Rect):
+ def __init__(self, area: tuple ):
super().__init__()
- self.rect = rect
+ self.area = area
+ self.update()
+
+ def update(self):
+ self.rect = Scaling.area_to_rect(self.area)
self.image = pygame.Surface(self.rect.size)
self.image.fill("green")
+
class Field():
def __init__(self):
self.sprites = pygame.sprite.Group()
- self.updated = True
- self.areas = [(0,0,40_000,28_000)]
+# self.updated = True
+ self.sprites.add(FieldSprite( (0, 0, 40_000, 28_000) ))
+# self.areas = [(0,0,40_000,28_000)]
def coordinates_inside_area(self, area, x, y) -> bool:
if x < area[0]:
@@ -26,21 +32,23 @@ class Field():
return False
return True
- def slice(self, pos: tuple, direction: bool, thickness: int) -> bool:
+ def slice(self, pos: tuple, direction: bool, thickness: int) -> pygame.Rect:
""" Slice one area into two areas """
# Find the overlapping area
- for area in self.areas:
- if self.coordinates_inside_area(area, pos[0], pos[1]):
+ for field in self.sprites:
+ if self.coordinates_inside_area(field.area, pos[0], pos[1]):
break
else:
- return False
+ return None
# remove current area
- self.areas.remove(area)
- ax, ay, aw, ah = area
+ ax, ay, aw, ah = field.area
+ field.remove(self.sprites)
# create new areas if there is any space
+# self.updated = True
+
if direction:
x1 = ax
x2 = pos[0] - thickness
@@ -48,31 +56,31 @@ class Field():
x4 = ax + aw
if x2 > x1:
- self.areas.append( (x1, ay, x2-x1, ah) )
+ self.sprites.add(FieldSprite( (x1, ay, x2-x1, ah) ))
if x4 > x3:
- self.areas.append( (x3, ay, x4-x3, ah) )
- else:
- y1 = ay
- y2 = pos[1] - thickness
- y3 = pos[1] + 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
+ self.sprites.add(FieldSprite( (x3, ay, x4-x3, ah) ))
+ return Scaling.area_to_rect((x2, ay, x3-x2, ah))
+ y1 = ay
+ y2 = pos[1] - thickness
+ y3 = pos[1] + thickness
+ y4 = ay + ah
+
+ if y2 > y1:
+ self.sprites.add(FieldSprite( (ax, y1, aw, y2-y1) ))
+ if y4 > y3:
+ self.sprites.add(FieldSprite( (ax, y3, aw, y4-y3) ))
+ return Scaling.area_to_rect((ax, y2, aw, y3-y2))
def __update_sprites(self):
- self.sprites.empty()
- for area in self.areas:
- self.sprites.add(FieldSprite(Scaling.area_to_rect(area)))
- self.updated = False
+ """"""
+# self.sprites.empty()
+# for area in self.areas:
+# self.sprites.add(FieldSprite(Scaling.area_to_rect(area)))
+# self.updated = False
def get_sprites(self) -> pygame.sprite.Group:
- if self.updated:
- self.__update_sprites()
+# if self.updated:
+# self.__update_sprites()
return self.sprites
def __del__(self):
diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py
index 789edd7..4bce0f0 100644
--- a/src/sliceitoff/game/game.py
+++ b/src/sliceitoff/game/game.py
@@ -14,7 +14,8 @@ class Game:
if event.type == pygame.MOUSEBUTTONDOWN:
self.player.set_position(pygame.mouse.get_pos())
if event.button == 1:
- self.player.set_lazer()
+ if self.player.fire_lazer():
+ return True
if event.button == 3:
self.player.set_direction()
return False
diff --git a/src/sliceitoff/main.py b/src/sliceitoff/main.py
index 966f3a1..529fcaa 100644
--- a/src/sliceitoff/main.py
+++ b/src/sliceitoff/main.py
@@ -19,8 +19,8 @@ def main():
status = Status()
field = Field()
- player = Player(field = field)
enemies = Enemies(field = field, level = 2)
+ player = Player(field = field, enemies = enemies)
game = Game(player=player)
clock = pygame.time.Clock()
@@ -35,7 +35,7 @@ def main():
enemies.get_sprites(),
player.get_sprites()
])
- clock.tick(120)
+ clock.tick(60)
sleep(2)
diff --git a/src/sliceitoff/player/player.py b/src/sliceitoff/player/player.py
index e9cb569..a7d2b63 100644
--- a/src/sliceitoff/player/player.py
+++ b/src/sliceitoff/player/player.py
@@ -14,8 +14,9 @@ class PlayerSprite(pygame.sprite.Sprite):
self.rect = self.image.get_rect().move(x,y)
class Player():
- def __init__(self, field = None):
+ def __init__(self, field = None, enemies = None):
self.field = field
+ self.enemies = enemies
self.position = (20_000, 14_000)
self.direction = False
self.lazer = False
@@ -43,10 +44,21 @@ class Player():
self.__update_sprites()
return self.sprites
- def set_lazer(self):
+ def fire_lazer(self):
# self.lazer = True
# self.updated = True
- self.field.slice(self.position, self.direction, 1_000)
+ hitbox = self.field.slice(self.position, self.direction, 1_000)
+ if hitbox is not None:
+ for enemy in self.enemies.sprites:
+ if hitbox.colliderect(enemy.rect):
+ return True
+ for field in self.field.sprites:
+ for enemy in self.enemies.sprites:
+ if enemy.rect.colliderect(field.rect):
+ break
+ else:
+ field.remove(self.field.sprites)
+ return False
def set_direction(self):
if not self.lazer: