透明 spritesheet 具有黑色背景 [英] Transparent spritesheet has black background

查看:100
本文介绍了透明 spritesheet 具有黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 和 Pygame 开发一款游戏.我为其中一个敌人创建了一个精灵表,并让它运行了我的代码.问题是图像看起来有黑色背景,即使它是透明图像.它的代码是这样的:

I'm working on a game using Python and Pygame. I created a sprite sheet for one of the enemies and got my code for it working. The problem is that the image appears to have a black background even though it is a transparent image. The code for it is this:

    enemySheet = pygame.image.load("resources/Alien.png").convert_alpha()
    transColor = (255,255,255)
    cells = []
    for n in range(3):
        width, height=(36,32)
        rect = pygame.Rect(n * width, 0, width, height)
        image = pygame.Surface(rect.size).convert_alpha()
        image.blit(enemySheet, (0,0), rect)
        cells.append(image)

    enemyImg = cells[0]
    enemyImg.set_colorkey(transColor)
    enemy = enemyImg.get_rect()
    enemy.center = (216,216)

我已经尝试了一些东西,但没有任何效果.欢迎提出任何想法.

I have already tried a few things but nothing has worked. Any ideas are welcome.

推荐答案

默认情况下,新表面填充为黑色.如果你想让它透明,你可以向 transColor(alpha 值)添加第四个数字,然后填充 image

New surfaces are filled with black by default. If you want to make it transparent you can either add a fourth number to the transColor (the alpha value) and then fill the image,

transColor = (255,255,255,0)

    # In the for loop.
    image = pygame.Surface(rect.size).convert_alpha()
    image.fill(transColor)

或者只是传递 pygame.SRCALPHA 标志:

or just pass the pygame.SRCALPHA flag:

image = pygame.Surface(rect.size, pygame.SRCALPHA)

更好的解决方案是使用 pygame.Surface.subsurface 切割板材:

A nicer solution would be to use pygame.Surface.subsurface to cut the sheet:

for n in range(3):
    width, height = (36, 32)
    rect = pygame.Rect(n * width, 0, width, height)
    image = enemySheet.subsurface(rect)
    cells.append(image)

这篇关于透明 spritesheet 具有黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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