summaryrefslogtreecommitdiff
path: root/src/sliceitoff/enemies/enemies.py
blob: ee8109b00c4e400b6ab04af3f08d4daa32dc4573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
""" enemies.enemies - group for enemies for the leve"""
from random import randrange
import pygame

from .ball import EnemyBall
from .bouncher import EnemyBouncher

class Enemies(pygame.sprite.Group):
    """ Init with count. Randomizing enemy types. Some are worth more. """
    def __init__(self, count = 0):
        super().__init__()
        while count:
            match randrange(0,4):
                case 0|1|2:
                    if count >= 1:
                        self.add(EnemyBall())
                        count -= 1
                case 3:
                    if count >= 2:
                        self.add(EnemyBouncher())
                        count -= 2

    def update(self, field_rects = None, **kwargs):
        """ Do actions on enemies that are only partly on the fields """
        super().update(**kwargs)
        for enemy in self.sprites():
            for field_rect in field_rects:
                # if enemy is completely inside any field do next enemy
                if field_rect.contains(enemy):
                    break
            else:
                # now find field that enemy is partly on
                for field_rect in field_rects:
                    if field_rect.colliderect(enemy):
                        enemy.update(wall_hit = field_rect)