Permalink
Browse files

Implemented hint panel

  • Loading branch information...
1 parent ccd34a0 commit 9ff1f0ffc74050195e24aa40daaca6d0bab6f1de @frgorp committed Apr 2, 2013
Showing with 160 additions and 18 deletions.
  1. +1 −0 risk/graphics/assets/dialog.py
  2. +30 −0 risk/graphics/assets/text.py
  3. +20 −1 risk/graphics/graphics.py
  4. +109 −17 risk/graphics/input.py
@@ -175,6 +175,7 @@ def get_result(self, poll_sleep):
self.drag_dialog()
elif self.finished_button.mouse_hovering(event.pos):
done = self.finished_button.confirmed_click()
+ #print done
return self.current
def reset(self):
@@ -22,6 +22,36 @@ def render_text(self, msg):
font.set_bold(self.bold)
self.surface = font.render(msg, False, self.colour)
+# supports multiline
+class CentredTextAsset(TextAsset):
+ def __init__(self, x, y, width, height, msg,
+ colour=assets.base.BLACK, size=32, bold=False):
+ self.width = width
+ self.height = height
+ TextAsset.__init__(self, x, y, msg, colour, size, bold)
+
+ def render_text(self, msg):
+ font = Font(_FONT, self.size)
+ font.set_bold(self.bold)
+ new_surface = pygame.Surface((self.width, self.height),
+ pygame.SRCALPHA, 32)
+ msg = msg.strip("\n")
+ lines = msg.split("\n")
+ longest = max(lines, key=lambda x: len(x))
+
+ #new_surface = pygame.Surface((self.width, self.height))
+ temp_surface = font.render(longest, False, self.colour)
+ msg_width = temp_surface.get_size()[0]
+ msg_height = font.get_height() * len(lines)
+ msg_x = (new_surface.get_width() - msg_width) / 2
+ msg_y = (new_surface.get_height() - msg_height) / 2
+ for index, line in enumerate(lines):
+ font_surface = font.render(line, False, self.colour)
+ new_surface.blit(font_surface, (msg_x,
+ msg_y + (font.get_height() * index)))
+ self.surface = new_surface
+
+
class CurrentPlayerAsset(TextAsset):
def __init__(self, x, y, game_master):
self.game_master = game_master
View
@@ -28,7 +28,10 @@
DEFAULT_BACKGROUND = 'resources/risk_board.png'
DEFAULT_OVERLAY = 'assets/art/gui/main_borders.png'
-buttons = {}
+INFO_PANEL_X = 370
+INFO_PANEL_Y = 585
+INFO_PANEL_WIDTH = 410
+INFO_PANEL_HEIGHT = 110
territory_coordinates = {
'north_america': {
@@ -113,6 +116,7 @@ def shutdown(*args):
def add_graphic_hooks(game_master):
game_master.add_start_turn_callback(check_picasso_liveness)
+ game_master.add_start_turn_callback(show_bot_player_hint)
#game_master.add_start_turn_callback(show_human_player)
game_master.add_start_turn_callback(delay)
game_master.add_start_turn_callback(check_gui_quit_event)
@@ -223,6 +227,21 @@ def update_current_phase(game_master, previous, current):
for state, asset in Datastore().get_storage('states').iteritems():
asset.set_state(state == current)
+def show_bot_player_hint(game_master):
+ datastore = Datastore()
+ picasso = get_picasso()
+ if not datastore.has_entry('bot_player_hint'):
+ hint_asset = assets.text.CentredTextAsset(INFO_PANEL_X, INFO_PANEL_Y,
+ INFO_PANEL_WIDTH, INFO_PANEL_HEIGHT,
+ "AI TAKING TURNS...",
+ bold=True)
+ datastore.add_entry('bot_player_hint', hint_asset)
+ hint_asset = datastore.get_entry('bot_player_hint')
+ if isinstance(game_master.current_player(), risk.ai.bots.BasicRiskBot):
+ picasso.add_asset('999', hint_asset)
+ else:
+ picasso.remove_asset('999', hint_asset)
+
def release_control(game_master, *args):
# release CPU for faster screen update
time.sleep(0)
View
@@ -21,18 +21,80 @@
from risk.graphics.datastore import Datastore
from risk.graphics.picasso import get_picasso
from risk.graphics.assets.player import *
-from risk.graphics.assets.text import TextAsset
+from risk.graphics.assets.text import TextAsset, CentredTextAsset
from risk.graphics.assets.territory import TerritoryAsset
from risk.graphics.assets.dialog import BlockingSliderDialogAsset
from risk.graphics.assets.message import PopupDialogAsset
-from risk.errors.game_master import NotConnected
+from risk.errors.game_master import NotConnected, TerritoryNotOwnedByPlayer
LAYER = '5_player_feedback'
+DIALOG_LAYER = '6_player_dialog'
+POPUP_DIALOG_LAYER = '7_popup_dialogs'
INPUT_POLL_SLEEP = 0.01
MAX_INPUT_LENGTH = 3
DIALOG_X = 400
DIALOG_Y = 300
+REINFORCE_HINTS = {
+ 'initial':
+"""
+ == REINFORCE ==
+
+Click on a territory to add an army to it.
+
+ You current have %s armies to add!
+"""
+}
+
+ATTACK_HINTS = {
+ 'initial':
+"""
+ == ATTACK ==
+
+Click on your own territory to begin
+attacking!
+Click on the 'next' button to begin fortifying.
+""",
+ 'attacking':
+"""
+ == Choose Target ==
+
+Select an adjacent enemy territory to attack.
+Click anywhere else to cancel.
+""",
+ 'failed':
+"""
+ == Attack Failed ==
+
+You've failed the attack! Army reduced to 1...
+""",
+ 'success':
+"""
+ == Attack Succeeded ==
+
+Great success! You've captured the enemy
+territory!
+"""
+}
+
+FORTIFY_HINTS = {
+ 'initial':
+"""
+ == FORTIFY ==
+
+Click on your own territory to begin
+fortifying!
+Click on the 'next' bitton to end turn.
+""",
+ 'fortifying':
+"""
+ == Choose Target ==
+
+Select a connected friendly territory to
+move armies.
+"""
+}
+
def slider_update(dialog, origin, target):
datastore = Datastore()
if not hasattr(dialog, 'show_origin_armies'):
@@ -101,6 +163,11 @@ def wait_for_territory_click(allow_fail=False):
if allow_fail:
return None
+def build_hint_asset(hint):
+ return CentredTextAsset(graphics.INFO_PANEL_X, graphics.INFO_PANEL_Y,
+ graphics.INFO_PANEL_WIDTH, graphics.INFO_PANEL_HEIGHT, hint,
+ size=16, bold=True)
+
###############################################################################
## Reinforce phase DFA
#
@@ -114,6 +181,9 @@ def reinforce_phase(player, game_master):
# core state machine
disable_enemy_territories(player)
while player.reserves > 0:
+ hint_asset = build_hint_asset(REINFORCE_HINTS['initial'] % \
+ player.reserves)
+ picasso.add_asset(LAYER, hint_asset)
event = wait_for_mouse_click()
for name, clickable in graphics.pressed_clickables(event.pos,
'territories'):
@@ -124,6 +194,7 @@ def reinforce_phase(player, game_master):
reinforce_add_army(player, game_master, territory)
except GameMasterError:
reinforce_add_army_fail(player, game_master, territory)
+ picasso.remove_asset(LAYER, hint_asset)
# exit state
enable_all_clickables()
#picasso.remove_asset(LAYER, reserve_count_asset)
@@ -134,47 +205,52 @@ def reinforce_add_army(player, game_master, territory, number_of_armies=1):
def reinforce_add_army_fail(player, game_master, territory):
risk.logger.debug("%s does not own %s" % (player.name, territory.name))
dialog = PopupDialogAsset(DIALOG_X, DIALOG_Y, "Reinforce Failed!", "penis")
- get_picasso().add_asset(LAYER, dialog)
+ get_picasso().add_asset(POPUP_DIALOG_LAYER, dialog)
dialog.get_confirmation()
- get_picasso().remove_asset(LAYER, dialog)
+ get_picasso().remove_asset(POPUP_DIALOG_LAYER, dialog)
###############################################################################
## Attack phase DFA
#
def attack_phase(player, game_master):
+ picasso = get_picasso()
done = False
+ hint_asset = build_hint_asset(ATTACK_HINTS['initial'])
while not done:
+ picasso.add_asset(LAYER, hint_asset)
disable_enemy_territories(player)
event = wait_for_mouse_click()
for name, clickable in get_clicked_territories(event.pos):
if isinstance(clickable, TerritoryAsset):
if clickable.territory.owner == player:
+ picasso.remove_asset(LAYER, hint_asset)
attack_choose_target(player, game_master,
clickable)
for name, clickable in get_clicked_buttons(event.pos):
if name == 'next':
done = True
+
+ picasso.remove_asset(LAYER, hint_asset)
enable_all_clickables()
def attack_choose_target(player, game_master, origin_asset):
origin = origin_asset.territory
picasso = get_picasso()
- datastore = Datastore()
- feedback_asset = datastore.get_entry('attack_feedback')
- picasso.add_asset(LAYER, feedback_asset)
+ hint_asset = build_hint_asset(ATTACK_HINTS['attacking'])
+ picasso.add_asset(LAYER, hint_asset)
disable_all_clickables()
origin_asset.force_highlight = True
enable_adjacent_territories(origin)
target_asset = wait_for_territory_click(allow_fail=True)
target = None
+ picasso.remove_asset(LAYER, hint_asset)
if target_asset:
target = target_asset.territory
if not target:
pass
else:
attack_perform_attack(player, game_master, origin, target)
- picasso.remove_asset(LAYER, feedback_asset)
origin_asset.force_highlight = False
enable_all_clickables()
@@ -195,12 +271,14 @@ def attack_perform_attack(player, game_master, origin, target):
def attack_success_move_armies(player, game_master, origin, target):
picasso = get_picasso()
+ hint_asset = build_hint_asset(ATTACK_HINTS['success'])
+ picasso.add_asset(LAYER, hint_asset)
dialog = BlockingSliderDialogAsset(DIALOG_X, DIALOG_Y, 'Attack Move', 1,
origin.armies, slider_update, [origin, target])
dialog.add_text(16, 16, "Attack was successful!")
dialog.add_text(16, 32, "How many armies to move?")
disable_all_clickables()
- picasso.add_asset(LAYER, dialog)
+ picasso.add_asset(DIALOG_LAYER, dialog)
done = False
while not done:
number_to_move = dialog.get_result(INPUT_POLL_SLEEP)
@@ -215,31 +293,43 @@ def attack_success_move_armies(player, game_master, origin, target):
# we really shouldn't get a parsing error from numeric dialog
raise
enable_all_clickables()
- picasso.remove_asset(LAYER, dialog)
+ picasso.remove_asset(DIALOG_LAYER, dialog)
+ picasso.remove_asset(LAYER, hint_asset)
def attack_failed(player, game_master, origin, target):
- pass
+ picasso = get_picasso()
+ hint_asset = build_hint_asset(ATTACK_HINTS['failed'])
+ picasso.add_asset(LAYER, hint_asset)
+ picasso.remove_asset(LAYER, hint_asset)
###############################################################################
## Fortify phase DFA
#
def fortify_phase(player, game_master):
+ picasso = get_picasso()
disable_enemy_territories(player)
+ hint_asset = build_hint_asset(FORTIFY_HINTS['initial'])
done = False
# TODO merge with attack phase block
while not done:
+ picasso.add_asset(LAYER, hint_asset)
event = wait_for_mouse_click()
for name, clickable in get_clicked_territories(event.pos):
if isinstance(clickable, TerritoryAsset):
if clickable.territory.owner == player:
+ picasso.remove_asset(LAYER, hint_asset)
fortify_choose_target(player, game_master, clickable)
for name, clickable in get_clicked_buttons(event.pos):
if name == 'next':
done = True
+ picasso.remove_asset(LAYER, hint_asset)
enable_all_clickables()
def fortify_choose_target(player, game_master, origin_asset):
+ picasso = get_picasso()
+ hint_asset = build_hint_asset(FORTIFY_HINTS['fortifying'])
+ picasso.add_asset(LAYER, hint_asset)
origin_asset.disabled = True
origin_asset.force_highlight = True
origin = origin_asset.territory
@@ -257,6 +347,7 @@ def fortify_choose_target(player, game_master, origin_asset):
TerritoryNotOwnedByPlyer(player))
origin_asset.force_highlight = False
origin_asset.disabled = False
+ picasso.remove_asset(LAYER, hint_asset)
def fortify_choose_armies_to_move(player, game_master, origin, target):
picasso = get_picasso()
@@ -265,15 +356,16 @@ def fortify_choose_armies_to_move(player, game_master, origin, target):
dialog.add_text(16, 16, "Fortifying...")
dialog.add_text(16, 35, "Select amount of armies to move")
disable_all_clickables()
- picasso.add_asset(LAYER, dialog)
+ picasso.add_asset(DIALOG_LAYER, dialog)
try:
number_to_move = dialog.get_result(INPUT_POLL_SLEEP)
- game_master.player_move_armies(player, origin.name,
- target.name, number_to_move)
+ if number_to_move > 0:
+ game_master.player_move_armies(player, origin.name,
+ target.name, number_to_move)
except GameMasterError as e:
fortify_failed(player, origin, target, e)
enable_all_clickables()
- picasso.remove_asset(LAYER, dialog)
+ picasso.remove_asset(DIALOG_LAYER, dialog)
def fortify_failed(player, origin, target, reason):
picasso = get_picasso()
@@ -284,6 +376,6 @@ def fortify_failed(player, origin, target, reason):
msg = expected[reason.__class__] if reason.__class__ in expected.keys() \
else "Unkonwn reason for failure..."
dialog = PopupDialogAsset(DIALOG_X, DIALOG_Y, "Fortify Failed!", msg)
- picasso.add_asset(LAYER, dialog)
+ picasso.add_asset(POPUP_DIALOG_LAYER, dialog)
dialog.get_confirmation()
- picasso.remove_asset(LAYER, dialog)
+ picasso.remove_asset(POPUP_DIALOG_LAYER, dialog)

0 comments on commit 9ff1f0f

Please sign in to comment.