diff options
-rw-r--r-- | src/sliceitoff/enemies/enemies.py | 37 | ||||
-rw-r--r-- | src/sliceitoff/field/field.py | 4 | ||||
-rw-r--r-- | src/sliceitoff/game/level.py | 3 |
3 files changed, 24 insertions, 20 deletions
diff --git a/src/sliceitoff/enemies/enemies.py b/src/sliceitoff/enemies/enemies.py index 797fe31..e9b4829 100644 --- a/src/sliceitoff/enemies/enemies.py +++ b/src/sliceitoff/enemies/enemies.py @@ -17,7 +17,7 @@ class EnemySprite(pygame.sprite.Sprite): def set_movement(self, movement: tuple): self.movement = movement - def update(self, dt = 0): + def update(self, dt = 0, **kwargs): if dt: self.position = ( self.position[0] + self.movement[0] * dt, @@ -56,26 +56,27 @@ class Enemies(pygame.sprite.Group): enemy.set_movement( (randrange(0,200)-100, randrange(0,200)-100) ) self.add(enemy) self.field = field - - def wall_hit(self, field, enemy): - if enemy.rect.x < field.rect.x: - enemy.force_right() - if enemy.rect.y < field.rect.y: - enemy.force_down() - if ( enemy.rect.x + enemy.rect.w >= field.rect.x + field.rect.w ): - enemy.force_left() - if ( enemy.rect.y + enemy.rect.h >= field.rect.y + field.rect.h ): - enemy.force_up() - - def hit_walls(self, field_sprites): + + def update(self, field_rects = [], **kwargs): + super().update(**kwargs) """ Do actions on enemies that are only partly on the fields """ for enemy in self.sprites(): - for field in field_sprites: + for field_rect in field_rects: # if enemy is completely inside any field do next enemy - if field.rect.contains(enemy): + if field_rect.contains(enemy): break else: # now find field that enemy is partly on - for field in field_sprites: - if field.rect.colliderect(enemy): - self.wall_hit(field, enemy) + for field_rect in field_rects: + if field_rect.colliderect(enemy): + self.wall_hit(field_rect, enemy) + + def wall_hit(self, field, enemy): + if enemy.rect.x < field.x: + enemy.force_right() + if enemy.rect.y < field.y: + enemy.force_down() + if ( enemy.rect.x + enemy.rect.w >= field.x + field.w ): + enemy.force_left() + if ( enemy.rect.y + enemy.rect.h >= field.y + field.h ): + enemy.force_up() diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py index 6baf966..a062159 100644 --- a/src/sliceitoff/field/field.py +++ b/src/sliceitoff/field/field.py @@ -114,6 +114,10 @@ class Field(pygame.sprite.LayeredUpdates): def active_sprites(self): """ Returns all sprites that are not dead """ return [s for s in self.sprites() if not s.dead] + + def active_rects(self): + """ Returns active areas as rects """ + return [s.rect for s in self.sprites() if not s.dead] def explode(self, area): sx, sy, w, h = area diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index a711b0d..26df345 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -41,8 +41,7 @@ class Level: def step(self, dt): self.status.update(dt = dt) self.field.update(dt = dt) - self.enemies.update(dt = dt) - self.enemies.hit_walls(self.field.active_sprites()) + self.enemies.update(dt = dt, field_rects = self.field.active_rects() ) if self.level_info.active: self.level_info.update(dt = dt) |