pygame.transform 模塊允許您對(duì)加載、創(chuàng)建后的圖像進(jìn)行一系列操作,比如調(diào)整圖像大小、旋轉(zhuǎn)圖片等操作,常用方法如下所示:
方法 | 說(shuō)明 |
---|---|
pygame.transform.scale() | 將圖片縮放至指定的大小,并返回一個(gè)新的 Surface 對(duì)象。 |
pygame.transform.rotate() | 將圖片旋轉(zhuǎn)至指定的角度。 |
pygame.transform.rotozoom() | 以角度旋轉(zhuǎn)圖像,同時(shí)將圖像縮小或放大至指定的倍數(shù)。 |
下面看一組簡(jiǎn)單的演示示例:
import pygame
#引入pygame中所有常量,比如 QUIT
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,250))
pygame.display.set_caption('W3Cschool')
#加載一張圖片(455*191)
image_surface = pygame.image.load("C:/Users/Administrator/Desktop/w3cschool.png").convert()
image_new = pygame.transform.scale(image_surface,(300,300))
# 查看新生成的圖片的對(duì)象類型
#print(type(image_new))
# 對(duì)新生成的圖像進(jìn)行旋轉(zhuǎn)至45度
image_1 =pygame.transform.rotate(image_new,45)
# 使用rotozoom() 旋轉(zhuǎn) 0 度,將圖像縮小0.5倍
image_2 = pygame.transform.rotozoom(image_1,0,0.5)
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
# 將最后生成的image_2添加到顯示屏幕上
screen.blit(image_2,(0,0))
pygame.display.update()
程序的運(yùn)行結(jié)果如下:
圖1:程序運(yùn)行結(jié)果
更多建議: