def __init__(self, x, y, width, height, msg, size=16, text_colour=base.BLACK, bg_colour=base.WHITE, highlight_text=base.WHITE, highlight_bg=base.BLACK, offset_x=0, offset_y=0, bold=False): self.normal = pygame.Surface((width, height)) self.normal.fill(bg_colour) font = Font(_FONT, size) font.set_bold(bold) font_width, font_height = font.size(msg) font_x = (width - font_width) / 2 font_y = (height - font_height) / 2 self.normal.blit(font.render(msg, False, text_colour), (font_x, font_y)) self.highlighted = pygame.Surface((width, height)) self.highlighted.fill(highlight_bg) self.highlighted.blit(font.render(msg, False, highlight_text), (font_x, font_y)) self.offset_x = offset_x self.offset_y = offset_y self.force_highlight = False self.disabled = False PicassoAsset.__init__(self, self.normal, x, y)
def initFont(name, ttfFile, size, bold = 0, italic = 0, underline = 0): global fontFaces if name in fontFaces: del fontFaces[name] font = Font(ttfFile, size) font.set_bold(bold) font.set_italic(italic) font.set_underline(underline) fontFaces[name] = font
def initFont(name, ttfFile, size, bold=0, italic=0, underline=0): global fontFaces if name in fontFaces: del fontFaces[name] font = Font(ttfFile, size) font.set_bold(bold) font.set_italic(italic) font.set_underline(underline) fontFaces[name] = font
class Menu: def __init__(self, x, y, fontsize, space, color_fg, color_bg, background, titles): self.titles = titles self.images = [] self.rects = [] self.himages = [] self.color_fg = color_fg self.color_bg = color_bg self.space = space self.background = data.load_image(background) self.index = 0 self.x = x self.y = y self.font = Font(data.filepath('fonts', 'vera.ttf'), fontsize) self.font.set_bold(True) self.fonth = Font(data.filepath('fonts', 'vera.ttf'), fontsize+5) self.fonth.set_bold(True) def init(self): dy = 0 for title in self.titles: image = self.font.render(title, True, self.color_bg) himage = self.fonth.render(title, True, self.color_fg) rect = image.get_rect() rect.move_ip(self.x, self.y + dy) dy += rect.height + self.space self.images.append(image) self.himages.append(himage) self.rects.append(rect) def run(self, screen): def blit(screen): screen.blit(self.background, (0,0)) for image, rect in zip(self.images, self.rects): if image != self.images[self.index]: screen.blit(image, rect) screen.blit(self.himages[self.index], self.rects[self.index]) pygame.display.update() done = False blit(screen) while not done: for event in pygame.event.get(): if event.type == QUIT: sys.quit() if event.type == KEYDOWN: if event.key == K_DOWN: self.index = min(self.index+1, len(self.images)-1) blit(screen) if event.key == K_UP: self.index = max(self.index-1, 0) blit(screen) if event.key == K_RETURN: done = True return self.index
def create_font_object(font) -> Font: obj = None if isinstance(font, (tuple, list)): if font[0] is not None and os.path.isfile(font[0]): obj = Font(*font[0:2]) if "bold" in font: obj.set_bold(True) if "italic" in font: obj.set_italic(True) else: obj = SysFont(*font[0:2], bold=bool("bold" in font), italic=bool("italic" in font)) if "underline" in font: obj.set_underline(True) elif isinstance(font, Font): obj = font else: obj = SysFont(pygame.font.get_default_font(), 15) return obj
def font_constructor(fontpath, size, bold, italic): """ pygame.font specific declarations :param fontpath: path to a font. :param size: size of a font. :param bold: bold style, True or False. :param italic: italic style, True or False. :return: A font.Font object. """ font = Font(fontpath, size) if bold: font.set_bold(True) if italic: font.set_italic(True) return font
def render_text(self, msg): font = Font(_FONT, self.size) font.set_bold(self.bold) new_surface = pygame.Surface((self.width, self.height), pygame.SRCALPHA, 32) msg = msg.strip("\n") lines = msg.split("\n") longest = max(lines, key=lambda x: len(x)) #new_surface = pygame.Surface((self.width, self.height)) temp_surface = font.render(longest, False, self.colour) msg_width = temp_surface.get_size()[0] msg_height = font.get_height() * len(lines) msg_x = (new_surface.get_width() - msg_width) / 2 msg_y = (new_surface.get_height() - msg_height) / 2 for index, line in enumerate(lines): font_surface = font.render(line, False, self.colour) new_surface.blit(font_surface, (msg_x, msg_y + (font.get_height() * index))) self.surface = new_surface
def render_text(self, msg): font = Font(_FONT, self.size) font.set_bold(self.bold) self.surface = font.render(msg, False, self.colour)
class Application: def init(self): pygame.init() self.screen = pygame.display.set_mode((SCREEN_W, SCREEN_H)) self.clock = pygame.time.Clock() pygame.display.set_caption("Twist'em All !") pygame.time.set_timer(USEREVENT, 1000) self.font = Font(data.filepath('fonts', 'vera.ttf'), 48) self.font.set_bold(True) def run(self): option = 0 items = ['Play', 'Quit', 'Help'] menu = Menu(400, 200, 25, 40, (255,0,0), (0, 0,0), 'title-background.png', items) menu.init() map_number = 0 won = False while option != 1 and map_number < 4: self.screen.set_clip(Rect(0, 0, SCREEN_W, SCREEN_H)) if not won: option = menu.run(self.screen) if option == 0: w_map = Map((640, 440)) w_map.load(maps[map_number]) panel = Panel(w_map) won = self.run_map(w_map, panel) if won: map_number += 1 if map_number >= 4: menu = Menu(300, 200, 18, 40, (255,0,0), (0, 0,0), 'title-background.png', ['You finished the game', 'More levels comming soon...']) menu.init() menu.run(self.screen) def run_map(self, world_map, panel): panel.draw_background(self.screen) while not world_map.finish: for event in pygame.event.get(): if event.type == QUIT: sys.exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: return False if event.type == USEREVENT: print 'Frames per second:', self.clock.get_fps() keys = pygame.key.get_pressed() world_map.update(keys) world_map.draw(self.screen, (0, 40)) panel.draw(self.screen) pygame.display.update(world_map.update_rects) pygame.display.update(panel.rect) self.clock.tick(FPS) if world_map.won: aditional = '' if world_map.main_character.life == world_map.main_character.life_max: aditional = ' (Perfect)' text = self.font.render('You Won !%s' % aditional , True, (0, 170, 0)) else: text = self.font.render('You Lost !', True, (170, 0, 0)) rect = text.get_rect() rect.centerx = SCREEN_W / 2 rect.centery = SCREEN_H / 2 self.screen.set_clip(Rect(0, 0, SCREEN_W, SCREEN_H)) self.screen.blit(text, rect) pygame.display.update() done = False pygame.time.wait(2000) return world_map.won