【问题标题】:Writing text in pygame在 pygame 中编写文本
【发布时间】:2014-06-17 02:18:02
【问题描述】:

我知道如何在 pygame 中显示文本,但我真正需要的是能够在 pygame 窗口已经运行时编写文本。

这是我的 pygame 窗口代码:

import pygame

def highscore():
    pygame.font.init()
    background_colour = (255,255,255)
    (width, height) = (600, 600)

    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption('Tutorial 1')
    screen.fill(background_colour)

    pygame.display.flip()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False



highscore()

【问题讨论】:

  • Blitting text in pygame 的可能重复项
  • 同一个问题已经有十几个问题了
  • @BartlomiejLewandowski 您链接的那个并不是真正的副本。也就是说,如果实际上没有几十个重复,我会感到震惊;你能链接一个吗?

标签: python python-2.7 pygame


【解决方案1】:

http://www.daniweb.com/software-development/python/code/244474/pygame-text-python

这是一个示例的链接...我认为您正在寻找的代码是这个...

# pick a font you have and set its size
myfont = pg.font.SysFont("Comic Sans MS", 30)
# apply it to text on a label
label = myfont.render("Python and Pygame are Fun!", 1, yellow)
# put the label object on the screen at point x=100, y=100
screen.blit(label, (100, 100))

【讨论】:

  • 这是否意味着我将能够进行实时输入并将其显示在 pygame 窗口中? @FireArrow5235
  • 这段代码肯定是其中的一部分......但是要做到这一点需要一些聪明才智......我不知道该怎么做
【解决方案2】:

一个缓慢但简单的方法是:

import pygame

def text(surface, fontFace, size, x, y, text, colour):
    font = pygame.font.Font(fontFace, size)
    text = font.render(text, 1, colour)
    surface.blit(text, (x, y))

screen = pygame.display.set_mode(500, 100)
screen.fill((255, 255, 255))
text(screen, 'font.ttf', 32, 5, 5, 'This is text', (0, 0, 0)

但这很慢,因为你每次都必须加载字体,所以我使用这个:

import pygame

fonts = {}

def text(surface, fontFace, size, x, y, text, colour):
    if size in fonts:
        font = fonts[size]
    else:
        font = pygame.font.Font(fontFace, size)
        fonts[size] = font
    text = font.render(text, 1, colour)
    surface.blit(text, (x, y))

screen = pygame.display.set_mode(500, 100)
screen.fill((255, 255, 255))
text(screen, 'font.ttf', 32, 5, 5, 'This is text', (0, 0, 0)

这样,它会跟踪使用的每个字体大小,如果它已经使用过,我会从字典中加载它,否则它会加载字体并将其保存到字典中。唯一的问题是,这仅在您只使用一种字体时才有效,但对于使用的不同字体,您可能只有不同的字典。

循环只是这样做:

screen = pygame.display.set_mode(500, 100)
running = True
while running:
    screen.fill((255, 255, 255))
    text(screen, 'font.ttf', 32, 5, 5, 'This is text', (0, 0, 0)
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = False

另外,请注意,您需要在 CWD 中创建一个名为“font.ttf”的文件才能使本示例正常工作。

【讨论】:

    【解决方案3】:

    在主循环中,你需要声明一个字体,然后在屏幕上显示它

    screen.blit(renderedfont, (position))
    

    那么你只需要更新显示:

    import pygame
        
    def highscore():
        pygame.font.init()
        background_colour = (255,255,255)
        (width, height) = (600, 600)
    
        screen = pygame.display.set_mode((width, height))
        pygame.display.set_caption('Tutorial 1')
        screen.fill(background_colour)
    
        pygame.display.flip()
    
        running = True
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
    
            myfont = pygame.font.SysFont("monospace", 75)
            BLACK = (0, 0, 0)
            label = myfont.render("Tutorial 1", 1, BLACK)
            screen.blit(label, (0, 10))
            pygame.display.update()
    
    highscore()
    

    【讨论】:

      【解决方案4】:

      首先你需要制作一个字体对象,有两种方法

      方式一:

      fontname = 'freesansbold.ttf'
      fontsize = 30
      font = pygame.font.Font(fontname, fontsize)
      

      方式二:

      fontname = 'comicsansbold'
      fontsize = 30
      font = pygame.font.SysFont(fontname, fontsize)
      

      然后你需要从你的字体对象渲染你的文本表面

      text = 'test'
      antialias = True
      colour = 0,0,0
      textSurf = font.render(text, antialias, colour
      

      现在你有了你的文本表面,你可以将它粘贴到屏幕上。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-09
        • 2021-08-25
        • 2016-02-20
        • 1970-01-01
        • 1970-01-01
        • 2013-06-15
        • 1970-01-01
        • 2019-04-24
        相关资源
        最近更新 更多