Properly implement player movement.
[naja.git] / naja / widgets / robot.py
1 """Widget to draw the player on the screen"""
2
3 import pygame.locals as pgl
4
5 from naja.constants import PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS
6 from naja.events import InvalidateTheWorld, PlayerMoved
7 from naja.resources import resources
8 from naja.resources.mutators import EIGHT_BIT
9 from naja.widgets.base import Widget
10
11 IMG_MAP = {
12         BITS.CYAN: 'board/robot_cyan.png',
13         BITS.YELLOW: 'board/robot_yellow.png',
14         BITS.MAGENTA: 'board/robot_magenta.png',
15         }
16
17
18 class RobotWidget(Widget):
19     """Widget which holds a tile on the game board."""
20     def __init__(self, state):
21         pos = (state.player.position[0] * TILE_SIZE[0],
22                state.player.position[1] * TILE_SIZE[1] + BIT_SIZE[1])
23         super(RobotWidget, self).__init__(pos, PLAYER_SIZE)
24         self.state = state
25
26     def prepare(self):
27         # Look up the required bits on the board location
28         self.pos = (self.state.player.position[0] * TILE_SIZE[0],
29                     self.state.player.position[1] * TILE_SIZE[1] + BIT_SIZE[1])
30         self.surface = resources.get_image('board/robot.png',
31                                            transforms=(EIGHT_BIT,)).copy()
32         for bit, img_name in IMG_MAP.iteritems():
33             if self.state.player.bits.check_bit(bit):
34                 bit_img = resources.get_image(img_name,
35                                               transforms=(EIGHT_BIT,))
36                 self.surface.blit(bit_img, (0, 0))
37
38     def draw(self, surface):
39         surface.blit(self.surface, self.rect)
40
41     def handle_event(self, ev):
42         if self.state.gameboard.player_mode == ACT:
43             return super(RobotWidget, self).handle_event(ev)
44         if ev.type == pgl.KEYDOWN:
45             if ev.key in KEYS.UP:
46                 if self.state.player.move(BITS.NORTH):
47                     self.state.gameboard.change_mode()
48                     PlayerMoved.post()
49                     InvalidateTheWorld.post()
50                     return True
51             if ev.key in KEYS.DOWN:
52                 if self.state.player.move(BITS.SOUTH):
53                     self.state.gameboard.change_mode()
54                     PlayerMoved.post()
55                     InvalidateTheWorld.post()
56                     return True
57             if ev.key in KEYS.LEFT:
58                 if self.state.player.move(BITS.WEST):
59                     self.state.gameboard.change_mode()
60                     PlayerMoved.post()
61                     InvalidateTheWorld.post()
62                     return True
63             if ev.key in KEYS.RIGHT:
64                 if self.state.player.move(BITS.EAST):
65                     self.state.gameboard.change_mode()
66                     PlayerMoved.post()
67                     InvalidateTheWorld.post()
68                     return True
69             if ev.key in (pgl.K_SPACE,):
70                 self.state.gameboard.change_mode()
71                 InvalidateTheWorld.post()
72                 return True
73         return super(RobotWidget, self).handle_event(ev)