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
23 ACT: "Choose an action using {NORTH,SOUTH} keys.\n"
24 "Press {RETURN} to execute it.",
25 EXAMINE: "Browse the tiles with {NORTH,SOUTH,EAST,WEST} keys.",
28 HINT_LEGAL_MOVE = "\nPress {RETURN} to move to this tile."
31 ACT: "Choose an Action",
32 EXAMINE: "Select a Tile",
36 class InfoAreaWidget(Widget):
38 Widget for the game board information area.
40 def __init__(self, pos, state):
41 super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
44 self.card_position = state.player.position
45 self.set_position(state.player.position)
48 if self.state.gameboard.player_mode == ACT:
49 self.set_position(self.state.player.position)
50 self.surface = pygame.surface.Surface(INFO_SIZE)
51 self.surface.fill((0, 0, 0))
53 box_width = INFO_SIZE[0] - INFO_LEFT_PADDING - INFO_RIGHT_PADDING
55 pos = lambda: (INFO_LEFT_PADDING, y_offset)
59 bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
60 for bit in reversed(range(8)))
61 if self.card.bitwise_operand:
62 bits_text = '%s %s' % (
63 bits_text, bit_glyphs(self.card.bitwise_operand))
64 card_bits = TextBoxWidget(
65 (0, 0), bits_text, padding=4, centre=True,
66 colour=PALETTE.WHITE, border=2,
67 bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
70 self.surface.blit(card_bits.surface, pos())
71 y_offset += card_bits.surface.get_rect().height + 12
74 for choice, action in enumerate(self.card.actions):
75 y_offset = self.prepare_action(choice, action, y_offset, box_width)
78 hint_text = HINTS[self.state.gameboard.player_mode]
79 if self.state.gameboard.player_mode == EXAMINE:
80 if self.card_position in self.state.player.legal_moves():
81 hint_text += HINT_LEGAL_MOVE
84 (0, 0), hint_text, padding=4,
85 colour=PALETTE.WHITE, border=2,
86 bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
89 y_offset = (INFO_SIZE[1] - hint.surface.get_rect().height
91 self.surface.blit(hint.surface, pos())
93 # Game mode (puzzle, easy, standard, hard, etc ...)
94 game_mode = TextBoxWidget(
95 (0, 0), self.mode_hint(), padding=4, centre=True,
96 colour=PALETTE.WHITE, border=2,
97 bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
101 y_offset = (INFO_SIZE[1] - BIT_SIZE[1] + 12)
102 self.surface.blit(game_mode.surface, pos())
105 gameboard = self.state.gameboard
109 0: "DEATH", 1: "LUDICROUS",
110 2: "RAMBO", 3: "HARD",
111 4: "STANDARD", 5: "EASY",
112 }.get(gameboard.max_health, "UNKNOWN")
114 def prepare_action(self, choice, action, y_offset, box_width):
115 x_offset = INFO_LEFT_PADDING
117 glyphs_y_offset = y_offset
118 y_offset += ACTION_TEXT_OFFSET
119 action_viable = action.check_available(self.state.player)
120 text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
122 text = TextBoxWidget(
123 (x_offset, y_offset), action.get_text(self.card),
125 fontsize=28, colour=text_colour,
126 border=2, border_colour=PALETTE.GREY)
127 text.render(self.surface)
130 if choice == self.chosen:
131 border_colour = PALETTE.GREEN if action_viable else PALETTE.ORANGE
133 bottom = y_offset + text.surface.get_rect().height
134 right = text.surface.get_rect().width + x_offset
135 pygame.draw.lines(self.surface, border_colour, True,
136 [(x_offset, y_offset), (right, y_offset),
137 (right, bottom), (x_offset, bottom)], 4)
139 required_keys = action.required_bits & frozenset([
140 BITS.RED, BITS.GREEN, BITS.BLUE])
141 if required_keys in BIT_MAP:
142 img_name = BIT_MAP[required_keys].replace(
143 '.png', '_small.png')
144 img = resources.get_image(img_name,
145 transforms=(EIGHT_BIT,))
146 self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
147 glyphs_x_offset += img.get_width() + 4
149 glyphs_x_offset = INFO_LEFT_PADDING
151 if BITS.MSB in action.required_bits:
152 msb = resources.get_image('board/msb_lock_decoration.png',
153 transforms=(EIGHT_BIT,))
154 msb_rect = msb.get_rect()
156 msb, (glyphs_x_offset - msb_rect.width - 4, glyphs_y_offset)
159 for glyph in action.get_glyphs():
160 img = resources.get_image(
161 glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
162 self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
163 glyphs_x_offset += img.get_width()
164 if action.get_msb_glyph() is not None:
165 img = resources.get_image(
166 action.get_msb_glyph(),
167 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
168 self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
170 return y_offset + text.surface.get_rect().height + 16
172 def set_position(self, position):
173 self.card_position = position
174 self.card = self.state.board_locations[self.card_position]
175 if self.state.gameboard.player_mode == ACT:
176 if self.chosen is None:
181 def draw(self, surface):
182 surface.blit(self.surface, self.pos)
184 def next_action(self, viable_only=False, step=1):
185 num_actions = len(self.card.actions)
188 player = self.state.player
190 for i in range(num_actions - 1):
191 # loop through each action at most once.
192 chosen = (chosen + step) % num_actions
193 action = self.card.actions[chosen]
194 if not viable_only or action.check_available(player):
195 sound.play_sound('zoop.ogg', volume=0.05)
198 def prev_action(self, viable_only=False):
199 return self.next_action(viable_only=viable_only, step=-1)
201 def try_perform_action(self):
202 player = self.state.player
203 action = self.card.actions[self.chosen]
204 if not action.check_available(player):
205 sound.play_sound('error.ogg')
207 sound.play_sound('chirp.ogg', volume=0.5)
208 action.perform_action(self.state.gameboard, self.card)
209 self.state.gameboard.card_used(player.position)
210 self.state.gameboard.change_mode(EXAMINE)
211 self.set_position(player.position)
213 def handle_event(self, ev):
214 if self.state.gameboard.player_mode == EXAMINE:
215 return super(InfoAreaWidget, self).handle_event(ev)
216 if ev.type == pgl.KEYDOWN:
217 if ev.key in KEYS.SELECT:
218 self.try_perform_action()
219 return finish_event()
220 if ev.key in KEYS.UP:
222 return finish_event()
223 if ev.key in KEYS.DOWN:
225 return finish_event()
226 if ev.key in KEYS.SWITCH:
227 self.next_action(viable_only=True)
228 return finish_event()
229 return super(InfoAreaWidget, self).handle_event(ev)