Overview
TheMessageBoard class creates a rectangular widget for displaying text messages on the screen. It uses a Box widget with text drawn inside. The text is a list of lines that can be retrieved and changed with the .text attribute.
Constructor
Parameters
The pygame surface to which the message board will draw itself.
The rectangle defining the location and size of the message board on the surface.
The initial text of the message board as a list of strings (one per line).
Padding in pixels around the text inside the message board.
The font as a (name, size) tuple.
The color of the text.
The background color of the message board.
Width of the border. Same meaning as in the Box widget.
Color of the border. Same meaning as in the Box widget.
Methods
draw()
Draws the message board to the surface, including the surrounding box and all text lines.- Draws the surrounding box
- Renders all lines of text one below the other
- Tests if text fits within the MessageBoard plus padding
- Raises
LayoutErrorif any line cannot fit
LayoutError if a line is too large to fit in the widget with the specified padding.
Attributes
text- The list of text lines to display (can be modified dynamically)box- The underlyingBoxwidget used for drawing
Usage Example
Fromgame.py:
Notes
- Text is rendered using
pygame.font.SysFont - Each string in the
textlist is rendered on a separate line - Lines are stacked vertically from top to bottom
- The widget validates that text fits within the available space including padding
- You can dynamically update text by modifying the
textattribute