You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用pygame.font.Font()在Pygame中实现文本换行?

Solution for Text Wrapping in Your Pygame "Would You Rather" Game

Got it, let's tackle this text wrapping issue for your W.Y.R. game without bringing in any extra modules or complicated methods you don't want to use. Here's how to extend your existing Pygame code to handle unlimited-length questions with proper line breaks:

Step 1: Create a Text Wrapping Helper Function

First, we'll make a simple function that splits your long text into lines that fit within your button's width. This works by building lines word-by-word and checking if adding the next word would exceed your max allowed width:

def wrap_text(font, text, max_width):
    # Split text into individual words
    words = text.split(' ')
    lines = []
    current_line = ''
    
    for word in words:
        # Test if adding the next word would exceed the max width
        test_line = current_line + word + ' '
        line_width, _ = font.size(test_line)
        
        if line_width <= max_width or not current_line:
            current_line = test_line
        else:
            # Add the current line to our list and start a new one
            lines.append(current_line.strip())
            current_line = word + ' '
    
    # Add the last remaining line
    if current_line:
        lines.append(current_line.strip())
    
    return lines

Step 2: Integrate with Your Existing Code

Now, use this function with your existing font setup. You'll need to define the max width (use your button's width minus some padding so text doesn't touch the edges) and then render each line individually. Here's how to adjust your code:

# Your existing font setup
font_location = "your_font_path.ttf"  # Replace with your actual font path
button_text_font = pygame.font.Font(font_location, 20)
bg_color = (255, 255, 255)  # Replace with your text color
button_padding = 10  # Adjust this to control spacing from button edges

# Example long W.Y.R. questions
red_text = "Would you rather have the ability to talk to animals but they all hate you, or be able to speak every human language but no one understands you?"
blue_text = "Would you rather never be able to use a phone again, or never be able to use a computer again?"

# Define max width for text (use your button's width minus padding on both sides)
# Replace with your actual button width, e.g., if red_button_rect.width is 300:
red_button_max_width = 300 - (button_padding * 2)
blue_button_max_width = 300 - (button_padding * 2)

# Wrap the text into lines
red_wrapped_lines = wrap_text(button_text_font, red_text, red_button_max_width)
blue_wrapped_lines = wrap_text(button_text_font, blue_text, blue_button_max_width)

# Render each line
red_rendered_lines = [button_text_font.render(line, True, bg_color) for line in red_wrapped_lines]
blue_rendered_lines = [button_text_font.render(line, True, bg_color) for line in blue_wrapped_lines]

Step 3: Draw the Wrapped Text on Buttons

Finally, draw each line onto your buttons, adjusting the y-position for each line to create vertical spacing. Let's say your red button starts at (x, y):

# Example button position (replace with your actual button rect)
red_button_x = 50
red_button_y = 100
line_spacing = 5  # Space between lines

# Draw each line of red button text
current_y = red_button_y + button_padding
for line_surface in red_rendered_lines:
    # Center the line horizontally in the button
    line_x = red_button_x + (300 - line_surface.get_width()) // 2
    screen.blit(line_surface, (line_x, current_y))
    current_y += line_surface.get_height() + line_spacing

# Repeat the same logic for the blue button
blue_button_x = 400
blue_button_y = 100
current_y = blue_button_y + button_padding
for line_surface in blue_rendered_lines:
    line_x = blue_button_x + (300 - line_surface.get_width()) // 2
    screen.blit(line_surface, (line_x, current_y))
    current_y += line_surface.get_height() + line_spacing

Key Notes

  • Adjust button_padding and line_spacing to match your game's visual style.
  • Replace the button width values (300 in the example) with your actual button's pixel width.
  • This approach works entirely with your existing pygame.font.Font and render calls—no extra modules required.

内容的提问来源于stack exchange,提问作者K. Turkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:30:08