Interesting Python Tutorial: Flip Images with Pygame

王林
Release: 2023-04-17 14:32:15
forward
1559 people have browsed it

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)
Copy after login

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()
Copy after login

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()
Copy after login

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template