diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-23 20:19:38 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-23 20:19:38 +0200 |
commit | 87cd7293498d6b30389e056a59b2e69eb7b3a418 (patch) | |
tree | 392e2e15446a533d0bdc1faddd5a6e2d8fc731ee /src | |
parent | b03cb075e7a2042764a355a46d649b187f66343b (diff) |
enemies as group
Diffstat (limited to 'src')
-rw-r--r-- | src/sliceitoff/enemies/enemies.py | 25 | ||||
-rw-r--r-- | src/sliceitoff/game/gameplay.py | 14 | ||||
-rw-r--r-- | src/sliceitoff/game/level.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/player/player.py | 7 |
4 files changed, 25 insertions, 23 deletions
diff --git a/src/sliceitoff/enemies/enemies.py b/src/sliceitoff/enemies/enemies.py index a3a60a0..aaa41be 100644 --- a/src/sliceitoff/enemies/enemies.py +++ b/src/sliceitoff/enemies/enemies.py @@ -18,11 +18,11 @@ class EnemySprite(pygame.sprite.Sprite): def set_movement(self, movement: tuple): self.movement = movement - def update(self, move = 0): - if move: + def update(self, dt = 0): + if dt: self.position = ( - self.position[0] + self.movement[0] * move, - self.position[1] + self.movement[1] * move) + self.position[0] + self.movement[0] * dt, + self.position[1] + self.movement[1] * dt) self.rect = pygame.Rect( Scaling.scale_to_display(self.position), self.image.get_size()) @@ -46,18 +46,15 @@ class EnemyBall(EnemySprite): self.update() -class Enemies(): +class Enemies(pygame.sprite.Group): def __init__(self, field = None): - self.sprites = pygame.sprite.Group() - self.emerge_enemies() - self.field = field - - def emerge_enemies(self): + super().__init__() for _ in range(3 + (Stats.level-1) * 2): enemy = EnemyBall() 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) + self.add(enemy) + self.field = field def wall_hit(self, field, enemy): if enemy.rect.x < field.rect.x: @@ -69,10 +66,10 @@ class Enemies(): if ( enemy.rect.y + enemy.rect.h >= field.rect.y + field.rect.h ): enemy.force_up() - def update(self, dt): + def update(self, dt = 0): """ Update sprites basis of dt. dt = milliseconds from last update """ - self.sprites.update(move=dt) - for enemy in self.sprites: + super().update(dt = dt) + for enemy in self.sprites(): for field in self.field.sprites(): if field.rect.contains(enemy): break diff --git a/src/sliceitoff/game/gameplay.py b/src/sliceitoff/game/gameplay.py index cc20c3b..6296a5a 100644 --- a/src/sliceitoff/game/gameplay.py +++ b/src/sliceitoff/game/gameplay.py @@ -5,14 +5,22 @@ from stats import Stats class Gameplay: """ Logic of the playfield """ - def __init__(self, player = None, field = None, status = None): + def __init__( + self, + player = None, + field = None, + status = None, + enemies = None): self.status = status self.player = player self.field = field + self.field = enemies def fire(self): - if self.player.fire_lazer() and Stats.lose_life(): - return True + if self.player.fire_lazer(): + field.kill_if_not_colliding(enemies.sprites()) + if Stats.lose_life(): + return True return Stats.percent < 20 def quit(self): diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index e5dc9f6..a417d8f 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -31,7 +31,7 @@ class Level: self.display.update( ( self.status.sprites, self.field, - self.enemies.sprites, + self.enemies, self.player.sprites) ) if self.gameplay.step(): diff --git a/src/sliceitoff/player/player.py b/src/sliceitoff/player/player.py index 6e2ab0b..d36691c 100644 --- a/src/sliceitoff/player/player.py +++ b/src/sliceitoff/player/player.py @@ -17,13 +17,13 @@ class PlayerSprite(pygame.sprite.Sprite): class Player(): def __init__(self, field = None, enemies = None): - self.field = field self.enemies = enemies self.position = (200_000, 140_000) self.direction = False self.lazer = False self.sprites = pygame.sprite.Group() self.updated = True + self.field = field def __update_sprites(self): self.sprites.empty() @@ -49,16 +49,13 @@ class Player(): def fire_lazer(self): if DEBUG: print("PLAYER: fire lazer") - # self.lazer = True - # self.updated = True hitbox = self.field.slice(self.position, self.direction, 4_500) hit = False if hitbox is not None: - for enemy in self.enemies.sprites: + for enemy in self.enemies.sprites(): if hitbox.colliderect(enemy.rect): hit = True break - self.field.kill_if_not_colliding(self.enemies.sprites) return hit def set_direction(self): |