pygameの中にテキスト音声を追加する方法

AK - 10CA - 2172 Glenforest SS:

私はこの機能を持っているので、画面にブリットテキスト:

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)

それは白い文字が黒い画面に「こんにちは」を印刷します。このテキストの上にプレイヤーに置くと、どのように私は、テキストバブルは、このようなことルックスを表示させることができます。

この

私は単に「を確認するためにクリックして」緑の背景を持っていると言うテキストをしたいです。誰もがこれを行う方法のいずれかの手掛かりを得ました。私はpygameのに新しいビットだけど、謝罪します。

Rabbid76:

4番目のパラメータはfont.render、テキストのオプションの背景色です。

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

別のオプションは、表面にテキストをレンダリングし、(ピクセルごとのアルファフォーマットに表面形式を変換することです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)

バブル表面上にテキストをBLIT:

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

バブル表面を上にBLIT screen

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

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=402661&siteId=1