【Pygame 学习笔记】4.Transform 图片的操作

上一篇文章,我们讲解了如何导入图片并绘制到屏幕中,这篇文章,我们趁热打铁,继续深入学习如何用pygame的transform对图片进行各种操作。

这篇文章需要的资源如下:

0.png

 

Sky.jpg


给出Pygame基本框架代码,这篇文章将在这部分代码的基础上做更多操作

import pygame
from pygame.locals import *
import sys

path="resources/"

class Game:
    def __init__(self):
        pygame.init()
        self.W,self.H=800,800
        self.screen=pygame.display.set_mode((self.W,self.H))
        pygame.display.set_caption("Window")

    def listen(self):
        for event in pygame.event.get():
            if event.type==QUIT:
                sys.exit()

    def draw(self):
        self.screen.fill((255,255,255))

    def run(self):
        while True:
            self.listen()
            self.draw()
            pygame.display.update()

if __name__ == '__main__':
    game=Game()
    game.run()

首先,我们来学习如何对图片进行放大缩小

制作一个游戏,我们需要的素材可能与我们需要的大小不同,最常见的就是背景图片不符合窗口大小,这个并不难解决,我们只需要对图片进行缩放即可,我们要将刚刚提供的Sky.jpg绘制到屏幕,但是大小不同,无法填满,现在就需要用到pygame.transform中的scale啦

pygame.transform.scale(surface,size)

surface指surface对象,也就是要进行缩放操作的图片

size指缩放后的大小,在框架中,我们的窗口的宽高是self.W和self.H,我们可以这样写:

首先在初始化函数中引入该图片

self.bg=pygame.image.load(path+"Sky.jpg")

然后用scale方法缩放至窗口大小,注意,scale方法不会在原surface对象上操作,它会返回一个新的surface对象,所以我们要重新定义self.bg

self.bg=pygame.transform.scale(self.bg,(self.W,self.H))

然后在draw中,画出该图片,因为它的左上角就是屏幕的左上角,所以起始坐标为0,0

self.screen.blit(self.bg,(0,0))

 


还有一个方法和scale很像,叫smoothscale,只是smoothscale可以平滑放大图片,两者没有太大区别,这里不做讲解


transform中有一个可以放大到原来的2倍,scale2x

pygame.transform.scale2x(surface)

我们只需要传入surface即可

实际效果和用scale和smoothscale差不多


接下来,我们来学习如何旋转图片

我们先导入0.png,然后放大并绘制到屏幕中心,在初始化函数中添加如下代码

self.bird=pygame.image.load(path+"0.png")
self.bird=pygame.transform.scale(self.bird,(200,200))

在draw中添加如下代码

rect=self.bird.get_rect()
rect.center=self.W/2,self.H/2
self.screen.blit(self.bird,rect)

 

 然后我们用rotate函数将图片旋转,返回新的图像

pygame.transform.rotate(surface,angle)

angle指旋转角度

我们尝试在初始化函数中这样写

self.bird=pygame.transform.rotate(self.bird,45)

 从结果不难发现,图片原来的角度是0°,角度往逆时针方向递增


有一个方法叫rotozoom

pygame.transform.rotozoom(surface,angle,scale)

从字面和参数中我们不难理解,rotozoom用于同时放大和旋转图像,这里不做详解


还有一个常用的方法,叫做flip,用于翻转图像

pygame.transform.flip(surface,flip_x,flip_y)

flip_x和flip_y都是bool类型的变量,表示是否沿着x轴和y轴翻转,True则翻转

沿着x轴翻转,就是水平翻转,沿着y轴翻转则是上下翻转

我们试着就该刚刚代码中对0.png进行旋转的操作,将其改为

self.bird=pygame.transform.flip(self.bird,True,False)

可看到,鸟儿的头朝向了左边

再试着将刚刚写入的代码改为

self.bird=pygame.transform.flip(self.bird,True,True)

可看到,鸟儿上下颠倒了


这篇文章,我们学会了pygame.transform下的一些常用方法

制作不易,喜欢我的文章的话别忘了多多点赞收藏关注哦

谢谢支持~

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pygame.transform.flip() is a method in the Pygame library that is used to flip an image along its axis. This method can be used to create mirror images or to change the orientation of an image. The method takes three arguments: the image to be flipped, a boolean value indicating whether to flip the image horizontally, and a boolean value indicating whether to flip the image vertically. Syntax: ``` pygame.transform.flip(surface, xbool, ybool) ``` - surface: The image to be flipped. - xbool: A boolean value indicating whether to flip the image horizontally (True or False). - ybool: A boolean value indicating whether to flip the image vertically (True or False). Example: ``` import pygame pygame.init() # Set up the display display_width = 640 display_height = 480 game_display = pygame.display.set_mode((display_width, display_height)) # Load the image my_image = pygame.image.load("image.png") # Flip the image horizontally flipped_image = pygame.transform.flip(my_image, True, False) # Display the original and flipped images game_display.blit(my_image, (0, 0)) game_display.blit(flipped_image, (display_width/2, 0)) pygame.display.update() # Quit the game pygame.quit() ``` In the above example, we first load an image using the `pygame.image.load()` method. We then use the `pygame.transform.flip()` method to create a flipped version of the image by passing in `True` as the first argument to indicate that we want to flip the image horizontally. We then display both the original and flipped images using the `blit()` method. Finally, we update the display and quit the game.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值