Return to repo list

tzed

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

commit 647c3ac22114da1dc535a993f4d642ba16e97e03
parent 2efffc25ea31afd935852e882aa72a698b1c17cd
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed, 25 Aug 2021 19:19:14 -0500

message scrolling and trimming

Diffstat:
Mdata/etc/defaults.json | 2+-
Msrc/game.py | 26+++++++++++++++++++++++---
Msrc/message.py | 6+++++-
3 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/data/etc/defaults.json b/data/etc/defaults.json @@ -5,7 +5,7 @@ "screen_margin_y" : 4, "framerate" : 30, "message_font" : "PixelOperatorMono.ttf", - "message_font_size" : 14, + "message_font_size" : 16, "controls" : { "up" : [119, 1073741906], "down" : [115, 1073741905], diff --git a/src/game.py b/src/game.py @@ -159,8 +159,12 @@ class Game(object): Move the player from tile-to-tile relative to the given offset. """ + + # First, prep messages + d = { (-1, 0) : "west", (1, 0) : "east", (0, -1) : "north", (0, 1) : "south" } + mess = "You move " + d[offset] if offset in d.keys() else "" - # First, calculate the new tilepos for the player if it exists. + # Next, calculate the new tilepos for the player if it exists. if self.player != None: np = (self.player.tilepos[0] + offset[0], self.player.tilepos[1] + offset[1]) @@ -171,6 +175,8 @@ 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!" # 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. @@ -178,8 +184,7 @@ class Game(object): celloff = (-1 if offset[0] < 0 else 0 if offset[0] == 0 else 1, -1 if offset[1] < 0 else 0 if offset[1] == 0 else 1) ncc = (self.player.cellpos[0] + celloff[0], self.player.cellpos[1] + celloff[1]) - # Then, if the adjacent position exists and the absolute cellpos is in the cellmap, calculate the tilepos the player - # would land on in this new cell. + # Check if we would land on in this new cell. if self.current_board.adjacent_cells[celloff] != None and ncc in self.current_board.cellmap.keys(): ntp = (0 if celloff[0] > 0 else self.player.tilepos[0] if celloff[0] == 0 else self.current_board.cell_dimensions[0] - 1, 0 if celloff[1] > 0 else self.player.tilepos[1] if celloff[1] == 0 else self.current_board.cell_dimensions[1] - 1) @@ -190,6 +195,21 @@ 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!" + + # Finally, post the message + if mess != "": + self.message_board.post(mess, self.gamedata) + + def post_message(self, text): + """ + This method is used by external objects to + post messages. + """ + self.message_board.post(text, self.gamedata) def collect_game_data(self): """ diff --git a/src/message.py b/src/message.py @@ -58,18 +58,22 @@ class MessageBoard(object): # Other vals self.messages = [] self.rendered_messages = pygame.sprite.Group() + self.max_messages = (SCREEN_HEIGHT // MESSAGE_FONT_SIZE) - 2 def post(self, text, data): """ 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, 120, 120) if m < len(self.messages) - 1 else (255, 255, 255) + 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):