如何使用 PyGame 旋转和缩放图像?
在本文中,我们将了解如何旋转和缩放图像。图像缩放是指调整原始图像的大小,图像旋转是指将图像旋转某个角度。坐标平面中的旋转是逆时针的。让我们继续使用所使用的方法和完整的代码来执行这些任务。
缩放图像
为了缩放图像,我们使用pygame.transform.scale(image, DEFAULT_IMAGE_SIZE)方法,我们传递要缩放的图像和我们将根据需要手动设置的默认图像大小。
例子:
使用的图像:
Python3
# Import pygame
import pygame
# Initialise pygame
pygame.init()
# Set window size
size = width,height = 600, 600
screen = pygame.display.set_mode(size)
# Clock
clock = pygame.time.Clock()
# Load image
image = pygame.image.load('gfg.png')
# Set the size for the image
DEFAULT_IMAGE_SIZE = (200, 200)
# Scale the image to your needed size
image = pygame.transform.scale(image, DEFAULT_IMAGE_SIZE)
# Set a default position
DEFAULT_IMAGE_POSITION = (200,200)
# Prepare loop condition
running = False
# Event loop
while not running:
# Close window event
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = True
# Background Color
screen.fill((0, 0, 0))
# Show the image
screen.blit(image, DEFAULT_IMAGE_POSITION)
# Part of event loop
pygame.display.flip()
clock.tick(30)
Python3
# Import pygame
import pygame
# Initialise pygame
pygame.init()
# Set window size
size = width,height = 600, 600
screen = pygame.display.set_mode(size)
# Clock
clock = pygame.time.Clock()
# Load image
image = pygame.image.load('gfg.png')
# Set the size for the image
DEFAULT_IMAGE_SIZE = (200, 200)
# Rotate the image by any degree
image = pygame.transform.rotate(image, 180)
# Set a default position
DEFAULT_IMAGE_POSITION = (200,200)
# Prepare loop condition
running = False
# Event loop
while not running:
# Close window event
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = True
# Background Color
screen.fill((0, 0, 0))
# Show the image
screen.blit(image, DEFAULT_IMAGE_POSITION)
# Part of event loop
pygame.display.flip()
clock.tick(30)
Python3
# Import pygame
import pygame
# Initialise pygame
pygame.init()
# Set window size
size = width,height = 600, 600
screen = pygame.display.set_mode(size)
# Clock
clock = pygame.time.Clock()
# Load image
image = pygame.image.load('gfg.png')
# Set the size for the image
DEFAULT_IMAGE_SIZE = (200, 200)
# Scale the image to your needed size
image = pygame.transform.scale(image, DEFAULT_IMAGE_SIZE)
# Rotate the image by any degree
image = pygame.transform.rotate(image, 90)
# Set a default position
DEFAULT_IMAGE_POSITION = (200,200)
# Prepare loop condition
running = False
# Event loop
while not running:
# Close window event
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = True
# Background Color
screen.fill((0, 0, 0))
# Show the image
screen.blit(image, DEFAULT_IMAGE_POSITION)
# Part of event loop
pygame.display.flip()
clock.tick(30)
输出:
旋转图像
要旋转我们使用我们传递我们要旋转并通过旋转来完成度的图像pygame.transform.rotate(图像,学位)方法的图像。
例子:
蟒蛇3
# Import pygame
import pygame
# Initialise pygame
pygame.init()
# Set window size
size = width,height = 600, 600
screen = pygame.display.set_mode(size)
# Clock
clock = pygame.time.Clock()
# Load image
image = pygame.image.load('gfg.png')
# Set the size for the image
DEFAULT_IMAGE_SIZE = (200, 200)
# Rotate the image by any degree
image = pygame.transform.rotate(image, 180)
# Set a default position
DEFAULT_IMAGE_POSITION = (200,200)
# Prepare loop condition
running = False
# Event loop
while not running:
# Close window event
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = True
# Background Color
screen.fill((0, 0, 0))
# Show the image
screen.blit(image, DEFAULT_IMAGE_POSITION)
# Part of event loop
pygame.display.flip()
clock.tick(30)
输出:
旋转和缩放图像
让我们看看如何执行给定图像的缩放和旋转。我们将设置合适的默认图像大小以及我们希望在窗口屏幕上看到图像的默认图像位置。上面解释的相同方法将用于缩放和旋转图像。
例子:
蟒蛇3
# Import pygame
import pygame
# Initialise pygame
pygame.init()
# Set window size
size = width,height = 600, 600
screen = pygame.display.set_mode(size)
# Clock
clock = pygame.time.Clock()
# Load image
image = pygame.image.load('gfg.png')
# Set the size for the image
DEFAULT_IMAGE_SIZE = (200, 200)
# Scale the image to your needed size
image = pygame.transform.scale(image, DEFAULT_IMAGE_SIZE)
# Rotate the image by any degree
image = pygame.transform.rotate(image, 90)
# Set a default position
DEFAULT_IMAGE_POSITION = (200,200)
# Prepare loop condition
running = False
# Event loop
while not running:
# Close window event
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = True
# Background Color
screen.fill((0, 0, 0))
# Show the image
screen.blit(image, DEFAULT_IMAGE_POSITION)
# Part of event loop
pygame.display.flip()
clock.tick(30)
输出: