翻转(或反转)Pygame 中的文本

Flip (or invert) text in Pygame

我刚开始使用 PyGame,我正在尝试弄清楚如何水平和/或垂直翻转文本,但到目前为止,还没有成功。到目前为止,这是我的代码:

import pygame, sys, datetime, os
from pygame.locals import *
os.environ['SDL_VIDEO_CENTERED'] = '1'
now = datetime.datetime.now()
dateoftime = (now.strftime("%d-%m-%Y"))

pygame.init()

screen = pygame.display.set_mode((768, 768), pygame.NOFRAME)
clock = pygame.time.Clock()
White=(255,255,255)
Black=(0,0,0)
basicfont = pygame.font.SysFont('pricedown', 100)
text = basicfont.render((dateoftime), True, White, Black)
textrect = text.get_rect()
textrect.centerx = screen.get_rect().centerx
textrect.centery = screen.get_rect().centery

pygame.transform.flip(screen,True,False)

screen.fill(Black)
screen.blit(text, textrect)
pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
clock.tick(50)

我想做的另一件事是将文本居中但更改它的 y 值。例如,将 x 值保持为中心(从 textrect 计算),但将 y 值更改为 200 或 600。当我想更改 x 值时也是如此。此外,我需要更改哪些部分以显示每秒更新的当前时间 (H:M:S)。我试图更改 "clock.time(50)" 中的数量,但这并没有减少。

大体上如有错误,敬请指正。谢谢。

渲染后只需翻转文本。首先删除 pygame.transform.flip(screen,True,False),然后在渲染后立即添加 text = pygame.transform.flip(text, True, False)

import pygame, sys, datetime, os
from pygame.locals import *

os.environ['SDL_VIDEO_CENTERED'] = '1'
now = datetime.datetime.now()
dateoftime = (now.strftime("%d-%m-%Y"))

pygame.init()

screen = pygame.display.set_mode((768, 768), pygame.NOFRAME)
clock = pygame.time.Clock()
White=(255,255,255)
Black=(0,0,0)
basicfont = pygame.font.SysFont('pricedown', 100)
text = basicfont.render((dateoftime), True, White, Black)
# ADDED!
text = pygame.transform.flip(text, True, False)  # Flip the text vertically.
textrect = text.get_rect()
textrect.centerx = screen.get_rect().centerx
textrect.centery = screen.get_rect().centery

#pygame.transform.flip(screen,True,False)  # Get rid of this.

screen.fill(Black)
screen.blit(text, textrect)
pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
clock.tick(50)