如何使用 Pygame 裁剪图像? [英] How can I crop an image with Pygame?

查看:75
本文介绍了如何使用 Pygame 裁剪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 pygame 并且想要一个具有三种状态的按钮的图形:正常、悬停和按下.我有一张这样的图片...

I am learning pygame and want a graphic for a button with the three states: normal, hover, and pressed. I have an image like this one ...

... 我想使用它的一部分来获得一个新的 Surface.

... and I want to get a new Surface using a portion of it.

我正在使用此代码加载图像:

I'm loading the image with this code:

 buttonStates = pygame.image.load(os.path.join('image','button.png'))

如何仅使用该图形的一部分来制作新表面?

How can I make a new surface using just a portion of that graphic?

推荐答案

cropped = pygame.Surface((80, 80))
cropped.blit(buttonStates, (0, 0), (30, 30, 80, 80))

一个表面上的 blit 方法将另一个表面粘贴"到它上面.blit 的第一个参数是源表面.第二个是要粘贴到的位置(在本例中为左上角).第三个(可选)参数是要粘贴的源图像的区域 - 在本例中为距顶部 30 像素和距左侧 30 像素的 80x80 正方形.

The blit method on a surface 'pastes' another surface on to it. The first argument to blit is the source surface. The second is the location to paste to (in this case, the top left corner). The third (optional) argument is the area of the source image to paste from -- in this case an 80x80 square 30px from the top and 30px from the left.

这篇关于如何使用 Pygame 裁剪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆