如何在 Pygame 中将具有一定透明度的 PNG 闪烁到表面上? [英] How do I blit a PNG with some transparency onto a surface in Pygame?

查看:32
本文介绍了如何在 Pygame 中将具有一定透明度的 PNG 闪烁到表面上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 PNG 图像 blit 到表面上,但图像的透明部分由于某种原因变成黑色,这是简单的代码:

I'm trying to blit a PNG image onto a surface, but the transparent part of the image turns black for some reason, here's the simple code:

screen = pygame.display.set_mode((800, 600), pygame.DOUBLEBUF, 32)

world = pygame.Surface((800, 600), pygame.SRCALPHA, 32)
treeImage = pygame.image.load("tree.png")

world.blit(treeImage, (0,0), (0,0,64,64))
screen.blit(world, pygame.rect.Rect(0,0, 800, 600))

我该怎么做才能解决问题?图像具有 alpha 透明度,我已在 PhotoShop 中打开它,背景变为透明,而不是黑色或白色或任何其他颜色.

What do I have to do to solve the problem? The image has alpha transparency, I've opened it up in PhotoShop and the background turns transparent, not black or white or any other color.

感谢您的支持:)

推荐答案

http://www.pygame.org/docs/ref/image.html 推荐:

对于 alpha 透明度,就像在 .png 图像中一样,在加载后使用 convert_alpha() 方法,以便图像具有每个像素的透明度.

For alpha transparency, like in .png images use the convert_alpha() method after loading so that the image has per pixel transparency.

这篇关于如何在 Pygame 中将具有一定透明度的 PNG 闪烁到表面上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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