Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented game info view panel

  • Loading branch information...
commit 56af6722e909de5a7f12e335c010d2ccff2800f0 1 parent ad7ae89
Fred Shih elElmo authored
BIN  resources/Vera-Bold.ttf
View
Binary file not shown
15 risk/game_master.py
View
@@ -21,7 +21,7 @@ class GameMaster(object):
_RISK_RULE_STARTING_RESERVES = 40
_DEPLOYS_PER_TURN = 5
- def __init__(self, board, settings, num_players=5):
+ def __init__(self, board, settings, num_players=6):
MIN_PLAYERS = 2
MAX_PLAYERS = 6
if not MIN_PLAYERS <= num_players <= MAX_PLAYERS:
@@ -162,10 +162,13 @@ def function_with_callback(self, *args):
def check_player_elimination(self, function, result, args):
if function == 'player_attack':
+ _PLAYER_ARG_INDEX = 0
if result == True:
for player in self.players:
if len(self.player_territories(player)) == 0:
self.eliminate_player(player)
+ self._recalculate_current_player_index(
+ args[_PLAYER_ARG_INDEX])
# TODO fix select next player bug
def eliminate_player(self, player):
@@ -197,8 +200,10 @@ def _get_player_with_index(self, index):
try:
return self.players[index]
except IndexError:
- raise NoSuchPlayerError(index, self.number_of_players)
+ raise NoSuchPlayerError(index, self.number_of_players())
+ def _recalculate_current_player_index(self, current_player):
+ self._current_player = self.players.index(current_player)
###########################################################################
## Player actions
@@ -226,6 +231,12 @@ def player_territories(self, player):
player_territories[name] = territory
return player_territories
+ def player_total_armies(self, player):
+ count = 0
+ for name, territory in self.player_territories(player).iteritems():
+ count += territory.armies
+ return count
+
@event_action
def player_attack(self, player, origin_name, target_name):
origin = self.player_territories(player)[origin_name]
24 risk/graphics/assets/clickable.py
View
@@ -67,3 +67,27 @@ def _normal_surface(self):
def _highlighted_surface(self):
return self.highlighted
+
+class ImageButtonAsset(ClickableAsset):
+ def __init__(self, x, y, normal_image, pressed_image):
+ normal_image = pygame.image.load(normal_image).convert_alpha()
+ pressed_image = pygame.image.load(pressed_image).convert_alpha()
+ width, height = normal_image.get_size()
+
+ ClickableAsset.__init__(self, x, y, width, height, "")
+ self.normal = normal_image
+ self.pressed = pressed_image
+ self.current = self.normal
+
+ def confirmed_click(self):
+ self.current = self.pressed
+ result = ClickableAsset.confirmed_click(self)
+ self.current = self.normal
+ return result
+
+ def _normal_surface(self):
+ return self.current
+
+ def _highlighted_surface(self):
+ return self.current
+
54 risk/graphics/assets/gameplay.py
View
@@ -0,0 +1,54 @@
+import pygame
+
+import risk
+
+import risk.graphics.assets
+
+from .base import PicassoAsset
+from .base import WHITE, LIGHT_BROWN
+from .text import TextAsset
+from .territory import TerritoryAsset
+
+DEFAULT_WIDTH = 250
+DEFAULT_HEIGHT = 140
+DEFAULT_X_OFFSET = 5
+COLOUR_BLOCK_WIDTH = 10
+
+class PlayersAsset(PicassoAsset):
+ def __init__(self, x, y, game_master):
+ PicassoAsset.__init__(self, None, x, y)
+
+ self.game_master = game_master
+ self.known_players = self.game_master.players
+ self.update()
+
+ def draw(self):
+ return self.surface
+
+ # TODO CLEANUP, SRSLY!
+ def update(self):
+ current_step = 0
+ stride = DEFAULT_HEIGHT / len(self.known_players)
+ new_view = pygame.Surface((DEFAULT_WIDTH, DEFAULT_HEIGHT),
+ pygame.SRCALPHA, 32)
+ for player in self.known_players:
+ if player == self.game_master.current_player():
+ highlight = pygame.Surface((new_view.get_width(),
+ stride - 5))
+ highlight.fill(LIGHT_BROWN)
+ new_view.blit(highlight, (0, current_step))
+ # account for player elimination
+ if player in self.game_master.players:
+ text_asset = TextAsset(0, 0, player.name, size=16, bold=True)
+ army_count_asset = TextAsset(0, 0, str(len(self.game_master.player_territories(player))), size=16,
+ bold=True)
+ colour_block = pygame.Surface((COLOUR_BLOCK_WIDTH, stride))
+ colour = TerritoryAsset.mapping[player]
+ colour_block.fill(colour)
+ new_view.blit(text_asset.draw(), (DEFAULT_X_OFFSET + COLOUR_BLOCK_WIDTH,
+ current_step))
+ new_view.blit(army_count_asset.draw(), (new_view.get_width() - army_count_asset.get_width() - 5, current_step))
+ new_view.blit(colour_block, (0, current_step - 5))
+ current_step += stride
+
+ self.surface = new_view
4 risk/graphics/assets/territory.py
View
@@ -23,7 +23,7 @@ def build_territory_asset(continent, territory, x, y):
def build_player_colour_mapping(players):
overflow_colour = base.BLACK
colours = [
- base.BROWN,
+ base.BLUE,
base.PURPLE,
base.ORANGE,
base.YELLOW,
@@ -120,7 +120,7 @@ def draw(self):
pygame.draw.circle(self.surface, base.BLACK,
(circle_radius, circle_radius),
circle_radius)
- pygame.draw.circle(self.surface, base.WHITE,
+ pygame.draw.circle(self.surface, base.LIGHT_BROWN,
(circle_radius, circle_radius),
circle_radius - 2)
font_surface = font.render(str(self.count), False,
5 risk/graphics/assets/text.py
View
@@ -9,14 +9,17 @@
class TextAsset(PicassoAsset):
- def __init__(self, x, y, msg, colour=assets.base.BLACK, size=32):
+ def __init__(self, x, y, msg, colour=assets.base.BLACK, size=32,
+ bold=False):
self.colour = colour
self.size = size
+ self.bold = bold
PicassoAsset.__init__(self, None, x, y)
self.render_text(msg)
def render_text(self, msg):
font = Font(_FONT, self.size)
+ font.set_bold(self.bold)
self.surface = font.render(msg, False, self.colour)
class CurrentPlayerAsset(TextAsset):
22 risk/graphics/graphics.py
View
@@ -12,6 +12,7 @@
import risk.graphics.assets.territory
import risk.graphics.assets.dialog
import risk.graphics.assets.image
+import risk.graphics.assets.gameplay
from risk.logger import *
from risk.graphics import assets
@@ -116,6 +117,9 @@ def add_graphic_hooks(game_master):
game_master.add_start_turn_callback(check_gui_quit_event)
game_master.add_end_turn_callback(check_gui_quit_event)
game_master.add_end_action_callback(release_control)
+ game_master.add_start_turn_callback(update_game_info_panel)
+ game_master.add_end_action_callback(update_game_info_panel)
+ #game_master.add_end_action_callback(delay)
def initialize_territories(picasso, game_master):
datastore = Datastore()
@@ -132,6 +136,7 @@ def initialize_territories(picasso, game_master):
build_player_colour_mapping(game_master.players)
def initialize_other_graphic_assets(picasso, game_master):
+ picasso = get_picasso()
datastore = Datastore()
#current_player_asset = assets.text.CurrentPlayerAsset(
# 100, 100, game_master)
@@ -140,11 +145,19 @@ def initialize_other_graphic_assets(picasso, game_master):
feedback_asset = assets.text.TextAsset(100, 650,
'choose territory to attack')
datastore.add_entry('attack_feedback', feedback_asset)
+ game_info_asset = assets.gameplay.PlayersAsset(30, 550, game_master)
+ picasso.add_asset('999_ontop', game_info_asset)
+ datastore.add_entry('game_info', game_info_asset)
def add_buttons(picasso):
datastore = Datastore()
- next_button = assets.clickable.ClickableAsset(
- 1000, 635, 120, 65, 'NEXT')
+ #next_button = assets.clickable.ClickableAsset(
+ # 1000, 635, 120, 65, 'NEXT')
+ next_button = assets.clickable.ImageButtonAsset(
+ 1000, 635,
+ 'assets/art/gui/button_next_up.png',
+ 'assets/art/gui/button_next_down.png'
+ )
datastore.add_entry('next', next_button, 'buttons')
for button in datastore.get_storage('buttons').values():
@@ -172,7 +185,7 @@ def is_human_player(game_master):
return isinstance(game_master.current_player(),
risk.player.HumonRiskPlayer)
-def delay(game_master):
+def delay(game_master, *args):
if not is_human_player(game_master):
time.sleep(1)
@@ -186,6 +199,9 @@ def check_gui_quit_event(game_master):
if event.type == pygame.QUIT:
game_master.end_game()
+def update_game_info_panel(*args):
+ Datastore().get_entry('game_info').update()
+
def release_control(game_master, *args):
# release CPU for faster screen update
time.sleep(0)
Please sign in to comment.
Something went wrong with that request. Please try again.