Introduction
Pygame is a multimedia toolkit based on Python that may be used to create games and other multimedia applications. The pygame module will be used in this course to construct various forms on the screen while considering their height, width, and location in the pygame window.
SDL stands for Simple DirectMedia Layer, and Pygame is a Python wrapper for it. SDL allows you to utilize any platform to access your system's multimedia hardware, including sound, video, mouse, keyboard, and joystick. The pygame project was created to replace the now-defunct PySDL project. Because SDL and pygame are cross-platform, you can create games and rich multimedia Python apps on any platform that supports them!
Pygame
This tutorial will look at how Pygame may be used to flip pictures.
To flip the picture, use the pygame.transform.flip(Surface, xbool, ybool) function, which may be used to convert the image vertically or horizontally depending on our needs.
Syntax
pygame.transform.flip(Surface, xbool, ybool)
INPUT
Flip the image in the vertical direction
To do so, we must first flip the image vertically. To make the picture vertical, we'll use pygame.transform. Flip (). Pass True for xbool and False for ybool to flip the image vertically.
CODE
# pygame and sys import
import pygame
import sys
from pygame.locals import *
# All imported modules are initialised via pygame.init().
pygame.init()
pygame.display.set_caption('CodingNinjas')
# display size on screen
scrn = pygame.display.set_mode((600, 400), 0, 32)
# pygame.image.load() will returns object
imge = pygame.image.load('cn_pygame.png')
while True:
#color of background
scrn.fill((255, 255, 255))
# imge copy
imge_copy = imge.copy()
# pygame.transform.flip()->flips the image
imge_wth_flp = pygame.transform.flip(imge_copy, True, False)
# surface.blit() function draws a source
# Surface onto this Surface.
scrn.blit(imge_wth_flp, (50 + 1 * 120, 100))
# event listener to quit screen
for vnt in pygame.event.get():
if vnt.type == QUIT:
pygame.quit()
sys.exit()
# frames per second
pygame.display.update()
OUTPUT
Flip the image in the horizontal direction
In this case, we must flip the picture horizontally. To convert it horizontally, xbool is set to False, and ybool is set to True.
CODE
# pygame and sys import
import pygame
import sys
from pygame.locals import *
# All imported modules are initialised via pygame.init().
pygame.init()
pygame.display.set_caption('CodingNinjas')
# display screen
scrn = pygame.display.set_mode((600, 400), 0, 32)
# pygame.image.load() returns object
imge = pygame.image.load('cn_pygame.png')
while True:
# color of background
scrn.fill((255, 255, 255))
# copy of image
imge_copy = imge.copy()
# pygame.transform.flip() flips the photo
imge_with_flip = pygame.transform.flip(imge_copy, False, True)
# surface.blit() function draws a source
# Surface onto this Surface.
scrn.blit(img_with_flip, (50 + 1 * 120, 100))
# event listener to quit screen
for vnt in pygame.event.get():
if vnt.type == QUIT:
pygame.quit()
sys.exit()
# update the frame per second
pygame.display.update()