2 Widget for the game board information area.
5 import pygame.locals as pgl
7 from naja.constants import (
8 INFO_SIZE, ACT, KEYS, EXAMINE, PALETTE,
9 ACTION_TEXT_OFFSET, INFO_LEFT_PADDING,
10 INFO_RIGHT_PADDING, BIT_SIZE, BITS)
11 from naja.events import 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
17 from naja.widgets.base import Widget
18 from naja.widgets.tile import BIT_MAP
19 from naja.widgets.text import TextBoxWidget, TextWidget
20 from naja import constants
24 ACT: "Choose an action using {NORTH,SOUTH} keys.\n"
25 "Press {RETURN} to execute it.",
26 EXAMINE: "Browse the tiles with {NORTH,SOUTH,EAST,WEST} keys.",
29 HINT_LEGAL_MOVE = "\nPress {RETURN} to move to this tile."
32 ACT: "Choose an Action",
33 EXAMINE: "Select a Tile",
37 class InfoAreaWidget(Widget):
39 Widget for the game board information area.
41 def __init__(self, pos, state):
42 super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
45 self.card_position = state.player.position
46 self.set_position(state.player.position)
48 self.flash_light = True
51 if self.state.gameboard.player_mode == ACT:
52 self.set_position(self.state.player.position)
53 self.surface = pygame.surface.Surface(INFO_SIZE)
54 self.surface.fill((0, 0, 0))
56 box_width = INFO_SIZE[0] - INFO_LEFT_PADDING - INFO_RIGHT_PADDING
58 pos = lambda: (INFO_LEFT_PADDING, y_offset)
62 pos(), TITLES[self.state.gameboard.player_mode],
64 title.render(self.surface)
65 y_offset += title.surface.get_rect().height - 4
68 bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
69 for bit in reversed(range(8)))
70 if self.card.bitwise_operand:
71 bits_text = '%s %s' % (
72 bits_text, bit_glyphs(self.card.bitwise_operand))
73 card_bits = TextBoxWidget(
74 pos(), bits_text, box_width=box_width,
75 colour=PALETTE.LIGHT_TURQUOISE, bg_colour=PALETTE.BLACK)
76 card_bits.render(self.surface)
77 y_offset += card_bits.surface.get_rect().height + 4
80 for choice, action in enumerate(self.card.actions):
81 y_offset = self.prepare_action(choice, action, y_offset, box_width)
84 hint_text = HINTS[self.state.gameboard.player_mode]
85 if self.state.gameboard.player_mode == EXAMINE:
86 if self.card_position in self.state.player.legal_moves():
87 hint_text += HINT_LEGAL_MOVE
90 (0, 0), hint_text, padding=4,
91 colour=PALETTE.WHITE, border=2,
92 bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
95 y_offset = (INFO_SIZE[1] - hint.surface.get_rect().height
97 self.surface.blit(hint.surface, pos())
99 # Game mode (puzzle, easy, standard, hard, etc ...)
100 game_mode = TextBoxWidget(
101 (0, 0), self.mode_hint(), padding=4, centre=True,
102 colour=PALETTE.WHITE, border=2,
103 bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
107 y_offset = (INFO_SIZE[1] - BIT_SIZE[1] + 12)
108 self.surface.blit(game_mode.surface, pos())
111 gameboard = self.state.gameboard
115 0: "DEATH", 1: "LUDICROUS",
116 2: "RAMBO", 3: "HARD",
117 4: "STANDARD", 5: "EASY",
118 }.get(gameboard.max_health, "UNKNOWN")
120 def prepare_action(self, choice, action, y_offset, box_width):
121 x_offset = INFO_LEFT_PADDING
123 glyphs_y_offset = y_offset
124 y_offset += ACTION_TEXT_OFFSET
125 action_viable = action.check_available(self.state.player)
126 text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
128 text = TextBoxWidget(
129 (x_offset, y_offset), action.get_text(self.card),
131 fontsize=28, colour=text_colour,
132 border=2, border_colour=PALETTE.GREY)
133 text.render(self.surface)
136 if choice == self.chosen:
138 border_colour = (PALETTE.GREEN if action_viable else
141 border_colour = (PALETTE.DARK_GREEN if action_viable else
145 bottom = y_offset + text.surface.get_rect().height
146 right = text.surface.get_rect().width + x_offset
147 pygame.draw.lines(self.surface, border_colour, True,
148 [(x_offset, y_offset), (right, y_offset),
149 (right, bottom), (x_offset, bottom)], 4)
151 required_keys = action.required_bits & frozenset([
152 BITS.RED, BITS.GREEN, BITS.BLUE])
153 if required_keys in BIT_MAP:
154 img_name = BIT_MAP[required_keys].replace(
155 '.png', '_small.png')
156 img = resources.get_image(img_name,
157 transforms=(EIGHT_BIT,))
158 self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
159 glyphs_x_offset += img.get_width() + 4
161 glyphs_x_offset = INFO_LEFT_PADDING
163 if BITS.MSB in action.required_bits:
164 msb = resources.get_image('board/msb_lock_decoration.png',
165 transforms=(EIGHT_BIT,))
166 msb_rect = msb.get_rect()
168 msb, (glyphs_x_offset - msb_rect.width - 4, glyphs_y_offset)
171 for glyph in action.get_glyphs():
172 img = resources.get_image(
173 glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
174 self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
175 glyphs_x_offset += img.get_width()
176 if action.get_msb_glyph() is not None:
177 img = resources.get_image(
178 action.get_msb_glyph(),
179 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
180 self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
182 return y_offset + text.surface.get_rect().height + 16
184 def set_position(self, position):
185 self.card_position = position
186 self.card = self.state.board_locations[self.card_position]
187 if self.state.gameboard.player_mode == ACT:
188 if self.chosen is None:
193 def draw(self, surface):
194 self.flash_count += 1
195 if self.flash_count >= (constants.FPS // 2):
196 self.flash_light = not self.flash_light
199 surface.blit(self.surface, self.pos)
201 def next_action(self, viable_only=False, step=1):
202 num_actions = len(self.card.actions)
205 player = self.state.player
207 for i in range(num_actions - 1):
208 # loop through each action at most once.
209 chosen = (chosen + step) % num_actions
210 action = self.card.actions[chosen]
211 if not viable_only or action.check_available(player):
212 sound.play_sound('zoop.ogg', volume=0.05)
215 def prev_action(self, viable_only=False):
216 return self.next_action(viable_only=viable_only, step=-1)
218 def try_perform_action(self):
219 player = self.state.player
220 action = self.card.actions[self.chosen]
221 if not action.check_available(player):
222 sound.play_sound('error.ogg')
224 sound.play_sound('chirp.ogg', volume=0.5)
225 action.perform_action(self.state.gameboard, self.card)
226 self.state.gameboard.card_used(player.position)
227 self.state.gameboard.change_mode(EXAMINE)
228 self.set_position(player.position)
230 def handle_event(self, ev):
231 if self.state.gameboard.player_mode == EXAMINE:
232 return super(InfoAreaWidget, self).handle_event(ev)
233 if ev.type == pgl.KEYDOWN:
234 if ev.key in KEYS.SELECT:
235 self.try_perform_action()
236 return finish_event()
237 if ev.key in KEYS.UP:
239 return finish_event()
240 if ev.key in KEYS.DOWN:
242 return finish_event()
243 if ev.key in KEYS.SWITCH:
244 self.next_action(viable_only=True)
245 return finish_event()
246 return super(InfoAreaWidget, self).handle_event(ev)