It isn't always a 4/4 split
[naja.git] / naja / widgets / game_bits.py
1 """
2 Widget that holds the games's bits.
3 """
4
5 import pygame
6
7 from naja.constants import BIT_SIZE
8 from naja.resources import resources
9 from naja.resources.mutators import EIGHT_BIT
10 from naja.widgets.base import Widget
11
12
13 class GameBitsWidget(Widget):
14     """
15     Widget which holds the game's bits.
16     """
17     def __init__(self, pos, state):
18         super(GameBitsWidget, self).__init__(pos, BIT_SIZE)
19         self.state = state
20
21     def prepare(self):
22         self.surface = pygame.Surface(BIT_SIZE)
23
24         health = self.state.gameboard.health
25         max_health = self.state.gameboard.max_health
26         wins = self.state.gameboard.wins
27         wins_required = self.state.gameboard.wins_required
28
29         bits = []
30         bits.extend((i < health, 'health') for i in range(max_health))
31         bits.extend((i < wins, 'win') for i in range(wins_required))
32
33         for pos, (is_set, image) in enumerate(bits):
34             img = resources.get_image(
35                 'bits', '%s_%s.png' % (image, 'on' if is_set else 'off'),
36                 transforms=(EIGHT_BIT,))
37             self.surface.blit(img, (img.get_width() * pos, 0))
38
39     def draw(self, surface):
40         surface.blit(self.surface, self.pos)