有趣的 Python 教程:Pygame 翻轉圖像
作者:聆聽世界的魚
在本文中,我們將了解如何使用 Pygame 翻轉圖像。要翻轉圖像,我們需要使用 pygame.transform.flip(Surface, xbool, ybool) 方法,該方法被調用來根據我們的需要在垂直方向或水平方向翻轉圖像。
在本文中,我們將了解如何使用 Pygame 翻轉圖像。
要翻轉圖像,我們需要使用 pygame.transform.flip(Surface, xbool, ybool) 方法,該方法被調用來根據我們的需要在垂直方向或水平方向翻轉圖像。
語法:
pygame.transform.flip(Surface, xbool, ybool)
原始圖像如下:
垂直翻轉圖像
我們在垂直方向上翻轉圖像。我們將使用 pygame.transform.flip() 來垂直顯示圖像。將 xbool 作為 True 和 ybool 作為 False 傳遞,這樣圖像就會垂直翻轉。
代碼如下:
# 導入 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()
效果圖如下:
水平方向翻轉圖像
我們在水平方向翻轉圖像。對于這個 xbool 作為 False 和 ybool 作為 True 傳遞,水平翻轉它。代碼如下:
# 導入 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()
顯示如下:
OK,你學會了嗎?
責任編輯:龐桂玉
來源:
Linux公社