我有这段代码,它没有显示分配给屏幕宽度中间变量“标题”的文本。 除了根据文本长度猜测位置之外,还有什么方法可以将其居中吗?
import pygame
pygame.init()
pygame.font.init()
WIDTH = 1920
HEIGHT = 1080
CYAN = (0, 255, 255)
RED = (255, 0, 0)
window = pygame.display.set_mode((WIDTH, HEIGHT))
def startWindow():
titleFont = pygame.font.SysFont("Comic Sans MS", 35)
window.fill(RED)
title = titleFont.render("Ball Game", False, CYAN)
window.blit(title, (WIDTH//2, 35))
pygame.display.update()
startWindow()
最佳答案
您应该在渲染后首先获取文本矩形,然后将矩形相对于屏幕宽度居中。之后你可以将它传递到 blit。
centerTitle = title.get_rect(center=(WIDTH/2,35))
window.blit(title, centerTitle)
关于python - 如何使 pygame 显示的文本居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65402786/