在pygam中如何将段落呈现到曲面上

2024-09-27 00:14:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个功能齐全的主菜单,一个按钮带你去游戏,另一个按钮带你去指令页。在

然而,在我的说明页上,我只能写一句话。如果我添加多行,它显示在中间的方块。在

我想实现的是在屏幕上写一个多行的段落来显示一些指令、游戏信息等

下面是我当前游戏GUI的屏幕截图: Game GUI

这是我目前掌握的相关代码:

def text_objects(text, font):
   textSurface = font.render(text, True, (0,0,0))
   return textSurface, textSurface.get_rect()


def ParagraphText(text, font):
   ParagraphSize = (600,500)
   FontSize = font.get_height()

   ParagraphSurf = pygame.Surface(ParagraphSize)

   ParagraphSurf.fill(WHITE)
   ParagraphSurf.set_colorkey(WHITE)

   SplitLines = text.splitlines()

   CentreText = (ParagraphSize[1] - len(SplitLines)*(FontSize + 1)//2)

   for idx, line in enumerate(SplitLines):
       currentTextline = font.render(text, False, (0, 0, 0))
       currentPostion = (0, idx * FontSize + CentreText)
       ParagraphSurf.blit(currentTextline, currentPostion)

   return ParagraphSurf, ParagraphSize



def Instructions():
   paragraph = """Your are the last surviving rhino. Your horn
   is worth millions! Right now you are trapped in a zoo and
   waiting to be slaughtered for your horn. But you can't give
   up! Escape from the tribesmen and zoo security so you can
   enjoy your life as a free being once again"""

   screen.blit(pygame.image.load("background0.jpg").convert(), (0,0))
   InstructionsFont = pygame.font.SysFont("elephant",15)
   TextSurf, TextRect = text_objects("Instructions", InstructionsFont)
   TextRect.center = ((screen_width/2),(screen_height/6))
   screen.blit(TextSurf, TextRect)

   ParagraphText(paragraph,InstructionsFont)

   intro = True

   while intro:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               pygame.quit()
               quit()

       Buttons("BACK",100,500,120,50,TURQUOISE,DARK_TURQUOISE,"back")

       pygame.display.update()
       clock.tick(15)

Tags: textin游戏forgetdefscreenpygame
1条回答
网友
1楼 · 发布于 2024-09-27 00:14:14

^{}对象的^{}方法只能blit单行文本,如documentation所述。在

要解决此问题,您必须调整text_objects()函数,将传入的文本字符串的每一行分别绘制到适当的位置:

  1. 创建一个新的表面,整个段落将被绘制到上面。要伪造透明表面,请使用Pygame的^{}方法。1)
  2. 使用Python内置的^{}方法将文本分成几行字符。在
  3. 如果要在段落表面内垂直对齐(例如居中)文本,请计算一个可选的偏移值。在
  4. 在分割线上循环(即枚举):
    • 将当前字符行渲染到虚拟曲面上。1)
    • 计算此行的位置以适合段落样式。在
    • b将虚拟表面点到主要段落表面上。在
  5. 将段落表面绘制到主屏幕上或与其他参数一起返回。在

现在是时候实施这一点了:

def text_objects(text, font):
    paragraphSize = (xsize, ysize)
    fontSize = font.get_height()

    # Step 1
    paragraphSurface = pygame.Surface(paragraphSize ) 

    #Set colorkey to fake transparent paragraph surface
    paragraphSurface.fill((255, 255, 255))
    paragraphSurface.set_colorkey((255, 255, 255))

    # Step 2
    splitLines = text.splitlines() 

    # Step 3: center the text vertically 
    offSet = (paragraphSize[1] - len(splitLines) * (fontSize + 1)) // 2 

    #Step 4
    for idx, line in enumerate(splitLines):
        currentTextline = font.render(line, False, (0, 0, 0))
        currentPostion = (0, idx * fontSize + offSet)
        paragraphSurface.blit(currentTextline, currentPostion)

    #Step 5
    return paragraphSurface, paragraphSize

如果要使文本中的每一行居中,请不要将currentPostionx-坐标设置为0,而是使用以下计算:

^{pr2}$

sample: center text

或右对齐:

#right align paragraph
currentPostion = (paragraphSize[0] - currentTextline.get_width(), #x-coordinate
                  idx * fontSize + offSet) #y-coordinate

sample: right align

1)请注意,在呈现一行时,将antialias参数设置为true,可能会因为段落表面的设置颜色键而产生不良影响!

我希望这对你有帮助:)

相关问题 更多 >

    热门问题