python - Pygame:如何消除行星图像上出现的黑色光晕?

标签 python pygame

在使用 pygame 测试我新发现的 Python 技能时,我遇到了一个有趣的问题。 当我将行星 .png 图像加载到屏幕上时,行星会显示出黑色光环。

planet_with_halo

这真的很奇怪,因为我加载到游戏中的其他图像(.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”创建并放置到 Sprite 组中时,行星对象将使用(需要时)其中一张预加载的图像。

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

提前致谢!

最佳答案

我检查了您在问题中提供的示例行星。 -planet45.png

整个图像的 Alpha channel 为 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? .

关于python - Pygame:如何消除行星图像上出现的黑色光晕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65587989/

相关文章:

Python 遍历文件系统。奇怪的问题

python - 使用 Marshmallow 序列化 SQLAlchemy

python - 通过 termios.TIOCSTI 注入(inject) unicode 字符

python - 为什么在 Visual Studio Code 上运行 Python 3 脚本时会出现 SyntaxError?

python - 为什么我的变量在列表中的行为不符合我的预期?

python - 打印乐谱 - Pygame

python - 如何迭代两个字典并将它们保存在python列表中

python - 在pygame中的数组支持网格中交换颜色的问题

python - 尝试在一定时间后延迟生成敌人的特定功能

python - Sprite 没有显示在 pygame 窗口上