summaryrefslogtreecommitdiff
path: root/hud.py
blob: 179153c416dff93006765e3a73dc8b01ad28b0e9 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from game import Game, GameSprite
from font import Font


class ScoreCounterSprite(GameSprite):
    """Single digit for a score counter"""

    def __init__(self, game: Game):
        """Initialise the score counter

        :param game: The game which this belongs to
        :type game: Game
        """
        self.number_images = []

        self.x = 0

        for i in range(10):
            self.number_images.append(
                Font.load_text(game.texture_factory, str(i)))

        super().__init__(game, self.number_images[0])

    def update_image(self):
        """Update the digit"""
        self.set_image(self.number_images[int(self.x % 10)])

    def set(self, x):
        """Set the image

        :param x: number to set this digit to
        """
        self.x = x
        self.update_image()


class ScoreCounter:
    """Sprite to display a number"""

    def __init__(self, game: Game, num_digits, position=(0, 0)) -> None:
        """__init__.

        :param game:
        :type game: Game
        :param num_digits:
        :param position:
        :rtype: None
        """
        self.digits = []
        x, y = position

        self.number = 0

        for i in range(num_digits):
            sprite = ScoreCounterSprite(game)
            sprite.set_pos((x+(Font.FONT_SIZE + 1)*i, y))
            self.digits.append(sprite)

    def set(self, number):
        """Set the score to be displayed

        :param number:
        """
        if number != self.number:
            self.number = number
            power = 10**len(self.digits)
            for digit in self.digits:
                power /= 10
                digit.set((number // power) % 10)

    def destroy(self):
        """Remove this counter"""
        for n in self.digits:
            n.destroy()

    def send_to_front(self):
        """Move this counter to the foreground"""
        for d in self.digits:
            d.send_to_front()

    def show(self):
        """Make this counter visible"""
        for d in self.digits:
            d.show()

    def hide(self):
        """Make this counter invisible"""
        for d in self.digits:
            d.hide()


class GameHud:
    """Object to manage the items visible in the game's heads up display"""

    SCORE_DIGITS = 8
    HP_DIGITS = 2

    def __init__(self, game) -> None:
        """Initialise the HUD

        :param game: The game which this belongs to
        """

        self.game = game
        self.score_counter = ScoreCounter(game, GameHud.SCORE_DIGITS,
                                          position=(
                                              game.w
                                              - GameHud.SCORE_DIGITS
                                              * (Font.FONT_SIZE+1),
                                              1)
                                          )

        self.hp_symbol = GameSprite(game, game.player.image)
        self.hp_symbol.set_pos((1, 1))

        x_image = Font.load_text(game.texture_factory, "x")
        self.x_symbol = GameSprite(game, x_image)
        self.x_symbol.set_pos((self.hp_symbol.x+self.hp_symbol.w+1, 1))

        self.hp_counter = ScoreCounter(game, GameHud.HP_DIGITS,
                                       position=(self.x_symbol.x+1 +
                                                 self.x_symbol.w, 1)
                                       )

        self.items = (self.score_counter,
                      self.hp_symbol,
                      self.x_symbol,
                      self.hp_counter)

    def tick(self):
        """Update the hud"""
        self.score_counter.set(self.game.score)
        self.hp_counter.set(
            self.game.player.hp if self.game.player.hp > 0 else 0)

        for x in self.items:
            x.send_to_front()

    def destroy(self):
        """Remove all the associated objects"""
        for x in self.items:
            x.destroy()

    def hide(self):
        """Make this object invisible"""
        for x in self.items:
            x.hide()

    def show(self):
        """Make this object visible"""
        for x in self.items:
            x.show()