【发布时间】:2021-01-07 05:53:47
【问题描述】:
在使用pygame 测试我新发现的python 技能时,我遇到了一个有趣的问题。
当我将行星 .png 图像加载到屏幕上时,行星显示为黑色光晕。
这真的很奇怪,因为我加载到游戏中的其他图像(.png 和 .bmp)都没有问题。
我已使用 for 循环导入图像并使用 convert_alpha() 转换它们。
当我不转换图像时,黑色光晕仍然存在。
我还尝试在 for 循环的每次迭代中设置颜色键,但无济于事:
class Pl_Images(pygame.sprite.Sprite):
def __init__(self):
self.pl_images = []
for num in range(1,5):
img = pygame.image.load(f"Images/planets/static/planet{num}.png")
img_rect = img.get_rect()
img = pygame.transform.scale(img, (int(img_rect.width//10), int(img_rect.height//10)))
self.pl_images.append(img)
当行星对象由行星生成器类“PlanetSurface”创建并放置到精灵组中时,其中一个预加载的图像随后被行星对象使用(在需要时)。
import pygame
from planet import Planet
import random
from pygame.sprite import Sprite
from pl_images import Pl_Images
class PlanetSurface(Sprite):
def __init__(self, settings):
"""
Creates a planet surface and planets that travel across the screen
"""
super(PlanetSurface, self). __init__()
self.settings = settings
self.surface = pygame.surface.Surface(settings.screen.get_size())
self.surface.set_colorkey([0,0,0])
self.images = Pl_Images()
self.planets = pygame.sprite.Group()
self.pl_timer = random.randrange(420, 720) # 7 seconds and 12 seconds
self.max_planets = 3
self.current_num_planets = 0
def update(self):
""" update the planets """
self.planets.update()
for planet in self.planets.copy():
if planet.rect.y > self.settings.screen_height:
self.planets.remove(planet)
self.current_num_planets -= 1
if self.pl_timer == 0:
if self.current_num_planets >= self.max_planets:
pass
else:
self.new_planet = Planet(self.settings, self.images)
self.rect = self.new_planet.rect
self.planets.add(self.new_planet)
self.pl_timer = random.randrange(2100, 3600)
# Redraw planet for a smooth effect
self.surface.fill((0,0,0))
self.planets.draw(self.surface)
self.pl_timer -= 1
这是 Planet 类:
class Planet(Sprite):
def __init__(self, settings, images):
super(Planet, self). __init__()
self.images = images
self.planets = self.images.pl_images
self.settings = settings
self.num_planets = len(self.planets)
self.image_index = random.randrange(self.num_planets - 1)
self.image = self.planets[self.image_index]
self.rect = self.image.get_rect()
self.rect.x = random.randrange(
0,
self.settings.screen_width - self.rect.width
)
self.rect.y = -self.rect.height
self.vel_x = 0
self.vel_y = random.randrange(1, 2)
def update(self):
self.rect.x += self.vel_x
self.rect.y += self.vel_y
当调用更新方法时,图像会被绘制到行星表面。
我正在绘制图像的表面是一个使用透明colorkey 的简单表面:
这个自定义表面是由主函数在主背景之上但在游戏表面之下绘制的:
self.screen.blit(self.background.bgimage, (self.background.bgX2,
self.background.bgY2))
self.screen.blit(self.planet_surface.surface, (0,0))
self.screen.blit(self.game_surface.surface, (0,0))
我对这个问题感到很困惑,因为行星是具有透明背景的 .png 图像。
有谁知道这个黑色光环可能来自哪里?
是我做错了什么还是pygame 中的故障?
我上传了一个样本星球进行分析。 planet45.png
提前致谢!
【问题讨论】:
-
目前的情况是,这是一个透明度问题。 PyGame 只是将完全透明的区域渲染为透明的,而将其他所有区域渲染为不透明的。至于为什么,不幸的是,我不知道 PyGame 中的透明度是如何工作的,所以其他人可能需要在这方面提供帮助。
-
我发现this question 似乎相关,这有帮助吗?
-
这是有道理的。感谢您的反馈!希望有人有解决方案。
-
为什么要设置色键
img.set_colorkey([0,0,0])。你有PNG。它们应该是自动透明的。至少为行星图像删除img.set_colorkey([0,0,0])。 -
如果您的其余图像都可以正常工作,而只有这一个不能正常工作,那么此图像显然有一些不同之处。已经有一段时间了,但我过去经常使用图像处理。必须操纵图像的 Alpha 通道和/或合成模式才能获得正确的效果是很常见的,而且有时图像只是简单的“错误”并且您无能为力。如果我没记错的话,“错误”通常意味着在不应该的情况下预先假定了背景颜色。我怀疑这里...黑色背景被错误地假定了。