From 6d318e5a0e51913414006173040aeea527780c47 Mon Sep 17 00:00:00 2001 From: izucked Date: Sun, 14 Aug 2022 20:34:05 +0100 Subject: [PATCH] Fixed 'Couldn't find glyph' exception bug --- src/symbol.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/symbol.py b/src/symbol.py index ac41324..7d9a10d 100644 --- a/src/symbol.py +++ b/src/symbol.py @@ -1,6 +1,8 @@ from random import choice, randrange +from typing import List import pygame +from pygame.font import Font from config import Config @@ -15,6 +17,18 @@ font = pygame.font.Font(Config.FONT_PATH, Config.FONT_SIZE) +def get_char_surfaces(font: Font, char_set: List[chr], char_color: pygame.Color) -> List[pygame.Surface]: + surfaces = [] + for char in char_set: + try: + surfaces.append(font.render(char, True, char_color)) + except pygame.error as e: + # In case of 'Couldn't find glyph' error + if "Couldn't find glyph" in e.args[0]: + continue + return surfaces + + class Symbol: def __init__(self, x, y, speed, color): @@ -25,7 +39,7 @@ def __init__(self, x, y, speed, color): self.interval = randrange(5, 30) self.state = NOT_PLACED - self.charSet = [font.render(char, True, color) for char in katakana] + self.charSet = get_char_surfaces(font, katakana, color) self.surface = choice(self.charSet) def update(self): @@ -68,11 +82,11 @@ def __init__(self, pos_x, start_y, placeable_positions_list): self.next_placement_pos = 0 for n, i in enumerate( - range( - start_y, - start_y - Config.FONT_SIZE * self.column_height, - -Config.FONT_SIZE - ) + range( + start_y, + start_y - Config.FONT_SIZE * self.column_height, + -Config.FONT_SIZE + ) ): if n == 0: # Let first symbol be white @@ -110,8 +124,8 @@ def get_white_symbol(self): def check_white_symbol(self): if ( - self.get_white_symbol().get_y_position() == self.next_placement_pos - and self.next_placement_pos != -1 + self.get_white_symbol().get_y_position() == self.next_placement_pos + and self.next_placement_pos != -1 ): self.next_placement_pos = ( self.placeable_positions.pop(0)