我有这段代码,它没有在屏幕宽度的中间显示分配给变量“ title”的文本。除了根据文本的长度猜测位置之外,还有什么方法可以居中?
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句