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

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

问题描述

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

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天全站免登陆