在 pygame 中有没有办法将数字放在屏幕中央?

Is there a way to place number on the center of the screen in pygame?

我正在使用 pygame 来显示分数 我有 0 到 9 的图像,需要将它们集中放置,但我不能只将它放在中间,因为分数的大小可以更改,到目前为止我找到的唯一解决方案是将其硬编码为最高 9,然后最高 99 等,如下面的代码

if len(str(self.value)) == 1:
    win.blit(self.imgs[self.value],
             ((x - self.imgs[self.value].get_width(), y)))

elif len(str(self.value)) == 2:
    win.blit(self.imgs[int(str(self.value)[0])],
             (x - self.imgs[int(str(self.value)[0])].get_width(), y))
    win.blit(self.imgs[int(str(self.value)[1])],
             (x, y))

有没有一种通用的方法可以集中循环显示乐谱的所有数字?

获取一张pygame.Rect object with the size of the image (pygame.Surface) by get_rect(). Set the center of the rectangle by an keyword attribute (center = (x, y)) and use the rectangle to blit图片:

centered_rect = self.imgs[self.value].get_rect(center = (x, y))
win.blit(self.imgs[self.value], centered_rect)