Pygame将两个曲面连接在一起

2024-03-29 08:27:12 发布

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

我正在尝试将文本表面添加到更大的文本表面。但我不知道怎么做。在我的例子中,文本表面是紧挨着的,但我想让它们成为一个。下面是一个示例,说明我正在尝试执行的操作,但我不知道正确的格式/命令。在

font = pygame.font.SysFont(font, size)

text_surf1 = font.render(string1, True, black)
text_surf2 = font.render(string2, True, black)

text_surf1 += text_surf2


gameDisplay.blit(text_surf3, (x,y))

Tags: text文本命令true示例格式render表面
1条回答
网友
1楼 · 发布于 2024-03-29 08:27:12

没有组合两个曲面的函数,但是可以创建另一个^{},将前两个曲面的宽度之和传递给第三个曲面。在

txt1 = font.render(string1, True, black)
txt2 = font.render(string2, True, black)

# Create a surface and pass the sum of the widths.
# Also, pass pg.SRCALPHA to make the surface transparent.
txt3 = pg.Surface((txt1.get_width() + txt2.get_width(), txt1.get_height()), pg.SRCALPHA)

# Blit the first two surfaces onto the third.
txt3.blit(txt1, (0, 0))
txt3.blit(txt2, (txt1.get_width(), 0))

也可以将相邻的两个曲面blit到gameDisplay上,除非您想对组合的曲面执行其他操作。在

相关问题 更多 >