summaryrefslogtreecommitdiff
path: root/shooter.py
blob: 94878e344c3fb3e444c20118daa04721c74aadbe (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from dataclasses import dataclass
from typing import List

from game import DamageableSprite, Game, GameSprite
from sprite import Sprite


class Lazer(GameSprite):
    """Lazer object that is shot by a shooter"""

    def __init__(self, game: Game, velocity=-4, color="white"):
        """Initialise the lazer

        :param game: The game which this belongs to
        :type game: Game
        :param velocity: Velocity to move the lazer at
        :param color: name of the colour of the lazer
        """
        self.velocity = velocity
        self.game = game
        super().__init__(game, game.texture_factory.get_image(
            f"lazer:{color}"))

    def tick(self):
        """Update this object"""
        self.move(0, self.velocity)
        if self.y + self.h > self.game.h or self.y < 0:
            self.destroy()


@dataclass
class ShooterAttributes:
    """Attributes for a shooter object"""

    lazer_color: str = "white"
    cooldown: int = 40
    velocity: int = 1
    hp: int = 3


class Shooter(DamageableSprite):
    """A game object that is able to shoot lazers"""

    def __init__(self, game: Game,
                 image_name: str, attributes: ShooterAttributes):
        """Initialise the shooter

        :param game: The game which this belongs to
        :type game: Game
        :param image_name: The name of the image to use for this sprite
        :type image_name: str
        :param attributes: The attributes to use for this object
        :type attributes: ShooterAttributes
        """
        super().__init__(game, image_name, hp=attributes.hp)
        self.lazers: List[Lazer] = []
        self.attributes = attributes
        self.last_shot = self.game.alpha

    def shoot(self):
        """Soot a lazer if possible"""
        next_shot = self.last_shot + self.attributes.cooldown

        if not self.destroyed \
                and self.game.alpha > next_shot:
            self.last_shot = self.game.alpha

            lazer = Lazer(self.game,
                          velocity=self.attributes.velocity,
                          color=self.attributes.lazer_color)
            lazer.set_pos((self.x + self.w//2 - 1, self.y +
                          self.h//2 - 1))
            lazer.show()
            self.lazers.append(lazer)

    def tick(self):
        """Update this object"""
        super().tick()
        for lazer in self.lazers:
            lazer.tick()

        self.lazers = Sprite.remove_destroyed(self.lazers)

    def destroy(self):
        """Remove all the associated objects"""
        super().destroy()
        for lazer in self.lazers:
            self.game.sprites.append(lazer)