如何横向反转 pygame 中的图像?

How to laterally invert images in pygame?

我正在尝试使用 sprite sheet,但它只包含面向右侧的角色图像,我想在使角色向左移动时横向反转这些图像。我不知道该怎么做,如果有人知道该怎么做,请随时告诉我!

您可以使用 pygame.transform.flip 翻转图像:

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

最小示例:

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()