【问题标题】:Flip (or invert) text in Pygame在 Pygame 中翻转(或反转)文本
【发布时间】:2018-04-12 15:52:43
【问题描述】:

我刚刚开始使用 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)”中的数量,但这并没有减少。

如果总体上有任何错误,我们将不胜感激。谢谢。

【问题讨论】:

    标签: python text pygame flip


    【解决方案1】:

    渲染后只需翻转文本。首先删除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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 2013-02-18
      • 1970-01-01
      • 2023-04-08
      • 2018-01-17
      • 2021-10-09
      • 2012-09-12
      相关资源
      最近更新 更多