views:

152

answers:

2

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

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?

+2  A: 

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

UUU
UIU
UUU

U for outer color and I for the inner color.

Nick D
Actually, four times (the corners) is good enough if the outline is not much more than one pixel wide. Two pixels should be fine as well. It depends on the speed of the font renderer how bad the solution really is. I used it in XNA and it's quite fast there, at least if the text is batch-rendered.
OregonGhost
good tip !
Nick D
you only need to render the font once. You can reuse the rendered surface multiple times for each time it is blitted to the target surface.
TokenMacGuy
TokenMacGuy, that is exactly what I do (did) in my demo games. I had pre-rendered fonts on a separate surface. Good tip 2! Of course, if you don't display much text on the screen, you won't see any difference on the frame rate.
Nick D
@TokenMacGuy using the pygame.font interface, I think I need to render the font twice; once in each colour.
SpoonMeiser
+3  A: 

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 !

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

Luper Rouch
See my comment at Nick D.'s answer, which applies to this one as well.
OregonGhost
+1, your ASCII art is beautiful :-)
Nick D