summaryrefslogtreecommitdiff
path: root/src/sliceitoff/enemies/enemies.py
blob: c52059fb123e006b4a0127d6340dd09461a63462 (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
from random import randrange
import pygame

from .ball import EnemyBall
from .bouncher import EnemyBouncher

class Enemies(pygame.sprite.Group):
    def __init__(self, field = None, 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
        self.field = field

    def update(self, field_rects = [], **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)