在 Pygame 中有一个文本大纲 [英] Have an outline of text in Pygame

查看:34
本文介绍了在 Pygame 中有一个文本大纲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个功能,它将文本放在屏幕上:

So I have this function in which it puts text on the screen:

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_speed('arialnarrow.ttf', 40, 'Hello', (255,255,255), (width/2), (height/2), False)

它在带有白色文本的黑色屏幕上生成世界Hello".是否有可能如果用户将鼠标悬停在它上面,它会创建一个红色的 (255,0,0) 轮廓?

It generates the world 'Hello' on a black screen with white text. Is it possible that if the user hovers their mouse over this, it creates a red (255,0,0) outline?

推荐答案

要完成大纲,您必须多次 blit.以轮廓颜色(红色)渲染文本:

To accomplish an outline you have to blit the multiple times. Render the text in the outline color (red):

outlineSurf = font.render(text, True, (255, 0, 0))
outlineSize = outlineSurf.get_size()

创建一个大于文本表面的表面.宽度和高度必须增加两倍的轮廓厚度:

Create a surface which is grater than the text surface. The width and the height have to be increased by the doubled outline thickness:

textSurf = pygame.Surface((outlineSize[0] + outline*2, outlineSize[1] + 2*outline))
textRect = textSurf.get_rect()

在文本表面上 Blit 轮廓表面 8 次,移动轮廓粗细(水平、垂直和对角线:

Blit the outline surface 8 times on the text surface, shifted by the outline thickness (horizontal, vertical and diagonal:

offsets = [(ox, oy) 
    for ox in range(-outline, 2*outline, outline)
    for oy in range(-outline, 2*outline, outline)
    if ox != 0 or ox != 0]
for ox, oy in offsets:   
    px, py = textRect.center
    textSurf.blit(outlineSurf, outlineSurf.get_rect(center = (px+ox, py+oy))) 

使用文本颜色渲染文本并将表面转换为每像素 alpha 格式 (convert_alpha):

Render the text with the text color and convert the surface to a per pixel alpha format (convert_alpha):

innerText = font.render(text, True, color).convert_alpha()

textSurf中间的文字Blit:

Blit the text in the middle of textSurf:

textSurf.blit(innerText, innerText.get_rect(center = textRect.center)) 

Blit textSurf 到窗口上:

Blit textSurf onto the window:

textRect.center = (x,y)
screen.blit(textSurf, textRect)

看例子:

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, x, y, bold : bool, outline: int):
    global textRect 
    # font = pygame.font.Font(font,size)
    font = pygame.font.SysFont(None, size)
    font.set_bold(True)
    if outline > 0:
        outlineSurf = font.render(text, True, (255, 0, 0))
        outlineSize = outlineSurf.get_size()
        textSurf = pygame.Surface((outlineSize[0] + outline*2, outlineSize[1] + 2*outline))
        textRect = textSurf.get_rect()
        offsets = [(ox, oy) 
            for ox in range(-outline, 2*outline, outline)
            for oy in range(-outline, 2*outline, outline)
            if ox != 0 or ox != 0]
        for ox, oy in offsets:   
            px, py = textRect.center
            textSurf.blit(outlineSurf, outlineSurf.get_rect(center = (px+ox, py+oy))) 
        innerText = font.render(text, True, color).convert_alpha()
        textSurf.blit(innerText, innerText.get_rect(center = textRect.center)) 
    else:
        textSurf = font.render(text, True, color)
        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())
    outlineSize = 3 if hover else 0 

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

这篇关于在 Pygame 中有一个文本大纲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆