Pygame – Flip the image
Last Updated :
30 Jun, 2021
In this article, we are going to see how images can be flipped using Pygame.
To flip the image we need to use pygame.transform.flip(Surface, xbool, ybool) method which is called to flip the image in vertical direction or horizontal direction according to our needs.
Syntax:
pygame.transform.flip(Surface, xbool, ybool)
Flip the image in vertical direction
In this, we have to flip the image in a vertical direction. We will use pygame.transform.flip() to display the image in vertical. Pass xbool as True and ybool as False, so that the image is flipped vertically.
Input used:
Python3
import pygame
import sys
from pygame. locals import *
pygame.init()
pygame.display.set_caption( 'GeeksforGeeks' )
screen = pygame.display.set_mode(( 600 , 400 ), 0 , 32 )
img = pygame.image.load( 'image.png' )
while True :
screen.fill(( 255 , 255 , 255 ))
img_copy = img.copy()
img_with_flip = pygame.transform.flip(img_copy, True , False )
screen.blit(img_with_flip, ( 50 + 1 * 120 , 100 ))
for event in pygame.event.get():
if event. type = = QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
|
Output
Flip the image in horizontal direction
In this we have to flip the image in horizontal direction. For this xbool is passed as False and ybool as True, to flip it horizontally.
Program:
Python3
import pygame
import sys
from pygame. locals import *
pygame.init()
pygame.display.set_caption( 'GeeksforGeeks' )
screen = pygame.display.set_mode(( 600 , 400 ), 0 , 32 )
img = pygame.image.load( 'image.png' )
while True :
screen.fill(( 255 , 255 , 255 ))
img_copy = img.copy()
img_with_flip = pygame.transform.flip(img_copy, False , True )
screen.blit(img_with_flip, ( 50 + 1 * 120 , 100 ))
for event in pygame.event.get():
if event. type = = QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
|
Output
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...