如何在Pygame中删除图像

如何在Pygame中删除图像

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

问题描述

我正在开发游戏,但是在处理图像时遇到一些问题.我已经加载了一些图像.加载它们并使用screen.blit()如下所示:

I'm working on a game and I have some problems working with images.I have loaded a few images . loading them and using screen.blit() was okay like below:

img1 = pygame.image.load("leaf.png")
img1 = pygame.transform.scale(img1, (25,25))
leaf = img1.get_rect()
leaf.x = random.randint(0, 570)
leaf.y =  random.randint(0, 570)

但是我不知道如何在这样的if语句中删除它们:

but I don't know how to remove them in an if statement like this for example:

if count == 1:
...

尽管我可能没有办法,但我应该在图像上绘制一个矩形以使其消失.另外,我不知道如何使用screen.fill()而不希望其他图像消失.还有其他办法吗?

and I though maybe there is no way and I should draw a rectangle on the image to disappear it. Also I don't know how to use screen.fill() while I don't want the other images to get disappeared. Is there any other way?

推荐答案

您可以填充单个图像,因为它们是pygame曲面.

You can fill individual images, since they are pygame Surfaces.

首先,我要做的是在定义叶子的x/y之后将类似的内容放进去:

First, what I would do is I would put something like this after defining the leaf's x/y:

leaf.image = img1

然后,我将创建一个名为transparent的颜色变量:

Then, I would create a color variable called transparent:

transparent = (0, 0, 0, 0)

您可能知道,前3个数字代表RGB颜色值.最后一个数字是颜色的alpha(透明度)值. 0是完全不可见的.

The first 3 numbers, as you might know, represent RGB color values. The last number is the alpha (transparency) value of a color. 0 is completely invisible.

最后,我将添加以下代码以使叶子完全透明:

Finally, I would add this code to make the leaf completely transparent:

leaf.image.fill(transparent)

这将使叶子透明,而不会使窗口中的所有其他图像消失.希望这会有所帮助!

This makes the leaf transparent without making every other image in your window disappear. Hope this helped!

这篇关于如何在Pygame中删除图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:14