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 __init__(self, drawFont: font.Font, width=750, height=550): self.current_page = 0 self.pages = [Surface((width, height))] self.pages[0].fill(colour.WHITE) drawArea = rect.Rect(5, 5, width - 5, height - 5) wordHorizontalSpacing = 5 wordVerticalSpacing = 2 leftMargin = 10 topMargin = 7 wordXOffset = leftMargin wordYOffset = topMargin for line in help_text.split('\n'): for word in line.split(): if word == '[p]': wordXOffset = leftMargin wordYOffset = topMargin self.pages.append(Surface((width, height))) self.pages[-1].fill(colour.WHITE) continue renderedWord = drawFont.render(word, True, colour.BLACK) if wordXOffset + renderedWord.get_width( ) + wordHorizontalSpacing > drawArea.width: wordXOffset = leftMargin wordYOffset += wordVerticalSpacing + renderedWord.get_height( ) if wordYOffset + renderedWord.get_height( ) + wordVerticalSpacing > drawArea.height: wordYOffset = topMargin self.pages.append(Surface((width, height))) self.pages[-1].fill(colour.WHITE) self.pages[-1].blit(renderedWord, (wordXOffset, wordYOffset)) wordXOffset += wordHorizontalSpacing + renderedWord.get_width() wordYOffset += drawFont.get_height() + wordVerticalSpacing wordXOffset = leftMargin for i, v in enumerate(self.pages): pageNumberRender = drawFont.render( "<- Page %d of %d ->" % (i + 1, len(self.pages)), True, colour.BLACK) v.blit(pageNumberRender, (v.get_width() - pageNumberRender.get_width() - 3, v.get_height() - pageNumberRender.get_height() - 3))
class TextBox: """ A class to represent interface of button, title, text on screen. ... Attributes ---------- content : string content of text coordinates : tuple/list with 2 dimension coordinates[0] x coordinates[0] y color_text : color variables above color of text color_rect: color variables above color of rectangle Methods ------- update_text(self): update_width_rect(self): update_font_text(self): update_font_size(self): update_coordinates(self): update_color_text(self): update_color_rect(self): update-functions delete(): delete textbox show(screen): draw textbox on screen is_click(self, event): if textbox is button, check is is clicked? get_text_width(self): return width of textbox return_content(self): return content of textbox """ def __init__(self, content, coordinates, color_text, color_rect): self.content = content self.font_text = None self.font_size = 40 self.color_text = color_text font = Font(self.font_text, self.font_size) self.text = font.render(self.content, True, self.color_text) self.coordinates = coordinates self.color_rect = color_rect self.rect = Rect(self.coordinates[0], self.coordinates[1], self.text.get_width() + 10, self.text.get_height() + 5) def update_text(self, content): self.content = content self.text = Font(self.font_text, self.font_size).render(self.content, True, self.color_text) def update_width_rect(self, size): self.rect = Rect(self.coordinates[0], self.coordinates[1], size, self.text.get_height() + 5) def update_font_text(self, font): self.font_text = font font = Font(self.font_text, self.font_size) self.text = font.render(self.content, True, self.color_text) def update_font_size(self, size): self.font_size = size font = Font(self.font_text, self.font_size) self.text = font.render(self.content, True, self.color_text) def update_coordinates(self, coordinates): self.coordinates = coordinates self.rect = Rect(self.coordinates[0], self.coordinates[1], self.text.get_width() + 10, self.text.get_height() + 5) def update_color_text(self, color_text): self.color_text = color_text font = Font(self.font_text, self.font_size) self.text = font.render(self.content, True, self.color_text) def update_color_rect(self, color_rect): self.color_rect = color_rect def delete(self): self.content = None self.text = None self.font_text = None self.font_size = None self.coordinates = None self.color_text = None self.color_rect = None self.rect = None def show(self, screen): rect(screen, self.color_rect, self.rect) screen.blit(self.text, (self.rect.x + 5, self.rect.y + 5)) def is_click(self, event): if self.rect.collidepoint(event): return True def get_text_width(self): return self.text.get_width() def return_content(self): return self.content