问题描述
我无法使用 Pygame 正确加载和 blit PNG 图像.代码实际上是有效的,但它显示了一些奇怪和黑色的东西"在我的精灵周围:
I'm having troubles to correctly load and blit PNG image with Pygame.Code is actually working, but it display some strange and black "things" around my sprite :
要加载tileset,我这样做:
To load tileset, I do:
def TilesRessourceFile(self, filename=None, tileSize=None, tiles=None):
logging.info("Loading ressources from %s", filename)
self._tileSize = tileSize
res = pygame.image.load(filename)
res.convert_alpha()
for tile in tiles:
name, (x, y), alpha = tile.values()
surface = pygame.Surface((tileSize, tileSize))
surface.blit(res, (0, 0), area=(x * tileSize, y * tileSize, (x + 1) * tileSize, (y + 1) * tileSize))
surface.convert_alpha() # set_colorkey(alpha)
self._tiles.update({name: surface})
然后我像这样 blit 精灵
Then I blit the sprite like this
def _implGameObjectRender(self, screen):
# logging.info("Render map %s", self._mapping)
for i in range(len(self._mapping)):
for j in range(len(self._mapping[i])):
screen.blit(self._mapping[i][j], (j * 128, i * 128))
可能不多,但我自己找不到解决方案.我已经试过检查:
It's probably not much, but I don't find the solution by myself.I already tried to check :
- 如何使用 pygame 加载 PNG
- pygame 的透明度(convert 和 convert_alpha)
我正在使用这个tileset:https://www.gamedevmarket.net/asset/2d-hand-painted-town-tileset-6626/
I'm using this tileset : https://www.gamedevmarket.net/asset/2d-hand-painted-town-tileset-6626/
tileset 提供了一个 json 文件来加载 Tiled.也试过这个,它完美地工作,所以我想问题出在我这边
The tileset provide a json file to load with Tiled. Also tried this, and it perfectly works so I guess the problem is on my side
谢谢你帮助我!
Python 3.9.1Pygame 2.0.1 (SDL 2.0.14)
Python 3.9.1Pygame 2.0.1 (SDL 2.0.14)
推荐答案
convert_alpha()
不会转换表面本身的格式,而是使用提供每像素 alpha 格式的格式创建新表面.
convert_alpha()
does not convert the format of the surface itself, but creates a new surface with a format that provides a per pixel alpha format.
要么
surface = pygame.Surface((tileSize, tileSize))
surface = surface.convert_alpha()
或
surface = pygame.Surface((tileSize, tileSize)).convert_alpha()
有 3 种方法可以创建透明的表面:
颜色键指定被视为透明的颜色.例如,如果您有一张应该是透明的黑色背景图像,请设置一个黑色键:
The color key specifies the color that is treated as transparent. For example, if you have an image with a black background that should be transparent, set a black color key:
surface.set_colorkey((0, 0, 0))
您可以在创建新表面时启用其他功能.设置 SRCALPHA
标志以创建带有包含每像素 alpha 的图像格式.像素的初始值为(0, 0, 0, 0):
You can enable additional functions when creating a new surface. Set the SRCALPHA
flag to create a surface with an image format that includes a per-pixel alpha. The initial value of the pixels is (0, 0, 0, 0):
surface = pygame.Surface((tileSize, tileSize), pygame.SRCALPHA)
使用convert_alpha()
使用提供每像素 alpha 的图像格式创建 Surface 的副本.
但是,如果您创建一个新表面并使用 convert_alpha()
,则 alpha 通道最初设置为最大值.像素的初始值为 (0, 0, 0, 255).您需要先用透明颜色填充整个表面,然后才能在其上绘制任何内容:
However, if you create a new surface and use convert_alpha()
, the alpha channels are initially set to maximum. The initial value of the pixels is (0, 0, 0, 255). You need to fill the entire surface with a transparent color before you can draw anything on it:
surface = pygame.Surface((tileSize, tileSize))
surface = surface.convert_alpha()
surface.fill((0, 0, 0, 0))
这篇关于使用图像透明度使“东西"看起来是黑色的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!