glyph for return
[naja.git] / naja / widgets / board.py
1 """
2 Widget that holds the game tiles.
3 """
4 import pygame.locals as pgl
5
6 from naja.constants import BOARD_SIZE, TILE_SIZE, KEYS, ACT
7 from naja.events import finish_event
8 from naja.sound import sound
9
10 from naja.widgets.base import Widget
11 from naja.widgets.tile import TileWidget
12
13
14 class BoardWidget(Widget):
15     """
16     Widget which holds all the tiles that make up the gameboard.
17     """
18     def __init__(self, pos, state, info):
19         super(BoardWidget, self).__init__(pos, BOARD_SIZE)
20         self.info = info
21         self.state = state
22         self.card_pos = state.player.position
23         self._tiles = []
24         self.legal = False
25         for y in range(0, 5):
26             for x in range(0, 5):
27                 tile_pos = (pos[0] + x * TILE_SIZE[0],
28                             pos[1] + y * TILE_SIZE[1])
29                 self._tiles.append(TileWidget(tile_pos, state, (x, y)))
30
31     def prepare(self):
32         for tile in self._tiles:
33             tile.set_highlight(self.card_pos)
34             tile.prepare()
35         self.size = BOARD_SIZE
36         if self.state.gameboard.player_mode == ACT:
37             self.card_pos = self.state.player.position
38
39     def draw(self, surface):
40         for tile in self._tiles:
41             tile.draw(surface)
42
43     def update_card_pos(self, card_pos):
44         self.card_pos = card_pos
45         self.info.set_position(self.card_pos)
46         self.legal = self.card_pos in self.state.player.legal_moves()
47         sound.play_sound('zoop.ogg', volume=0.05)
48
49     def change_pos(self, offset):
50         tpos = (offset[0] + self.card_pos[0], offset[1] + self.card_pos[1])
51         if tpos[0] < 0 or tpos[1] < 0 or tpos[0] > 4 or tpos[1] > 4:
52             return False
53         self.update_card_pos(tpos)
54         return True
55
56     def next_viable_pos(self):
57         moves = self.state.player.legal_moves()
58         try:
59             idx = moves.index(self.card_pos)
60             idx = (idx + 1) % len(moves)
61         except ValueError:
62             idx = 0
63         self.update_card_pos(moves[idx])
64
65     def handle_event(self, ev):
66         if self.state.gameboard.player_mode == ACT:
67             return super(BoardWidget, self).handle_event(ev)
68         if ev.type == pgl.KEYDOWN:
69             if ev.key in KEYS.UP:
70                 if self.change_pos((0, -1)):
71                     return finish_event()
72             elif ev.key in KEYS.DOWN:
73                 if self.change_pos((0, +1)):
74                     return finish_event()
75             elif ev.key in KEYS.LEFT:
76                 if self.change_pos((-1, 0)):
77                     return finish_event()
78             elif ev.key in KEYS.RIGHT:
79                 if self.change_pos((+1, 0)):
80                     return finish_event()
81             elif ev.key in KEYS.SWITCH:
82                 self.next_viable_pos()
83                 return finish_event()
84             elif ev.key in KEYS.SELECT:
85                 if self.state.player.set_position(self.card_pos):
86                     self.state.gameboard.change_mode(ACT)
87                     self.info.set_position(self.card_pos)
88                     sound.play_sound('chirp.ogg', volume=0.5)
89                     return finish_event()
90         return super(BoardWidget, self).handle_event(ev)