Return to repo list

tzed

Simple story-driven open world 2D CRPG.
Return to HMagellan.com

commit 3254bc13ad418211783b5bac9d4407280c202c57
parent 647c3ac22114da1dc535a993f4d642ba16e97e03
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed, 25 Aug 2021 19:40:20 -0500

added var substution in messages

Diffstat:
Msrc/game.py | 15+++++----------
Msrc/message.py | 35++++++++++++++++++++++++-----------
2 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/src/game.py b/src/game.py @@ -161,8 +161,8 @@ class Game(object): """ # First, prep messages + canmove = False d = { (-1, 0) : "west", (1, 0) : "east", (0, -1) : "north", (0, 1) : "south" } - mess = "You move " + d[offset] if offset in d.keys() else "" # Next, calculate the new tilepos for the player if it exists. if self.player != None: @@ -175,8 +175,7 @@ class Game(object): self.player.tilepos = np self.current_board.position_to_tile(self.player.tilepos) self.current_board.check_events_at_tilepos(self.player.tilepos) - elif mess != "": - mess = "You cannot move there!" + canmove = True # Otherwise, in case we are trying to move out of the current cell, calculate the relative offset of the new cellpos # and the absolute position of the new cellpos using the player's current absolute cellpos. @@ -195,14 +194,10 @@ class Game(object): self.current_board.change_cell(self.player.cellpos) self.current_board.position_to_tile(self.player.tilepos) self.current_board.check_events_at_tilepos(self.player.tilepos) - elif mess != "": - mess = "You cannot move there!" - elif mess != "": - mess = "You cannot move there!" + canmove = True # Finally, post the message - if mess != "": - self.message_board.post(mess, self.gamedata) + self.message_board.post("$PLAYERNAME moves " + d[offset] if offset in d.keys() and canmove else "$PLAYERNAME cannot move there!" if not canmove else "", self.gamedata) def post_message(self, text): """ @@ -217,7 +212,7 @@ class Game(object): state of the game and game objects. """ # TODO: This will see regular expansion - self.gamedata["player_name"] = "Joe" + self.gamedata["$PLAYERNAME"] = "Joe" def shift_frames(self, framerate = FRAMERATE): """ diff --git a/src/message.py b/src/message.py @@ -1,4 +1,4 @@ -import pygame +import pygame, re from .gamelib import * ############## @@ -65,16 +65,29 @@ class MessageBoard(object): Post a message to the board. """ # TODO: Make this better - # First, handle adding and pruning the message list - self.messages.append(text) - if len(self.messages) > self.max_messages: - self.messages.pop(0) - - # Reorganize the displayed messages - self.rendered_messages.empty() - for m in range(0, len(self.messages)): - c = (120, 90, 90) if m < len(self.messages) - 1 else (255, 255, 255) - self.rendered_messages.add(Message(self.font.render(self.messages[m], False, c), (self.board_rect.topleft[0] + 2, self.board_rect.bottom - (MESSAGE_FONT_SIZE * (len(self.messages) - m)) - 2))) + # Only work if text is not an empty string + if text != "": + + # First, parse for data variable names + pr = re.split(r' ', text) + for w in range(0, len(pr)): + if pr[w] in data.keys(): + pr[w] = data[pr[w]] + wtext = "" + for w2 in pr: + wtext = wtext + " " + w2 + wtext = wtext[0:] + + # Next, handle adding and pruning the message list + self.messages.append(wtext) + if len(self.messages) > self.max_messages: + self.messages.pop(0) + + # Reorganize the displayed messages + self.rendered_messages.empty() + for m in range(0, len(self.messages)): + c = (120, 90, 90) if m < len(self.messages) - 1 else (255, 255, 255) + self.rendered_messages.add(Message(self.font.render(self.messages[m], False, c), (self.board_rect.topleft[0] + 2, self.board_rect.bottom - (MESSAGE_FONT_SIZE * (len(self.messages) - m)) - 2))) def update_message_board(self, surface = None): """