Overview
TheBox class creates a rectangular widget with a background color and optional border. It provides a concept of an “internal rect” which represents the area inside the border.
Constructor
Parameters
The pygame surface to which the box will draw itself when its
draw() method is called.The outer rectangle defining the location and size of the box on the surface.
The background color of the box.
Width of the border. If 0, no border is drawn. If > 0, the border is drawn inside the bounding rect of the widget (so take this into account when computing internal space of the box).
Color of the border.
Methods
draw()
Draws the box to the surface, including the border (if specified) and background.get_internal_rect()
Returns the internal rect of the box - the area inside the border.pygame.Rect that excludes the border area.
Usage Example
Fromgame.py - creating a message board that uses Box internally:
Notes
- All rectangles are
pygame.Rectinstances - All colors are
pygame.Colorinstances - The border is drawn inside the outer rect, reducing the available internal space
- The internal rect is calculated automatically during initialization