2fa0d11a178b3dca09f2353ee42d0c658b25172a
[naja.git] / naja / widgets / robot.py
1 """Widget to draw the player on the screen"""
2
3 from naja.constants import (PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS)
4 from naja.resources import resources
5 from naja.resources.mutators import EIGHT_BIT
6 from naja.widgets.base import Widget
7
8 IMG_MAP = {
9     BITS.RED: 'board/robot_red.png',
10     BITS.GREEN: 'board/robot_green.png',
11     BITS.BLUE: 'board/robot_blue.png',
12 }
13
14
15 class RobotWidget(Widget):
16     """Widget which holds a tile on the game board."""
17     def __init__(self, state):
18         pos = (state.player.position[0] * TILE_SIZE[0],
19                state.player.position[1] * TILE_SIZE[1] + BIT_SIZE[1])
20         super(RobotWidget, self).__init__(pos, PLAYER_SIZE)
21         self.state = state
22
23     def prepare(self):
24         # Look up the required bits on the board location
25         self.pos = (self.state.player.position[0] * TILE_SIZE[0],
26                     self.state.player.position[1] * TILE_SIZE[1] + BIT_SIZE[1])
27         self.surface = resources.get_image('board/robot.png',
28                                            transforms=(EIGHT_BIT,)).copy()
29         for bit, img_name in IMG_MAP.iteritems():
30             if self.state.player.bits.check_bit(bit):
31                 bit_img = resources.get_image(img_name,
32                                               transforms=(EIGHT_BIT,))
33                 self.surface.blit(bit_img, (0, 0))
34
35     def draw(self, surface):
36         surface.blit(self.surface, self.rect)