pygame.transform.flip
时间: 2023-10-14 17:28:57 浏览: 49
pygame.transform.flip() is a method in the Pygame library that is used to flip an image along its axis. This method can be used to create mirror images or to change the orientation of an image. The method takes three arguments: the image to be flipped, a boolean value indicating whether to flip the image horizontally, and a boolean value indicating whether to flip the image vertically.
Syntax:
```
pygame.transform.flip(surface, xbool, ybool)
```
- surface: The image to be flipped.
- xbool: A boolean value indicating whether to flip the image horizontally (True or False).
- ybool: A boolean value indicating whether to flip the image vertically (True or False).
Example:
```
import pygame
pygame.init()
# Set up the display
display_width = 640
display_height = 480
game_display = pygame.display.set_mode((display_width, display_height))
# Load the image
my_image = pygame.image.load("image.png")
# Flip the image horizontally
flipped_image = pygame.transform.flip(my_image, True, False)
# Display the original and flipped images
game_display.blit(my_image, (0, 0))
game_display.blit(flipped_image, (display_width/2, 0))
pygame.display.update()
# Quit the game
pygame.quit()
```
In the above example, we first load an image using the `pygame.image.load()` method. We then use the `pygame.transform.flip()` method to create a flipped version of the image by passing in `True` as the first argument to indicate that we want to flip the image horizontally. We then display both the original and flipped images using the `blit()` method. Finally, we update the display and quit the game.