Home  >  Article  >  Backend Development  >  Interesting Python Tutorial: Flip Images with Pygame

Interesting Python Tutorial: Flip Images with Pygame

王林
王林forward
2023-04-17 14:32:151483browse

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:

Interesting Python Tutorial: Flip Images with Pygame

Flip the image vertically

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:

Interesting Python Tutorial: Flip Images with Pygame

Flip the image horizontally

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:

Interesting Python Tutorial: Flip Images with Pygame


##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!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete