如何解决Pygame:如何消除我的星球图像上出现的黑色光晕?
在使用 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”创建并放入精灵组时,Planet 对象会使用其中一个预加载的图像(在需要时)。
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])
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))
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
然后在调用 update 方法时将图像绘制到行星表面。
我绘制图像的表面是一个使用透明 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))
我对这个问题感到非常困惑,因为行星是具有透明背景的 .png 图像。
有谁知道这个黑色光环可能来自哪里?
是我做错了什么还是 pygame
有问题?
我上传了一个样本星球进行分析。 planet45.png
提前致谢!
解决方法
我检查了您在问题中提供的示例行星。 - planet45.png
整个图像的 alpha 通道为 255。图像具有不透明的白色背景和围绕行星的淡蓝色光晕。您可以设置白色 (set_colorkey()
) 使背景透明,但光环将保持不透明。问题不在于您的应用程序,而在于图像资源。您需要使用另一个提供每像素 alpha 的行星图像。
另见 How can I make an Image with a transparent Backround in Pygame? 或 How to convert the background color of image to match the color of Pygame window? 分别为 How do I blit a PNG with some transparency onto a surface in Pygame?。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。