def update(self, font: pygame.font) -> None: """ Updates the XPBar surface :param font: Font to be used to render the text :return: None """ self.fill(XPBAR_BACKGROUND) # Get current XP and player level xp, lvl = DataLoader.player_data["xp"], DataLoader.player_data["level"] # XP needed per level = level * 100 xp_needed = lvl * 100 # Draw inner XP indicator pygame.draw.rect( self, XPBAR_BAR, pygame.Rect(0, 0, self.__width - (self.__width * (1 - (xp / xp_needed))), self.__height)) # Draw outer border pygame.draw.rect(self, XPBAR_BORDER, pygame.Rect(0, 0, self.__width, self.__height), 15) # Draw text displaying level and XP lvl_str = f"Level {lvl}: {xp}/{xp_needed}" self.blit(font.render(lvl_str, True, TEXT_COLOUR), ((self.__width // 2) - (font.size(lvl_str)[0] // 2), (self.__height // 2) - (font.size(lvl_str)[1] // 2)))
def update(self, font: pygame.font, player_mana: int) -> None: """ Updates mana bar surface :param font: Font to be used to render the text :param player_mana: The amount of mana the player has :return: None """ self.fill((0, 0, 0, 0)) # Amount of mana left health_txt = str(int( (player_mana / self.__max_mana) * self.__max_mana)) # Border for mana bar pygame.draw.rect( self, MANABAR_BACKGROUND_COLOUR, pygame.Rect( font.size(health_txt)[0], 0, self.__width - font.size(health_txt)[0], self.__height), 2) # Main bar that shows amount pygame.draw.rect( self, MANABAR_BACKGROUND_COLOUR, pygame.Rect( font.size(health_txt)[0], 0, (self.__width - font.size(health_txt)[0]) * (player_mana / self.__max_mana), self.__height)) self.blit(font.render(health_txt, True, MANABAR_TEXT_COLOUR), (0, self.__height // 4))
def update(self, font: pygame.font) -> None: """ Updates the Tab surface :param font: Font to be used to render the text :return: None """ self.fill(TAB_BACKGROUND) # Draw each tab pygame.draw.rect(self, TAB_BORDER, self.__sects[0], 0 if self.selected_equipment else 2) pygame.draw.rect(self, TAB_BORDER, self.__sects[1], 0 if not self.selected_equipment else 2) # Draw text self.blit( font.render( "Equipment", True, TAB_SELECTED_TEXT_COLOUR if self.selected_equipment else TEXT_COLOUR), ((self.__sects[0].w // 2) - (font.size("Equipment")[0] // 2), (self.__sects[0].y + (self.__sects[0].h // 2)) - (font.size("Equipment")[1] // 2))) self.blit( font.render( "Attributes", True, TAB_SELECTED_TEXT_COLOUR if not self.selected_equipment else TEXT_COLOUR), (self.__sects[1].x + ((self.__sects[1].w // 2) - (font.size("Attributes")[0] // 2)), (self.__sects[1].y + (self.__sects[1].h // 2)) - (font.size("Equipment")[1] // 2)))
def update(self, font: pygame.font) -> None: """ Updates ItemDropDisplay surface each frame :param font: Font in which the text should be written in :return: None """ self.fill(ITEM_DROP_DISPLAY_BACKGROUND) for pos, item in enumerate(self.__items): # Background rect with the rarity of the item as its colour pygame.draw.rect( self, DataLoader.rarities[DataLoader.possible_items[item[0]] ["rarity"]], pygame.Rect(self.width - item[1].get_width(), (pos * item[1].get_height()), item[1].get_width(), item[1].get_height())) # Dimensions which the text will take up str_width, str_height = font.size(item[0]) # Draw item image self.blit(item[1], (self.width - item[1].get_width(), (pos * item[1].get_height()))) # Draw item name text self.blit(font.render(item[0], True, TEXT_COLOUR), (self.width - item[1].get_width() - str_width, (pos * item[1].get_height()) + (str_height // 2)))
def update(self, font: pygame.font) -> None: """ Updates the Attributes surface :param font: Font to be used to render the text :return: None """ self.fill(ATTRIBURES_BACKGROUND) self.__attr_data = DataLoader.player_data["attributes"] # Draw outer border pygame.draw.rect(self, ATTRIBURES_BORDER, pygame.Rect(0, 0, self.__width, self.__height), 5) # Loop through attribute data for pos, key in enumerate(self.__attr_data): # Draw attr number box pygame.draw.rect( self, ATTRIBURES_BORDER, pygame.Rect(self.__width // 8, (self.__height // 5) * (pos + 1), self.__width // 2, self.__button_size * 2), 5) # Draw plus and minus images self.blit(self.__plus_img, self.__pluses[pos]) self.blit(self.__minus_img, self.__minuses[pos]) # Draw attr names and attr number text self.blit(font.render(key, True, TEXT_COLOUR), (((self.__width // 8) + (self.__width // 4)) - (font.size(key)[0] // 2), ((self.__height // 5) * (pos + 1)) - font.size(key)[1])) self.blit( font.render(str(self.__attr_data[key]), True, TEXT_COLOUR), (((self.__width // 8) + (self.__width // 4)) - (font.size(str(self.__attr_data[key]))[0] // 2), ((self.__height // 5) * (pos + 1)) + font.size(str(self.__attr_data[key]))[1])) # Draw remaining skill point indicator pygame.draw.rect( self, ATTRIBURES_SP_BORDER, pygame.Rect(self.__width // 5, self.__height - 30, (self.__width // 5) * 3, 25)) self.blit( font.render( f"Unused SP: {DataLoader.player_data['unused_sp']}", True, TEXT_COLOUR), (self.__width // 5, self.__height - 25))
def update(self, font: pygame.font, data: dict) -> None: """ Updates skill tree surface each frame :param font: Font to use to draw text :param data: Data to determine which rect was collided :return: None """ self.fill(SKILLTREE_BACKGROUND) # Get skill points text to be drawn text = f"Unused SP: {DataLoader.player_data['unused_sp']}" # Draw border of surface pygame.draw.rect(self, SKILLTREE_BORDER, pygame.Rect(0, 0, self.__width, self.__height), 5) # Draw skill point border pygame.draw.rect( self, SKILLTREE_SP_BORDER, pygame.Rect((self.__width // 2) - (font.size(text)[0] // 2), self.__height - font.size(text)[1], font.size(text)[0], font.size(text)[1])) # Draw skill point text self.blit(font.render(text, True, TEXT_COLOUR), ((self.__width // 2) - (font.size(text)[0] // 2), self.__height - font.size(text)[1])) # Draw connecting lines between each tree element for line in self.__lines: pygame.draw.aaline(self, (255, 255, 255), (line[0][0], line[0][1]), (line[1][0], line[1][1])) # Draw skill image for pos, rect in enumerate(self.__level_rects): # Draw border around image if the mouse is hovered over it if data is not None: if data["st_pos"] == rect: pygame.draw.rect(self, (255, 255, 255), rect, 5) self.blit( self.__imgs[pos] if self.__levels[pos]["elem"].tag in DataLoader.player_data["skills"] else self.__bw_imgs[pos], rect)
def display_images(content: str, font_height: int, fontObj: pygame.font) -> int: img = pygame.image.load(content) textRectObj = img.get_rect() self.surface_display.blit( img, (self.window_frame[0] - fontObj.size('To Move:')[0] - self.square_frame[0], font_height)) font_height += textRectObj.height return font_height
def update(self, font: pygame.font, data: dict) -> None: """ Updates the Equipment surface :param font: Font to be used to render the text :param data: Dict containing the current item the mouse is over :return: None """ self.fill(EQUIPMENT_BACKGROUND) # Get current equipped armour imgs and names self.__imgs = self.__get_imgs() self.__armor_names = self.__get_armor_names() # Draw outer border pygame.draw.rect(self, EQUIPMENT_BORDER, pygame.Rect(0, 0, self.__width, self.__height), 5) # Create defense and coin strings defense = f"Defense: {str(sum([DataLoader.possible_items[i]['defense'] for i in self.__armor_names]))}" coins = f"Coins: {DataLoader.player_data['coins']}" # Draw the defense text self.blit(font.render(defense, True, TEXT_COLOUR), (5, self.__height - font.size(defense)[1])) # Draw the coins text self.blit(font.render(coins, True, COIN_TEXT_COLOUR), (self.__width - font.size(coins)[0] - 5, self.__height - font.size(defense)[1])) # Have to use pos like this as you cant enumerate the loop pos = 0 # Draw the armour slots for slot, img, armor in zip(self.__slots, self.__imgs, self.__armor_names): pygame.draw.rect( self, DataLoader.rarities[DataLoader.possible_items[armor] ["rarity"]], slot) self.blit(img, slot) if data is not None: if pos == data["eq_pos"]: pygame.draw.rect(self, EQUIPMENT_SELECTED, slot, 5) pos += 1
def draw_text(text: str, font: pygame.font, color: Tuple[int, int, int], width: int, background: Tuple[int, int, int] = None): """ Renders a pygame surface containing word-wrapped text Args: text: the text to be rendered font: the pygame font object to use width: the maximum width of the text block (height is considered unlimited) Returns: Pygame surface containing the rendered text """ if width == 0: return font.render(text, True, color, background) lines = [] # First loop: determine which words are placed in each line for in_line in text.split('\n'): line = "" words = in_line.split(' ') for word in words: if not line == "": line += " " if (font.size(line + word)[0] > width): lines.append(line) line = word.replace('\n', '') else: line += word lines.append(line) # Second loop: render each line individually rendered_lines = [] total_height = 0 for line in lines: rendered_line = font.render(line, True, color, background) total_height += rendered_line.get_height() rendered_lines.append(rendered_line) # Third loop: bring everything together into one surface object final_surface = pygame.Surface((width, total_height)) y = 0 for rendered_line in rendered_lines: final_surface.blit(rendered_line, (0, y)) y += rendered_line.get_height() - 10 return final_surface
def update(self, name: str, font: pygame.font, data: dict) -> None: """ Updates inspector surface each frame :param name: Name of the item being inspected :param font: Font used to write the text :param data: Dict containing data, e.g {'img': pygame.image, 'attr': {..., ...}} :return: None """ self.fill(INSPECTOR_BACKGROUND) if data is not None: if data["st_pos"] is not None: rarity_col = (0, 0, 0) else: rarity_col = DataLoader.rarities[data["attr"]["rarity"]] # Draw border of inspector with colour of item rarity pygame.draw.rect(self, rarity_col, pygame.Rect(0, 0, self.__width, self.__height), 5) # Draw background of image location pygame.draw.rect( self, rarity_col, pygame.Rect(self.__width // 2 - (data["img"].get_width() // 2), 10, data["img"].get_width(), data["img"].get_height())) # Draw name with underline font.set_underline(True) self.blit(font.render(name, True, TEXT_COLOUR), (self.__width // 2 - (font.size(name)[0] // 2), 90)) font.set_underline(False) # If there is data provided, loop through and draw it self.blit(data["img"], (self.__width // 2 - (data["img"].get_width() // 2), 10)) for pos, k in enumerate(data["attr"]): self.blit( font.render(f"{k}: {data['attr'][k]}", True, TEXT_COLOUR), (10, 110 + (20 * pos)))