Jan-06-2023, 07:15 PM
(This post was last modified: Jan-06-2023, 07:25 PM by deanhystad.)
I modified my screen_update to better handle multi-line text. Now it centers horizontally and vertically about a point. To get the starting y coordinate it splits the message into lines, converts the lines into text surfaces, and sums the height of those surfaces. Next it adds a line space between consecutive lines to get the height of the overall block of text. This information is used to compute a starting y position.
def refresh_screen(msg=None, font=text_font, color=text_color): """Draw the cube and an optional message""" line_space = 2 surface.fill(background) for square in cube.squares: square.draw(surface) if msg: lines = msg.split('\n') lines = [font.render(line, True, color) for line in lines] x = center.x y = center.y - ( sum(line.get_height() for line in lines) + (len(lines) - 1) * line_space) / 2 for line in lines: surface.blit(line, (x - line.get_width() / 2, y)) y = y + line.get_height() + line_space pygame.display.flip()It would be easy to modify this function to just draw some text centered over some x, y position.