comparison pyntnclick/utils.py @ 825:c5171ad0c3cd pyntnclick

Make engine computer start using text for alerts
author Neil Muller <neil@dip.sun.ac.za>
date Tue, 29 Jan 2013 12:45:54 +0200
parents 9f542ef6e498
children
comparison
equal deleted inserted replaced
824:9f542ef6e498 825:c5171ad0c3cd
40 if isinstance(color, basestring): 40 if isinstance(color, basestring):
41 return pygame.Color(color) 41 return pygame.Color(color)
42 return pygame.Color(*color) 42 return pygame.Color(*color)
43 43
44 44
45 def render_text(text, fontname, font_size, color, bg_color, resource, size): 45 def render_text(text, fontname, font_size, color, bg_color, resource, size,
46 centre=True):
46 """Render the text so it will fit in the given size, reducing font 47 """Render the text so it will fit in the given size, reducing font
47 size as needed. 48 size as needed.
48 49
49 Note that this does not do any text wrapping.""" 50 Note that this does not do any text wrapping."""
50 done = False 51 done = False
51 width, height = size 52 width, height = size
53 color = convert_color(color)
54 bg_color = convert_color(bg_color)
52 surface = Surface(size, SRCALPHA).convert_alpha() 55 surface = Surface(size, SRCALPHA).convert_alpha()
53 surface.fill(bg_color) 56 surface.fill(bg_color)
54 while not done and font_size > 0: 57 while not done and font_size > 0:
55 # We bail at font_size 1 and just clip in that case, since we're 58 # We bail at font_size 1 and just clip in that case, since we're
56 # out of good options 59 # out of good options
58 text_surf = font.render(text, True, color) 61 text_surf = font.render(text, True, color)
59 if (text_surf.get_width() > width or text_surf.get_height() > height): 62 if (text_surf.get_width() > width or text_surf.get_height() > height):
60 font_size -= 1 63 font_size -= 1
61 else: 64 else:
62 done = True 65 done = True
63 # Centre the text in the rect 66 if centre:
64 x = max(0, (width - text_surf.get_width()) / 2) 67 # Centre the text in the rect
65 y = max(0, (height - text_surf.get_height()) / 2) 68 x = max(0, (width - text_surf.get_width()) / 2)
69 y = max(0, (height - text_surf.get_height()) / 2)
70 else:
71 x = y = 0
66 surface.blit(text_surf, (x, y)) 72 surface.blit(text_surf, (x, y))
67 return surface 73 return surface