-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpygame_helpers.py
47 lines (35 loc) · 1.29 KB
/
pygame_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from pygame import Rect
# taken from http://pygame.org/wiki/TextWrap and modified, slightly
# draw some text into an area of a surface
# automatically wraps words
# returns any text that didn't get blitted
def draw_text(surface, text, color, rect, font, aa=False, bkg=None, wrap=False):
rect = Rect(rect)
y = rect.top
lineSpacing = -2
# get the height of the font
fontHeight = font.size("Tg")[1]
if wrap is False:
rect = Rect(rect.left,rect.top,rect.width,fontHeight)
while text:
i = 1
# determine if the row of text will be outside our area
if y + fontHeight > rect.bottom:
break
# determine maximum width of line
while font.size(text[:i])[0] < rect.width and i < len(text):
i += 1
# if we've wrapped the text, then adjust the wrap to the last word
if i < len(text):
i = text.rfind(" ", 0, i) + 1
# render the line and blit it to the surface
if bkg:
image = font.render(text[:i], 1, color, bkg)
image.set_colorkey(bkg)
else:
image = font.render(text[:i], aa, color)
surface.blit(image, (rect.left, y))
y += fontHeight + lineSpacing
# remove the text we just blitted
text = text[i:]
return y