1 """Widget to draw the player on the screen"""
3 import pygame.locals as pgl
5 from naja.constants import (PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS,
7 from naja.events import finish_event
8 from naja.resources import resources
9 from naja.resources.mutators import EIGHT_BIT
10 from naja.widgets.base import Widget
13 BITS.CYAN: 'board/robot_cyan.png',
14 BITS.YELLOW: 'board/robot_yellow.png',
15 BITS.MAGENTA: 'board/robot_magenta.png',
19 class RobotWidget(Widget):
20 """Widget which holds a tile on the game board."""
21 def __init__(self, state):
22 pos = (state.player.position[0] * TILE_SIZE[0],
23 state.player.position[1] * TILE_SIZE[1] + BIT_SIZE[1])
24 super(RobotWidget, self).__init__(pos, PLAYER_SIZE)
28 # Look up the required bits on the board location
29 self.pos = (self.state.player.position[0] * TILE_SIZE[0],
30 self.state.player.position[1] * TILE_SIZE[1] + BIT_SIZE[1])
31 self.surface = resources.get_image('board/robot.png',
32 transforms=(EIGHT_BIT,)).copy()
33 for bit, img_name in IMG_MAP.iteritems():
34 if self.state.player.bits.check_bit(bit):
35 bit_img = resources.get_image(img_name,
36 transforms=(EIGHT_BIT,))
37 self.surface.blit(bit_img, (0, 0))
39 def draw(self, surface):
40 surface.blit(self.surface, self.rect)
42 def handle_event(self, ev):
43 if self.state.gameboard.player_mode in (ACT, EXAMINE):
44 return super(RobotWidget, self).handle_event(ev)
45 if ev.type == pgl.KEYDOWN:
47 if self.state.player.move(BITS.NORTH):
48 self.state.gameboard.change_mode(ACT)
50 if ev.key in KEYS.DOWN:
51 if self.state.player.move(BITS.SOUTH):
52 self.state.gameboard.change_mode(ACT)
54 if ev.key in KEYS.LEFT:
55 if self.state.player.move(BITS.WEST):
56 self.state.gameboard.change_mode(ACT)
58 if ev.key in KEYS.RIGHT:
59 if self.state.player.move(BITS.EAST):
60 self.state.gameboard.change_mode(ACT)
62 if ev.key in KEYS.NOMOVE:
63 self.state.gameboard.change_mode(ACT)
65 if ev.key in KEYS.SWITCH:
66 self.state.gameboard.change_mode(EXAMINE)
68 return super(RobotWidget, self).handle_event(ev)