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