渲染轮廓字体的好方法是什么?

时间:2021-04-16 17:04:44

I'm writing a game in python with pygame and need to render text onto the screen.

我正在用pygame在python中编写游戏,需要将文本渲染到屏幕上。

I want to render this text in one colour with an outline, so that I don't have to worry about what sort of background the the text is being displayed over.

我想用一个带有轮廓的颜色渲染这个文本,这样我就不必担心文本显示的背景类型了。

pygame.font doesn't seem to offer support for doing this sort of thing directly, and I'm wondering if anyone has any good solutions for achieving this?

pygame.font似乎没有提供直接支持这种事情的支持,我想知道是否有人有任何好的解决方案来实现这一目标?

2 个解决方案

#1


I can give you a quick and bad solution:

print the text 8 times, to surround it, plus one more time for the inner text, like this

我可以给你一个快速和糟糕的解决方案:打印文本8次,包围它,再加上一次内部文本,像这样

UUU
UIU
UUU

U for outer color and I for the inner color.

U代表外部颜色,I代表内部颜色。

#2


A quick and dirty way would be to render your text multiple times with the outline color, shifted by small amounts on a circle around the text position:

一种快速而肮脏的方法是使用轮廓颜色多次渲染文本,在文本位置周围的圆圈上移动少量:

          1
       8  |  2
        \ | /
         \|/
     7----*----3
         /|\
        / | \ 
       6  |  4
          5

Edit: Doh you've been faster ! I wont delete my answer though, this ASCII art is just too good and deserves to live !

编辑:Doh你跑得快!我不会删除我的答案,这个ASCII艺术太好了,值得活下去!

Edit 2: As OregonGhost mentioned, you may need more or fewer steps for the outline rendering, depending on your outline width.

编辑2:正如OregonGhost所提到的,根据轮廓宽度的不同,您可能需要更多或更少的轮廓渲染步骤。

#1


I can give you a quick and bad solution:

print the text 8 times, to surround it, plus one more time for the inner text, like this

我可以给你一个快速和糟糕的解决方案:打印文本8次,包围它,再加上一次内部文本,像这样

UUU
UIU
UUU

U for outer color and I for the inner color.

U代表外部颜色,I代表内部颜色。

#2


A quick and dirty way would be to render your text multiple times with the outline color, shifted by small amounts on a circle around the text position:

一种快速而肮脏的方法是使用轮廓颜色多次渲染文本,在文本位置周围的圆圈上移动少量:

          1
       8  |  2
        \ | /
         \|/
     7----*----3
         /|\
        / | \ 
       6  |  4
          5

Edit: Doh you've been faster ! I wont delete my answer though, this ASCII art is just too good and deserves to live !

编辑:Doh你跑得快!我不会删除我的答案,这个ASCII艺术太好了,值得活下去!

Edit 2: As OregonGhost mentioned, you may need more or fewer steps for the outline rendering, depending on your outline width.

编辑2:正如OregonGhost所提到的,根据轮廓宽度的不同,您可能需要更多或更少的轮廓渲染步骤。