gpt4 book ai didi

python - 我如何只在 x 轴上居中文本,但能够在 pygame 中移动 y 轴?

转载 作者:行者123 更新时间:2023-12-04 01:16:56 24 4
gpt4 key购买 nike

我试图将文本放在屏幕的中央,但也在顶部以在中心标记文本。为了使当前文本居中,我使用下面的代码...

info = pygame.display.Info()
screen = pygame.display.set_mode((info.current_w, info.current_h), pygame.FULLSCREEN)
screen_rect = screen.get_rect()

txt = font.render("some random message", True, color)

screen.blit(txt, txt.get_rect(center=screen_rect.center))

但我找不到一种方法来修改中心,使用 get_rect() 只编辑 y 轴。我在文档中也找不到关于 get_rect() 函数的更多详细信息。

最佳答案

pygame.Rect对象具有以下属性:

x, y 
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h

因此您可以将 centerx 设置为屏幕的 centerx 以使矩形居中而不影响 y 值。

screen.blit(text, text.get_rect(centerx=screen_rect.centerx))

为了完成,如果你想让它在顶部居中,你可以这样写

screen.blit(text, text.get_rect(midtop=screen_rect.midtop)

在底部居中

screen.blit(text, text.get_rect(midbottom=screen_rect.midbottom)

关于python - 我如何只在 x 轴上居中文本,但能够在 pygame 中移动 y 轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63201658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com