在pygame中写文本

0 投票
4 回答
12136 浏览
提问于 2025-04-18 04:54

我知道怎么在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()

4 个回答

0

首先,你需要创建一个字体对象,这里有两种方法。

方法一:

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

现在你有了文本表面,可以把它显示到屏幕上,做一些其他操作。

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

另外,顺便提一下,这个例子需要在当前工作目录下有一个叫做 '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()
2

这里有一个链接,里面有个例子...我觉得你想要的代码大概是这个...

# 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))

撰写回答