2 Widget for the game board information area.
5 import pygame.locals as pgl
7 from naja.constants import (
8 INFO_SIZE, KEYS, PALETTE,
9 ACTION_TEXT_OFFSET, INFO_LEFT_PADDING,
10 INFO_RIGHT_PADDING, BIT_SIZE, BITS)
11 from naja.events import InvalidateTheWorld, finish_event
12 from naja.resources import resources
13 from naja.resources.mutators import EIGHT_BIT, blender
14 from naja.sound import sound
15 from naja.utils import bit_glyphs, Flashlight
17 from naja.widgets.base import Widget
18 from naja.widgets.tile import BIT_MAP
19 from naja.widgets.text import TextBoxWidget
20 from naja import constants
24 "Browse the tiles with {NORTH,SOUTH,EAST,WEST} keys.",
25 "Switch actions using TAB.",
28 HINT_LEGAL_MOVE = "\nPress {RETURN} to move and execute the action."
31 class InfoAreaWidget(Widget):
33 Widget for the game board information area.
35 def __init__(self, pos, state):
36 super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
38 self.set_position(state.player.position, legal=True)
39 self.flash_light = Flashlight(constants.FPS // 2)
42 self.surface = pygame.surface.Surface(INFO_SIZE)
43 self.surface.fill((0, 0, 0))
45 box_width = INFO_SIZE[0] - INFO_LEFT_PADDING - INFO_RIGHT_PADDING
47 pos = lambda: (INFO_LEFT_PADDING, y_offset)
51 bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
52 for bit in reversed(range(8)))
53 if self.card.bitwise_operand:
54 bits_text = '%s %s' % (
55 bits_text, bit_glyphs(self.card.bitwise_operand))
56 card_bits = TextBoxWidget(
57 (0, 0), bits_text, padding=4, centre=True,
58 colour=PALETTE.WHITE, border=2,
59 bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
62 self.surface.blit(card_bits.surface, pos())
63 y_offset += card_bits.surface.get_rect().height + 12
66 for choice, action in enumerate(self.card.actions):
67 y_offset = self.prepare_action(choice, action, y_offset, box_width)
70 hint_text = " ".join(HINTS)
71 if self.card_position in self.state.player.legal_moves():
72 hint_text += HINT_LEGAL_MOVE
75 (0, 0), hint_text, padding=4,
76 colour=PALETTE.WHITE, border=2,
77 bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
80 y_offset = (INFO_SIZE[1] - hint.surface.get_rect().height
82 self.surface.blit(hint.surface, pos())
84 # Game mode (puzzle, easy, standard, hard, etc ...)
85 game_mode = TextBoxWidget(
86 (0, 0), self.mode_hint(), padding=4, centre=True,
87 colour=PALETTE.WHITE, border=2,
88 bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
92 y_offset = (INFO_SIZE[1] - BIT_SIZE[1] + 12)
93 self.surface.blit(game_mode.surface, pos())
96 gameboard = self.state.gameboard
100 0: "DEATH", 1: "NINTENDO HARD",
101 2: "VERY HARD", 3: "HARD",
102 4: "STANDARD", 5: "EASY",
103 }.get(gameboard.max_health, "UNKNOWN")
105 def prepare_action(self, choice, action, y_offset, box_width):
106 x_offset = INFO_LEFT_PADDING
108 glyphs_y_offset = y_offset
109 y_offset += ACTION_TEXT_OFFSET
111 action.check_available(self.state.player) and self.legal)
112 text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
114 text = TextBoxWidget(
115 (x_offset, y_offset), action.get_text(self.card),
117 fontsize=28, colour=text_colour,
118 border=2, border_colour=PALETTE.GREY)
119 text.render(self.surface)
122 if choice == self.chosen:
123 if self.flash_light.on:
124 border_colour = (PALETTE.GREEN if action_viable else
127 border_colour = (PALETTE.DARK_GREEN if action_viable else
131 bottom = y_offset + text.surface.get_rect().height
132 right = text.surface.get_rect().width + x_offset
133 pygame.draw.lines(self.surface, border_colour, True,
134 [(x_offset, y_offset), (right, y_offset),
135 (right, bottom), (x_offset, bottom)], 4)
137 required_keys = action.required_bits & frozenset([
138 BITS.RED, BITS.GREEN, BITS.BLUE])
139 if required_keys in BIT_MAP:
140 img_name = BIT_MAP[required_keys].replace(
141 '.png', '_small.png')
142 img = resources.get_image(img_name,
143 transforms=(EIGHT_BIT,))
144 self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
145 glyphs_x_offset += img.get_width() + 4
147 glyphs_x_offset = INFO_LEFT_PADDING
149 if BITS.MSB in action.required_bits:
150 msb = resources.get_image('board/msb_lock_decoration.png',
151 transforms=(EIGHT_BIT,))
152 msb_rect = msb.get_rect()
154 msb, (glyphs_x_offset - msb_rect.width - 4, glyphs_y_offset)
157 for glyph in action.get_glyphs():
158 img = resources.get_image(
159 glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
160 self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
161 glyphs_x_offset += img.get_width()
162 if action.get_msb_glyph() is not None:
163 img = resources.get_image(
164 action.get_msb_glyph(),
165 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
166 self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
168 return y_offset + text.surface.get_rect().height + 16
170 def set_position(self, position, legal):
172 self.card_position = position
173 self.card = self.state.board_locations[self.card_position]
175 self.next_action(viable_only=True)
177 def draw(self, surface):
178 if self.flash_light.tick():
180 surface.blit(self.surface, self.pos)
182 def next_action(self, viable_only=False, step=1):
183 num_actions = len(self.card.actions)
186 player = self.state.player
187 chosen = self.chosen if self.chosen is not None else -step
188 for i in range(num_actions):
189 # loop through each action at most once.
190 chosen = (chosen + step) % num_actions
191 action = self.card.actions[chosen]
192 if not viable_only or action.check_available(player):
193 sound.play_sound('zoop.ogg', volume=0.05)
197 def prev_action(self, viable_only=False):
198 return self.next_action(viable_only=viable_only, step=-1)
200 def try_perform_action(self):
201 player = self.state.player
202 if self.chosen is None or not self.legal:
205 action = self.card.actions[self.chosen]
206 if action is None or not action.check_available(player):
207 sound.play_sound('error.ogg')
210 sound.play_sound('chirp.ogg', volume=0.5)
211 player.set_position(self.card_position)
212 action.perform_action(self.state.gameboard, self.card)
213 self.state.gameboard.card_used(self.card_position)
214 self.set_position(self.card_position, legal=True)
217 def handle_event(self, ev):
218 if InvalidateTheWorld.matches(ev):
219 self.flash_light.reset()
220 if ev.type == pgl.KEYDOWN:
221 if ev.key in KEYS.SWITCH:
222 self.next_action(viable_only=True)
223 return finish_event()
224 elif ev.key in KEYS.SELECT:
225 self.try_perform_action()
226 return finish_event()
227 return super(InfoAreaWidget, self).handle_event(ev)