Home >Backend Development >Python Tutorial >Interesting Python Tutorial: Flip Images with Pygame
In this article, we will learn how to flip an image using Pygame.
To flip the image, we need to use the pygame.transform.flip(Surface, xbool, ybool) method, which is called Flip the image vertically or horizontally according to our needs.
Syntax:
pygame.transform.flip(Surface, xbool, ybool)
The original image is as follows:
We flip the image vertically. We will use pygame.transform.flip() to display the image vertically. Pass xbool as True and ybool as False so the image is flipped vertically.
The code is as follows:
# 导入 pygame 和 sys import pygame import sys from pygame.locals import * # 初始化pygame # 导入模块 pygame.init() pygame.display.set_caption('www.linuxmi.com') # 图像大小将显示在屏幕上 screen = pygame.display.set_mode((1300, 600), 0, 32) # pygame.image.load() 将返回 # 有图像的对象 img = pygame.image.load('linuxmi.com.png') while True: # 背景颜色 screen.fill((255, 255, 255)) # 复制图像 img_copy = img.copy() # pygame.transform.flip() 将翻转图像 img_with_flip = pygnsformame.tra.flip(img_copy, False, True) # surface.blit() 函数绘制一个源 # 在这个表面上 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()
The rendering is as follows:
We flip the image horizontally. For this pass xbool as False and ybool as True, flip it horizontally. The code is as follows:
# 导入 pygame 和 sys import pygame import sys from pygame.locals import * # 初始化pygame # 导入模块 pygame.init() pygame.display.set_caption('www.linuxmi.com') # 图像大小将显示在屏幕上 screen = pygame.display.set_mode((1300, 600), 0, 32) # pygame.image.load() 将返回 # 有图像的对象 img = pygame.image.load('linuxmi.com.png') while True: # 背景颜色 screen.fill((255, 255, 255)) # 复制图像 img_copy = img.copy() # pygame.transform.flip() 将翻转图像 img_with_flip = pygame.transform.flip(img_copy, False, True) # surface.blit() 函数绘制一个源 # 在这个表面上 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()
is displayed as follows:
##OK ,Have you learned it?
The above is the detailed content of Interesting Python Tutorial: Flip Images with Pygame. For more information, please follow other related articles on the PHP Chinese website!