While testing my new-found python skills using pygame
, I ran into an interesting problem.
When I load planet .png images onto the screen, the planets show up with a black halo.
This is really strange since the other images(.png and .bmp) that I have loaded into the game work without a problem.
I have imported the images by using a for loop and converting them with convert_alpha()
.
When I do not convert the images, the black halo is still there.
I also tried setting the color key with each iteration of the for loop to no avail:
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)
One of the pre-loaded images are then used (when needed) by a Planet object when it is created by the planet generator class "PlanetSurface" and is placed into a sprite group.
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
Here is the Planet class:
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
The image is then drawn to the planet's surface when the update method is called.
The surface that I am drawing the images to is a simple surface that uses a transparent colorkey
:
This custom surface is being drawn by the main function on top of the main background but below the gaming surface:
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))
I am quite baffled by this problem since the planets are .png images with transparent backgrounds.
Does anyone have any idea where this black halo might be coming from?
Is there something that I am doing wrong or is this a glitch in pygame
?
I have uploaded a sample planet for analysis. planet45.png
Thanks in advance!