diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-23 18:42:26 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-23 18:42:26 +0200 |
commit | b03cb075e7a2042764a355a46d649b187f66343b (patch) | |
tree | f78e991650d33f7b1d9d8eab60f1e045da92d6c1 /src | |
parent | 975e5dca49fb09fe2f762462f1c337c0d63735bf (diff) |
Field as sprite group.
Diffstat (limited to 'src')
-rw-r--r-- | src/sliceitoff/display/scaling.py | 6 | ||||
-rw-r--r-- | src/sliceitoff/enemies/enemies.py | 6 | ||||
-rw-r--r-- | src/sliceitoff/field/field.py | 111 | ||||
-rw-r--r-- | src/sliceitoff/game/level.py | 8 | ||||
-rw-r--r-- | src/sliceitoff/player/player.py | 7 | ||||
-rw-r--r-- | src/sliceitoff/screens/welcome.py | 4 | ||||
-rw-r--r-- | src/sliceitoff/text/text.py | 8 |
7 files changed, 90 insertions, 60 deletions
diff --git a/src/sliceitoff/display/scaling.py b/src/sliceitoff/display/scaling.py index 53a9fb1..ddc897f 100644 --- a/src/sliceitoff/display/scaling.py +++ b/src/sliceitoff/display/scaling.py @@ -6,6 +6,8 @@ class Scaling(): factor = 0.02 left = 0 top = 0 + resolution = (0,0) + center = (0,0) borders = (pygame.Rect(0,0,0,0), pygame.Rect(0,0,0,0)) active = pygame.Rect(0,0,0,0) @@ -38,11 +40,13 @@ class Scaling(): @staticmethod def update_scaling(size: tuple) -> None: + __class__.resolution = size + __class__.center = (size[0]/2,size[1]/2) 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 - __class__.border = ( + __class__.borders = ( pygame.Rect( 0, 0, diff --git a/src/sliceitoff/enemies/enemies.py b/src/sliceitoff/enemies/enemies.py index d9ba59a..a3a60a0 100644 --- a/src/sliceitoff/enemies/enemies.py +++ b/src/sliceitoff/enemies/enemies.py @@ -55,7 +55,7 @@ class Enemies(): def emerge_enemies(self): for _ in range(3 + (Stats.level-1) * 2): enemy = EnemyBall() - enemy.set_position( (randrange(0,380_000), randrange(0,260_000)) ) + enemy.set_position( (randrange(0,300_000), randrange(0,200_000)) ) enemy.set_movement( (randrange(0,200)-100, randrange(0,200)-100) ) self.sprites.add(enemy) @@ -73,10 +73,10 @@ class Enemies(): """ Update sprites basis of dt. dt = milliseconds from last update """ self.sprites.update(move=dt) for enemy in self.sprites: - for field in self.field.sprites: + for field in self.field.sprites(): if field.rect.contains(enemy): break else: - for field in self.field.sprites: + for field in self.field.sprites(): if field.rect.colliderect(enemy): self.wall_hit(field, enemy) diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py index 5d2a573..842b07a 100644 --- a/src/sliceitoff/field/field.py +++ b/src/sliceitoff/field/field.py @@ -7,48 +7,57 @@ from stats import Stats DEBUG = os.getenv("DEBUG") + class FieldSprite(pygame.sprite.Sprite): - def __init__(self, area: tuple ): + def __init__(self, area: tuple): super().__init__() + self.color = (0,255,0,255) + self.dead = False 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") + self.image = pygame.Surface(self.rect.size, pygame.SRCALPHA) + self.image.fill(self.color) + + def update(self, dt = 0): + if dt and self.dead: + self.color = ( + self.color[0], + self.color[1], + self.color[2], + self.color[3] - dt/4) + if self.color[3] <= 0: + self.kill() + else: + self.image.fill(self.color) + + +class SliceSprite(FieldSprite): + def __init__(self, area: tuple ): + super().__init__(area) + self.color = (255,255,0,255) + self.image.fill(self.color) + self.dead = True -class Field(): + +class Field(pygame.sprite.LayeredUpdates): initial_area = (320_000, 220_000) def __init__(self): - self.sprites = pygame.sprite.Group() - self.sprites.add(FieldSprite( (0, 0, *__class__.initial_area) )) + super().__init__() + self.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]: - return False - if x >= area[0]+area[2]: - return False - 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.area_current = sum( s.area[2]*s.area[3] for s in self.sprites() ) Stats.percent = 100 * self.area_current / self.area_full if DEBUG: print( f"FIELD: {self.area_full}/{self.area_current}, " - f"{self.percentage}") + f"{Stats.percent}") def slice( self, @@ -56,20 +65,17 @@ class Field(): direction: bool, thickness: int) -> pygame.Rect: """ Slice one area into two areas """ - - # Find the overlapping area - for field in self.sprites: - if self.coordinates_inside_area(field.area, pos[0], pos[1]): - break - else: + + # Slicing hits the area? + overlap = self.get_sprites_at(Scaling.scale_to_display(pos)) + if not overlap: return None - - # remove current area - ax, ay, aw, ah = field.area - field.remove(self.sprites) + + # Save the area information and remove the sprite + ax, ay, aw, ah = overlap[0].area + overlap[0].remove(self) # create new areas if there is any space - if direction: x1 = ax x2 = pos[0] - thickness @@ -77,10 +83,12 @@ class Field(): x4 = ax + aw if x2 > x1: - self.sprites.add(FieldSprite( (x1, ay, x2-x1, ah) )) + self.add(FieldSprite( (x1, ay, x2-x1, ah) )) if x4 > x3: - self.sprites.add(FieldSprite( (x3, ay, x4-x3, ah) )) + self.add(FieldSprite( (x3, ay, x4-x3, ah) )) self.update_area() + + self.add(SliceSprite( (x2, ay, x3-x2, ah) )) return Scaling.area_to_rect((x2, ay, x3-x2, ah)) y1 = ay y2 = pos[1] - thickness @@ -88,13 +96,28 @@ class Field(): y4 = ay + ah if y2 > y1: - self.sprites.add(FieldSprite( (ax, y1, aw, y2-y1) )) + self.add(FieldSprite( (ax, y1, aw, y2-y1) )) if y4 > y3: - self.sprites.add(FieldSprite( (ax, y3, aw, y4-y3) )) + self.add(FieldSprite( (ax, y3, aw, y4-y3) )) + + self.add(SliceSprite( (ax, y2, aw, y3-y2) )) + self.update_area() return Scaling.area_to_rect((ax, y2, aw, y3-y2)) - - def update(self, dt): - """ Update sprites basis of dt. dt = milliseconds from last update """ - pass -
\ No newline at end of file + + def active_sprites(self): + """ Returns all sprites that are not dead """ + return (s for s in self.sprites() if not s.dead) + + def kill_if_not_colliding(self, sprites): + """ If there is empty fields that are not yet dead kill them """ + for field in self.active_sprites(): + for enemy in sprites: + if enemy.rect.colliderect(field.rect): + break + else: + field.dead = True + +# def update(self, dt): +# """---""" +
\ No newline at end of file diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index fadbee3..e5dc9f6 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -24,11 +24,15 @@ class Level: def step(self, dt): for obj in self.obj_classes: - obj.update(dt) + obj.update(dt = dt) Stats.update_bonus(dt) - self.display.update( (obj.sprites for obj in self.obj_classes) ) + self.display.update( ( + self.status.sprites, + self.field, + self.enemies.sprites, + self.player.sprites) ) if self.gameplay.step(): return False diff --git a/src/sliceitoff/player/player.py b/src/sliceitoff/player/player.py index 2f4ce57..6e2ab0b 100644 --- a/src/sliceitoff/player/player.py +++ b/src/sliceitoff/player/player.py @@ -58,12 +58,7 @@ class Player(): if hitbox.colliderect(enemy.rect): hit = True break - 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) + self.field.kill_if_not_colliding(self.enemies.sprites) return hit def set_direction(self): diff --git a/src/sliceitoff/screens/welcome.py b/src/sliceitoff/screens/welcome.py index 93218f3..9a7a341 100644 --- a/src/sliceitoff/screens/welcome.py +++ b/src/sliceitoff/screens/welcome.py @@ -7,6 +7,10 @@ def welcome_screen(): " * Do not hit the balls\n" " * Make area 10% or smaller\n" " * Do not separate the balls\n" + " * Make area 10% or smaller\n" + " * Do not separate the balls\n" + " * Make area 10% or smaller\n" + " * Do not separate the balls\n" " * Be fast!!\n", font = 'computer', size = (8_000, 16_000), diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py index e86bede..e8ccb57 100644 --- a/src/sliceitoff/text/text.py +++ b/src/sliceitoff/text/text.py @@ -12,8 +12,8 @@ class LetterSprite(pygame.sprite.Sprite): self.image = image self.rect = self.image.get_rect().move(pos) self.direction = ( - Scaling.factor * (2_000 - randrange(4_000)), - Scaling.factor * (2_000 - randrange(4_000))) + Scaling.factor * (1_000 - randrange(2_000)), + Scaling.factor * (1_000 - randrange(2_000))) def update(self, explode=0): if explode: @@ -23,8 +23,8 @@ class LetterSprite(pygame.sprite.Sprite): self.rect.w, self.rect.h) self.direction = ( - self.direction[0] * 1.15, - self.direction[1] * 1.15) + self.direction[0] * 0.8, + self.direction[1] * 0.8 + 1) class TextPage(pygame.sprite.Group): |