def render_text(self, con, text: str, position: Tuple[int, int], fg_col, break_on_comma=False): font = self.fonts.get("mini") if break_on_comma and "," in text: text_lines = text.split(",") _longest = max(text_lines, key=len) _width = font.size(_longest)[0] text_surface = pygame.Surface( (_width, (font.get_linesize() * len(text_lines)))).convert_alpha() text_surface.fill(CONFIG.get_colour("empty")) y_offset = 0 for line in text_lines: print_pos = (0, y_offset) self.render_text(text_surface, line.strip(), (0, y_offset), fg_col, font) y_offset += font.get_linesize() else: text_surface = font.render(text, False, CONFIG.get_colour(fg_col)) con.blit(text_surface, position)
def _token_builder(interpreted_txt): # build a token from interpreted text # accepts an interpreted text list # returns a token object iswhitespace = _iswhitespace Token = _Token links = defaultdict(list) token_iswhitespace = True surfs, x, y = [], 0, 0 for (envs, chars) in interpreted_txt: bkg, color, font = envs["bkg"], envs["color"], envs["font"] strbuff, surfbuff = [], [] for char in chars: if not iswhitespace(char): token_iswhitespace = False if char == "\n": char = Surface((0, font.get_linesize())) if isinstance(char, unicode): if iswhitespace(char): char = " " strbuff.append(char) elif isinstance(char, str): if iswhitespace(char): char = " " strbuff.append(char) else: if strbuff: surfbuff.append(font.render("".join(strbuff), 1, color, bkg)) surfbuff.append(char) strbuff = [] if strbuff: surfbuff.append(font.render("".join(strbuff), 1, color, bkg)) if surfbuff: # calculate link rects link = envs["link"] surfbuff_w = sum(surf.get_width() for surf in surfbuff) surfbuff_h = max(surf.get_height() for surf in surfbuff) links[link].append(Rect(x, 0, surfbuff_w, surfbuff_h)) x += surfbuff_w # extend surfbuff to surfs and reset surfbuff surfs.extend(surfbuff) surfbuff = [] # get token width and height width = sum(surf.get_width() for surf in surfs) height = max(surf.get_height() for surf in surfs) # given token height, modify link rect y for rect in [rect for v in links.values() for rect in v]: rect.y += height - rect.h token_str = "".join(unicode(char) for (envs, chars) in interpreted_txt for char in chars) return Token((width, height), links, surfs, token_iswhitespace, token_str)
def renderText(text, font, antialias, color, size, autosize, wordwrap): lines = text.split('\n') if wordwrap and not autosize: for i in xrange(len(lines)): line = lines[i] del lines[i] lines[i:i] = wrapText(line, font, size[0]).split('\n') if len(lines) == 1: return font.render(text, antialias, color) else: lineHeight = font.get_linesize() height = lineHeight * len(lines) width = 0 lineSurfs = [] for line in lines: linesurf = font.render(line, antialias, color) lineSurfs.append(linesurf) if linesurf.get_width() > width: width = linesurf.get_width() surf = pygame.Surface((width, height), pygame.SRCALPHA) for i in xrange(len(lineSurfs)): surf.blit(lineSurfs[i], (0, i * lineHeight)) return surf
def linesize(font): # Bug in pygame or SDL_ttf? size = font.get_linesize() if size == 0: size = font.get_height() return size
def _token_builder(interpreted_txt): # build a token from interpreted text # accepts an interpreted text list # returns a token object iswhitespace = _iswhitespace Token = _Token links = defaultdict(list) token_iswhitespace = True surfs, x, y = [], 0, 0 for (envs, chars) in interpreted_txt: bkg, color, font = envs['bkg'], envs['color'], envs['font'] strbuff, surfbuff = [], [] for char in chars: if not iswhitespace(char): token_iswhitespace = False if char == '\n': char = Surface((0, font.get_linesize())) if isinstance(char, unicode): if iswhitespace(char): char = ' ' strbuff.append(char) elif isinstance(char, str): if iswhitespace(char): char = ' ' strbuff.append(char) else: if strbuff: surfbuff.append( font.render(''.join(strbuff), 1, color, bkg)) surfbuff.append(char) strbuff = [] if strbuff: surfbuff.append(font.render(''.join(strbuff), 1, color, bkg)) if surfbuff: # calculate link rects link = envs['link'] surfbuff_w = sum(surf.get_width() for surf in surfbuff) surfbuff_h = max(surf.get_height() for surf in surfbuff) links[link].append(Rect(x, 0, surfbuff_w, surfbuff_h)) x += surfbuff_w # extend surfbuff to surfs and reset surfbuff surfs.extend(surfbuff) surfbuff = [] # get token width and height width = sum(surf.get_width() for surf in surfs) height = max(surf.get_height() for surf in surfs) # given token height, modify link rect y for rect in [rect for v in links.values() for rect in v]: rect.y += (height - rect.h) token_str = ''.join( unicode(char) for (envs, chars) in interpreted_txt for char in chars) return Token((width, height), links, surfs, token_iswhitespace, token_str)
def block_size(text, font): brokenText = text.replace("\r\n", "\n").replace("\r", "\n").split("\n") height = len(brokenText) * font.get_linesize() width = 0 for line in brokenText: line_width, line_height = font.size(line) width = max(width, line_width) return width, height
def death_screen(surface: Surface, state: Union[State, None]): mid_y = surface.get_height() // 2 surface.fill(Color("black")) font = pygame.font.Font(None, 36) font_small = pygame.font.Font(None, 28) cont = True while cont: time.sleep(0.001) events = pygame.event.get() for event in events: if event.type == pygame.KEYUP: if event.key != pygame.K_ESCAPE: return True return False if state is None: msg = "esc: quit, any other key: new game" text = font.render(msg, False, Color("gray")) textpos = text.get_rect(centerx=surface.get_width() / 2, top=mid_y) surface.blit(text, textpos) pygame.display.flip() else: msg = [ "GAME OVER", f"SCORE: {len(state.snake_bits)}", "esc: quit, any other key: new game", ] text = font.render(msg[0], True, APPLE_COLOR) textpos = text.get_rect(centerx=surface.get_width() / 2, top=mid_y - font.get_linesize() / 2) surface.blit(text, textpos) text = font.render(msg[1], True, SNAKE_COLOR) textpos = text.get_rect(centerx=surface.get_width() / 2, top=mid_y + font.get_linesize() / 2) surface.blit(text, textpos) text = font_small.render(msg[2], True, Color("gray")) textpos = text.get_rect(centerx=surface.get_width() / 2, top=mid_y + 3 * font.get_linesize() / 2) surface.blit(text, textpos) pygame.display.flip()
def make_message_log(self, size: Tuple[int, int], messages: Reversible[Message]) -> None: """Render the messages provided and return them on a surface """ font = self.fonts.get("mini") con = pygame.Surface(size).convert_alpha() con.fill(CONFIG.get_colour("empty")) # start the first one 2 pixels off the bottom y_offset = size[1] - font.get_linesize() for message in reversed(messages): for line in reversed( self.wrap_text(message.full_text, int(size[0]))): print_pos = (0, y_offset) self.render_text(con, line, print_pos, message.fg_col) y_offset -= font.get_linesize() if y_offset < 0: break # log is full return con
def renderLines(lines, font, antialias, color, background=None): fontHeight = font.get_linesize() surfaces = [font.render(ln, antialias, color) for ln in lines] # can't pass background to font.render, because it doesn't respect the alpha maxwidth = max([s.get_width() for s in surfaces]) result = pygame.Surface((maxwidth, len(lines) * fontHeight), pygame.SRCALPHA) if background == None: result.fill((90, 90, 90, 0)) else: result.fill(background) for i in range(len(lines)): result.blit(surfaces[i], (0, i * fontHeight)) return result
def renderText(text, font, antialias, color, size, autosize, wordwrap): """ Renders a text into a surface @param text @param font @param antialias @param color @param size @param autosize @param wordwrap """ lines = text.split('\n') #Wordwrapping if wordwrap and not autosize: for i in xrange(len(lines)): line = lines[i] del lines[i] lines[i:i] = wrapText(line, font, size[0]).split('\n') #Text Rendering if len(lines) == 1: #Single line text return font.render(text, antialias, color) else: #Multi line text lineHeight = font.get_linesize() height = lineHeight * len(lines) width = 0 lineSurfs = [] for line in lines: linesurf = font.render(line, antialias, color) lineSurfs.append(linesurf) if linesurf.get_width() > width: width = linesurf.get_width() surf = pygame.Surface((width,height), pygame.SRCALPHA) for i in xrange(len(lineSurfs)): surf.blit(lineSurfs[i], (0,i * lineHeight)) return surf
def render_names(self, con, game_map, position, tile_size=16): """Renders entity names at a position on the map, at that position on the map""" # grab font from fonst list font = self.fonts.get("mini") # first transpose the given pixel position to map position _x, _y = position # offset values based on map pan _x = _x - self.map_offset[0] _y = _y - self.map_offset[1] tile_x = int(_x / tile_size) tile_y = int(_y / tile_size) # get the entities at that position names = game_map.get_names_at_location(tile_x, tile_y) if names: # offset name so it's not behind the mouse x_pos = position[0] + font.get_linesize() y_pos = position[1] self.render_outlined_text(con, names, (x_pos, y_pos), CONFIG.get_colour("white"), break_on_comma=True)
def renderText(text, font, antialias, color, size, autosize, wordwrap): lines = text.split('\n') #Wordwrapping if wordwrap and not autosize: for i in xrange(len(lines)): line = lines[i] del lines[i] lines[i:i] = wrapText(line, font, size[0]).split('\n') #Text Rendering if len(lines) == 1: #Single line text return font.render(text, antialias, color) else: #Multi line text lineHeight = font.get_linesize() height = lineHeight * len(lines) width = 0 lineSurfs = [] for line in lines: linesurf = font.render(line, antialias, color) lineSurfs.append(linesurf) if linesurf.get_width() > width: width = linesurf.get_width() surf = pygame.Surface((width, height), pygame.SRCALPHA) surf.set_colorkey((0, 0, 0)) for i in xrange(len(lineSurfs)): surf.blit(lineSurfs[i], (0, i * lineHeight)) return surf
import pygame