2 Widget that holds the game tiles.
4 import pygame.locals as pgl
6 from naja.constants import BOARD_SIZE, TILE_SIZE, KEYS, ACT, FPS
7 from naja.events import InvalidateTheWorld, finish_event
8 from naja.sound import sound
10 from naja.widgets.base import Widget
11 from naja.widgets.tile import TileWidget
12 from naja.utils import Flashlight
15 class BoardWidget(Widget):
17 Widget which holds all the tiles that make up the gameboard.
19 def __init__(self, pos, state, info):
20 super(BoardWidget, self).__init__(pos, BOARD_SIZE)
23 self.card_pos = state.player.position
26 self.flash_light = Flashlight(FPS // 2)
29 tile_pos = (pos[0] + x * TILE_SIZE[0],
30 pos[1] + y * TILE_SIZE[1])
31 self._tiles.append(TileWidget(tile_pos, state, (x, y)))
34 for tile in self._tiles:
35 tile.set_highlight(self.card_pos, self.flash_light.on)
37 self.size = BOARD_SIZE
38 if self.state.gameboard.player_mode == ACT:
39 self.card_pos = self.state.player.position
41 def draw(self, surface):
42 if self.flash_light.tick():
44 for tile in self._tiles:
47 def update_card_pos(self, card_pos):
48 self.card_pos = card_pos
49 self.info.set_position(self.card_pos)
50 self.legal = self.card_pos in self.state.player.legal_moves()
51 sound.play_sound('zoop.ogg', volume=0.05)
53 def change_pos(self, offset):
54 tpos = (offset[0] + self.card_pos[0], offset[1] + self.card_pos[1])
55 if tpos[0] < 0 or tpos[1] < 0 or tpos[0] > 4 or tpos[1] > 4:
57 self.update_card_pos(tpos)
60 def next_viable_pos(self):
61 moves = self.state.player.legal_moves()
63 idx = moves.index(self.card_pos)
64 idx = (idx + 1) % len(moves)
67 self.update_card_pos(moves[idx])
69 def force_skip_animation(self):
70 for tile in self._tiles:
73 def handle_event(self, ev):
74 if InvalidateTheWorld.matches(ev):
75 self.flash_light.reset()
76 if self.state.gameboard.player_mode == ACT:
77 return super(BoardWidget, self).handle_event(ev)
78 if ev.type == pgl.KEYDOWN:
80 if self.change_pos((0, -1)):
82 elif ev.key in KEYS.DOWN:
83 if self.change_pos((0, +1)):
85 elif ev.key in KEYS.LEFT:
86 if self.change_pos((-1, 0)):
88 elif ev.key in KEYS.RIGHT:
89 if self.change_pos((+1, 0)):
91 elif ev.key in KEYS.SWITCH:
92 self.next_viable_pos()
94 elif ev.key in KEYS.SELECT:
95 if self.state.player.set_position(self.card_pos):
96 self.state.gameboard.change_mode(ACT)
97 self.info.set_position(self.card_pos)
98 sound.play_sound('chirp.ogg', volume=0.5)
100 return super(BoardWidget, self).handle_event(ev)