pythonpygamesprite

Pygame - Load letters and display text for an image


I'm following Tech with Tim's guide on how to create a platformer game link

I created a function to load letters from a sprite image, based on his load_sprites function:

def load_letters_from_sprite_sheets(w, h):
    path = join('Assets', 'Menu', 'Text',)
    images = [f for f in listdir(path) if isfile(join(path, f))]

    all_images = {}

    for image in images:
        text_sheet = pygame.image.load(join(path, image)).convert_alpha()
        letters = []

        for i in range(text_sheet.get_width() // w):
            for j in range(text_sheet.get_height() // h):
                surface = pygame.Surface((w, h), pygame.SRCALPHA, 32)
                rect = pygame.Rect(i * w, j * h, w, h)
                surface.blit(text_sheet, (0, 0), rect)
                letters.append(pygame.transform.scale2x(surface))

        all_images[image.replace('.png', '')] = letters

    return all_images

To load an image which contains letters (Called on Game().init)

self.letter_sheets = load_letters_from_sprite_sheets( 8, 10)

The image with the letters So I created a small function to load the letters into a list:

def display_text(window, all_images, x, y, text: str, color = 'white', size = 1.0):
    if color.lower() == 'white':
        src_image = all_images['Text (White) (8x10)']
    else:
        src_image = all_images['Text (Black) (8x10)']
    
    for i in range(len(src_image)):
        #print(f'i: {i}, img: {src_image[i]}')
        window.blit(src_image[i], (x + i * src_image[i].get_width() , y))

However, when I try to display the text, it doesn't show (Called on Game mainloop):

display_text(self.window, self.letter_sheets, 100, 100, 'abcdefghijklmnopqrstuvwxyz', 'white')

What am I missing here? Thanks for the help :)

I tried displaying all the letters loaded from the image (without considering the user input for now), but it didn't show on screen.


Solution

  • I reproduced your code here:

    It displays something, you could have provided the necessary imports you used to make it clearer. So I assume you have a configuration error somewhere else.

    It's always a good idea to create a minimal example of your error. Use as little code as possible to recreate the problem at hand.

    Another issue is that your mapping is off. You are loading a lot of empty rectangles you need to change the mapping. Also, you are not using the text input at all and just iterate over all letters.Maybe this is on purpose for debugging. A better display text function:

    def display_text(window, letter_images, x, y, text: str, color='white', size=1.0):
        char_map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,;:?!()+-"
        
        for char in text:
            if char in char_map:
                index = char_map.index(char)
                letter_image = letter_images[index]
                scaled_image = pygame.transform.scale(letter_image, (int(letter_image.get_width() * size), int(letter_image.get_height() * size)))
                window.blit(scaled_image, (x, y))
                x += scaled_image.get_width()