7

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?

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
  • I've accepted an answer, but I'm still interested in any other solutions that migth exist. – SpoonMeiser Jul 14 '09 at 11:32
  • Related to https://stackoverflow.com/questions/8049764/how-can-i-draw-text-with-different-stroke-and-fill-colors-on-images-with-python – jdhao Dec 13 '18 at 02:12

2 Answers2

4

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
  • 9,304
  • 7
  • 42
  • 56
3

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 Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
  • 1
    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 Jul 10 '09 at 13:41
  • 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. – SingleNegationElimination Jul 10 '09 at 17:45
  • 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 Dandoulakis Jul 10 '09 at 18:12
  • @TokenMacGuy using the pygame.font interface, I think I need to render the font twice; once in each colour. – SpoonMeiser Jul 11 '09 at 00:37