'How to laterally invert images in pygame?

I'm trying to use a sprite sheet, however it only contains images of the character facing right, I want to laterally invert these images when making the character move left. I can't figure out how to do so, if anyone knows how please feel free to let me know!



Solution 1:[1]

You can flip an image with pygame.transform.flip:

flipped_image = pygame.transform.flip(image, True, False)

Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((300, 150))
clock = pygame.time.Clock()

image = pygame.image.load('bird.png').convert_alpha()
flipped_image = pygame.transform.flip(image, True, False)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    window.fill(0)
    window.blit(image, image.get_rect(center = (75, 75)))
    window.blit(flipped_image, flipped_image.get_rect(center = (225, 75)))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rabbid76