2 Widget that holds the game tiles.
4 import pygame.locals as pgl
6 from naja.constants import BOARD_SIZE, TILE_SIZE, KEYS, ACT
7 from naja.events import finish_event
8 from naja.sound import sound
10 from naja.widgets.base import Widget
11 from naja.widgets.tile import TileWidget
14 class BoardWidget(Widget):
16 Widget which holds all the tiles that make up the gameboard.
18 def __init__(self, pos, state, info):
19 super(BoardWidget, self).__init__(pos, BOARD_SIZE)
22 self.card_pos = state.player.position
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)))
32 for tile in self._tiles:
33 tile.set_highlight(self.card_pos)
35 self.size = BOARD_SIZE
36 if self.state.gameboard.player_mode == ACT:
37 self.card_pos = self.state.player.position
39 def draw(self, surface):
40 for tile in self._tiles:
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)
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:
53 self.update_card_pos(tpos)
56 def next_viable_pos(self):
57 moves = self.state.player.legal_moves()
59 idx = moves.index(self.card_pos)
60 idx = (idx + 1) % len(moves)
63 self.update_card_pos(moves[idx])
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:
70 if self.change_pos((0, -1)):
72 elif ev.key in KEYS.DOWN:
73 if self.change_pos((0, +1)):
75 elif ev.key in KEYS.LEFT:
76 if self.change_pos((-1, 0)):
78 elif ev.key in KEYS.RIGHT:
79 if self.change_pos((+1, 0)):
81 elif ev.key in KEYS.SWITCH:
82 self.next_viable_pos()
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)
90 return super(BoardWidget, self).handle_event(ev)