def __pygame(title, message): try: import pygame, pygame.font pygame.quit() #clean out anything running pygame.display.init() pygame.font.init() screen = pygame.display.set_mode((460, 140)) pygame.display.set_caption(title) font = pygame.font.Font(None, 18) foreg = 0, 0, 0 backg = 200, 200, 200 liteg = 255, 255, 255 ok = font.render('Ok', 1, foreg) screen.fill(backg) okbox = ok.get_rect().inflate(20, 10) okbox.centerx = screen.get_rect().centerx okbox.bottom = screen.get_rect().bottom - 10 screen.fill(liteg, okbox) screen.blit(ok, okbox.inflate(-20, -10)) pos = [20, 20] for text in message.split('\n'): msg = font.render(text, 1, foreg) screen.blit(msg, pos) pos[1] += font.get_height() pygame.display.flip() while 1: e = pygame.event.wait() if (e.type == pygame.QUIT or e.type == pygame.MOUSEBUTTONDOWN or pygame.KEYDOWN and e.key in (pygame.K_ESCAPE, pygame.K_SPACE, pygame.K_RETURN)): break pygame.quit() except pygame.error: raise ImportError
def linesize(font): # Bug in pygame or SDL_ttf? size = font.get_linesize() if size == 0: size = font.get_height() return size
def prepare_paragraph(text, width, size="normal", colour=(0,0,0)): font = FONTS[size] lines = [] words = text.split() if words: lastline = None line = words[0] for i in range(1,len(words)): lastline = line line = line+" "+words[i] w,h = font.size(line) if w > width: lines.append(lastline) line = words[i] else: line = "" lines.append(line) parawidth = max(font.size(each)[0] for each in lines) lineheight = font.get_height() paraheight = lineheight*len(lines) paragraph = Surface((parawidth,paraheight),pygame.SRCALPHA) paragraph.fill((255,255,255,0)) for y,line in enumerate(lines): text = prepare(line,size,colour) paragraph.blit(text,(0,y*lineheight)) return paragraph
def render(text, font, gfcolor=pygame.Color('yellow'), ocolor=(0, 0, 0), opx=5): textsurface = font.render(text, True, gfcolor).convert_alpha() w = textsurface.get_width() + 2 * opx h = font.get_height() osurf = pygame.Surface((w, h + 2 * opx)).convert_alpha() osurf.fill((0, 0, 0, 0)) surf = osurf.copy() osurf.blit(font.render(text, True, ocolor).convert_alpha(), (0, 0)) for dx, dy in _circlepoints(opx): surf.blit(osurf, (dx + opx, dy + opx)) surf.blit(textsurface, (opx, opx)) return surf