78f1ab91f488ef7ff275e7041ded574bc933f808
[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, BITS
8 from naja.resources import resources
9 from naja.widgets.base import Widget
10
11
12 class GameBitsWidget(Widget):
13     """
14     Widget which holds the game's bits.
15     """
16     def __init__(self, pos, state):
17         super(GameBitsWidget, self).__init__(pos, BIT_SIZE)
18         self.state = state
19
20     def prepare(self):
21         self.surface = pygame.Surface(BIT_SIZE)
22         bits = (
23             (BITS.MSB, 'msb', ()),
24             (BITS.YELLOW, 'yellow', ()),
25             (BITS.MAGENTA, 'magenta', ()),
26             (BITS.CYAN, 'cyan', ()),
27             (BITS.WEST, 'arrow', ()),
28             (BITS.EAST, 'arrow', ()),
29             (BITS.SOUTH, 'arrow', ()),
30             (BITS.NORTH, 'arrow', ()),
31         )
32         for pos, (bit, image, transforms) in enumerate(bits):
33             is_set = self.state.player.bits.check_bit(bit)
34             img = resources.get_image(
35                 'bits', '%s_%s.png' % (image, 'on' if is_set else 'off'),
36                 transforms=transforms)
37             self.surface.blit(img, (img.get_width() * pos, 0))
38
39     def draw(self, surface):
40         surface.blit(self.surface, self.pos)