如何在 Pygame 中添加文本语音

AK *_*172 2 python pygame

所以我有这个功能可以将文本闪烁到屏幕上:

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)
Run Code Online (Sandbox Code Playgroud)

如果我有

screen.fill((0,0,0))
text_speech('arialnarrow.ttf', 30, 'Hello', (255,255,255),  
            width/2, height/2, False)
Run Code Online (Sandbox Code Playgroud)

它在带有白色文本的黑色屏幕上打印“你好”。如果玩家将鼠标悬停在此文本上,我如何使文本气泡看起来像这样:

这个

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

Rab*_*d76 5

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

color = (255, 255, 255)
background = (0, 255, 0)
text = font.render(text, True, color, background)
Run Code Online (Sandbox Code Playgroud)

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

textSurf = font.render(text, True, color).convert_alpha()
textSize = textSurf.get_size()
Run Code Online (Sandbox Code Playgroud)

创建一个双倍大小的新曲面:

bubbleSurf = pygame.Surface((textSize[0]*2., textSize[1]*2))
bubbleRect = bubbleSurf.get_rect()
Run Code Online (Sandbox Code Playgroud)

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

bubbleSurf.fill(background)
Run Code Online (Sandbox Code Playgroud)

将文本 Blit 到气泡表面:

bubbleSurf.blit(textSurf, textSurf.get_rect(center = bubbleRect.center))
Run Code Online (Sandbox Code Playgroud)

将气泡表面 Blit 到screen

bubbleRect.center = (x,y)
screen.blit(bubbleSurf, bubbleRect)
Run Code Online (Sandbox Code Playgroud)

全功能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)
Run Code Online (Sandbox Code Playgroud)
text_speech('arialnarrow.ttf', 30, 'Hello', (255,255,255), (0,128,0), 
            width/2, height/2, False)
Run Code Online (Sandbox Code Playgroud)

看例子:

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()
Run Code Online (Sandbox Code Playgroud)