gpt4 book ai didi

python - 如何在 Pygame 中添加文本语音

转载 作者:行者123 更新时间:2023-12-02 02:56:36 24 4
gpt4 key购买 nike

所以我有一个将文本传输到屏幕的函数:

def text_speech(font : str ,size : int,text : str,color,x,y, bold : bool):
SCREEN = width, height = 900, 600
font = pygame.font.Font(font,size)
font.set_bold(bold)
text = font.render(text, True, color)
textRect = text.get_rect()
textRect.center = (x,y)
screen.blit(text,textRect)

如果我有

screen.fill((0,0,0))
text_speech('arialnarrow.ttf', 30, 'Hello', (255,255,255),
width/2, height/2, False)

它在黑色屏幕上用白色文本打印“Hello”。如果玩家将鼠标悬停在该文本上,如何才能显示如下所示的文本气泡:

this

我只想要一个绿色背景的文本,上面写着“点击确认”。任何人都知道如何做到这一点。抱歉,我对 Pygame 有点陌生。

最佳答案

font.render的第4个参数是文本的可选背景颜色:

color = (255, 255, 255)
background = (0, 255, 0)
text = font.render(text, True, color, background)

另一个选项是将文本渲染到表面并将表面格式转换为每像素 alpha 格式 (convert_alpha):

textSurf = font.render(text, True, color).convert_alpha()
textSize = textSurf.get_size()

创建一个具有双倍尺寸的新表面:

bubbleSurf = pygame.Surface((textSize[0]*2., textSize[1]*2))
bubbleRect = bubbleSurf.get_rect()

用气泡的背景颜色填充表面:

bubbleSurf.fill(background)

将文本传输到气泡表面:

bubbleSurf.blit(textSurf, textSurf.get_rect(center = bubbleRect.center))

将气泡表面传输到屏幕上:

bubbleRect.center = (x,y)
screen.blit(bubbleSurf, bubbleRect)

完整功能text_speech:

def text_speech(font : str ,size : int,text : str,color,background,x,y, bold : bool):
SCREEN = width, height = 900, 600
font = pygame.font.Font(font,size)
font.set_bold(bold)
textSurf = font.render(text, True, color).convert_alpha()
textSize = textSurf.get_size()
bubbleSurf = pygame.Surface((textSize[0]*2., textSize[1]*2))
bubbleRect = bubbleSurf.get_rect()
bubbleSurf.fill(background)
bubbleSurf.blit(textSurf, textSurf.get_rect(center = bubbleRect.center))
bubbleRect.center = (x,y)
screen.blit(bubbleSurf, bubbleRect)
text_speech('arialnarrow.ttf', 30, 'Hello', (255,255,255), (0,128,0), 
width/2, height/2, False)

参见示例:

import pygame
import pygame.font

pygame.init()

width, height = 400, 300
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
textRect = pygame.Rect(0, 0, 0, 0)

def text_speech(font : str, size : int, text : str,color,background,x,y, bold : bool, bubble: bool):
global textRect
font = pygame.font.SysFont(None, 40)
#font = pygame.font.Font(font,size)
font.set_bold(bold)
textSurf = font.render(text, True, color).convert_alpha()
if bubble:
textSize = textSurf.get_size()
bubbleSurf = pygame.Surface((textSize[0]*2., textSize[1]*2))
textRect = bubbleSurf.get_rect()
bubbleSurf.fill(background)
bubbleSurf.blit(textSurf, textSurf.get_rect(center = textRect.center))
textRect.center = (x,y)
screen.blit(bubbleSurf, textRect)
else:
textRect = textSurf.get_rect()
textRect.center = (x,y)
screen.blit(textSurf, textRect)


run = True
while run:
clock.tick(60)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
hover = textRect.collidepoint(pygame.mouse.get_pos())

screen.fill((0,0,0))
text_speech('arialnarrow.ttf', 30, 'Hello', (255,255,255), (0, 128, 0),
(width/2), (height/2), False, hover)
pygame.display.flip()

关于python - 如何在 Pygame 中添加文本语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60997970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com