Return to repo list

tzed

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

commit 2efffc25ea31afd935852e882aa72a698b1c17cd
parent 994194902777cd66906a3d05ad4751b3a40035cf
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed, 25 Aug 2021 18:58:13 -0500

message displays correctly now

Diffstat:
Msrc/game.py | 5++++-
Msrc/message.py | 43++++++++++++++++++++++++++++++++++++++-----
2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/src/game.py b/src/game.py @@ -39,7 +39,7 @@ class Game(object): # Game components self.interface = interface.Interface(self) - self.message_board = message.MessageBoard(self, (20, 20), (99, 99)) + self.message_board = message.MessageBoard(self, ((SCREEN_WIDTH // 3) - 2, SCREEN_HEIGHT - 16), (self.viewport_rect.right + 16, 4)) # Pygame sprite objects self.player = None @@ -61,6 +61,8 @@ class Game(object): # TMP! self.switch_board("testloc1", (0, 0)) + self.message_board.post("hi there", {}) + self.message_board.post("bye there", {}) # Switch to game control self.switch_mode(STATE_MODES.Overworld_Mode) @@ -222,6 +224,7 @@ class Game(object): if self.player != None: self.player.update(self.screen, self.viewport_rect) self.ui_group.update(self.screen) + self.message_board.update_message_board(self.screen) # Next, get info about the game self.collect_game_data() diff --git a/src/message.py b/src/message.py @@ -8,8 +8,35 @@ from .gamelib import * # This file contains: # 1. The 'MessageBoard' class, which is an object that parses text and creates surfaces with that text drawn on it. +############################# +# Section 1 - Message class # +############################# + +class Message(pygame.sprite.Sprite): + """ + Simple sprite for displayed messages. + """ + + def __init__(self, image, pos): + + # Parent initialization + pygame.sprite.Sprite.__init__(self) + + # Saved vals + self.image = image + self.rect = self.image.get_rect() + self.rect.topleft = pos + + def update(self, surface = None): + """ + Draw the Message sprite to the given + surface. + """ + if surface != None: + surface.blit(self.image, self.rect) + ################################## -# Section 2 - MessageBoard class # +# Section 1 - MessageBoard class # ################################## class MessageBoard(object): @@ -30,18 +57,24 @@ class MessageBoard(object): # Other vals self.messages = [] + self.rendered_messages = pygame.sprite.Group() def post(self, text, data): """ Post a message to the board. """ # TODO: Make this better - self.messages.add + self.messages.append(text) + + # 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) + 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_board(self, surface = None): + def update_message_board(self, surface = None): """ Draw messages to the screen in post order. """ - if surface != None: - pass + self.rendered_messages.update(surface)