如何使 pygame 精灵的背景透明

How to make the background of a pygame sprite 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()

您的图片必须是 。例如打开预览,使用alpha工具去除背景,从而增加透明度。

之前:

之后:

寻找 here 使图片透明的工具。

加载png图片时,需要进行转换:

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

这应该摆脱应该是透明 alpha 的黑框。