Hobbs2000
Hobbs2000

Reputation: 311

How to make the background of a pygame sprite transparent

This is supposed to just load a sprite onto the screen, but the background of the image is this black box, and i don't want to see that. How do I make the background of the image transparent?

from Tkinter import *
import pygame
from livewires import games

#Creating screen window
games.init(screen_width = 700, screen_height = 650, fps = 50)

#creating background image for the screen
screen_background = games.load_image('resized_stars background.png', transparent = False)
games.screen.background = screen_background

#Creating a sprite
spaceship_image = games.load_image('pixel_spaceship.png')
spaceship = games.Sprite(image = spaceship_image, x=350, y=235)
games.screen.add(spaceship)

#main loop at the end of the program just as in tkinter
games.screen.mainloop()

Upvotes: 1

Views: 3018

Answers (2)

Fabitastic
Fabitastic

Reputation: 11

When loading the png image, you have to convert it:

image = pygame.image.load("yourImage.png").convert_alpha()

This should get rid of that black box that should be the transparent alpha.

Upvotes: 0

A.J. Uppal
A.J. Uppal

Reputation: 19264

Your image needs to be a . Open Preview, for example, and use the alpha tool to remove the background, thus adding transparency.

Before :

enter image description here

After :

enter image description here

Look here for a tool on making your picture transparent.

Upvotes: 1

Related Questions