gpt4 book ai didi

python - 在 Pygame 中绘制文本大纲

转载 作者:行者123 更新时间:2023-12-02 19:36:27 25 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_speed('arialnarrow.ttf', 40, 'Hello', (255,255,255), (width/2), (height/2), False)

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

最佳答案

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

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

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

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

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

将文本 block 传输到 textSurf 的中间:

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

Blit textSurf 到窗口上:

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

关于python - 在 Pygame 中绘制文本大纲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60987711/

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