Make chess puzzle level (Kasparov to F3) winnable.
[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 from naja.widgets.text import TextWidget
12
13
14 class GameBitsWidget(Widget):
15     """
16     Widget which holds the game's bits.
17     """
18     def __init__(self, pos, state):
19         super(GameBitsWidget, self).__init__(pos, BIT_SIZE)
20         self.state = state
21
22     def prepare(self):
23         self.surface = pygame.Surface(BIT_SIZE)
24
25         size = BIT_SIZE[1]
26
27         zero = TextWidget((0, 0), '0', fontsize=24, colour='white')
28         one = TextWidget((0, 0), '1', fontsize=24, colour='white')
29         zero.prepare()
30         zero.pos = (0, (size - zero.size[1]))
31         one.pos = zero.pos
32
33         health = self.state.gameboard.health
34         max_health = self.state.gameboard.max_health
35         wins = self.state.gameboard.wins
36         wins_required = self.state.gameboard.wins_required
37
38         bits = []
39         bits.extend((i < health, 'health') for i in range(max_health))
40         bits.extend((i < wins, 'win') for i in range(wins_required))
41
42         for pos, (is_set, image) in enumerate(bits):
43             img = resources.get_image(
44                 'bits', '%s_%s.png' % (image, 'on' if is_set else 'off'),
45                 transforms=(EIGHT_BIT,))
46             self.surface.blit(img, (img.get_width() * pos, 0))
47             if is_set:
48                 one.render(self.surface)
49             else:
50                 zero.render(self.surface)
51             one.pos = zero.pos = (zero.pos[0] + size, zero.pos[1])
52
53     def draw(self, surface):
54         surface.blit(self.surface, self.pos)