我正在编写一些使用 Pygame 的 Python 代码,试图在背景顶部显示一个小精灵(一个球)。我有那部分工作,但我试图让球精灵的背景变得透明,这样它就不会显示为“黑色正方形内的球”精灵,而是显示为黑色像素不存在位块传送到显示表面。
这是我的代码:
# For sys.exit()
import sys
# Pygame imports
import pygame
from pygame.locals import *
# Initialize all the Pygame Modules
pygame.init()
# Build a screen (640 x 480, 32-bit color)
screen = pygame.display.set_mode((640,480))
# Create and Convert image files
# Use JPG files for lots of color, and use conver()
# Use PNG files for transparency, and use convert_alpha()
background = pygame.image.load("bg.jpg").convert()
ball = pygame.image.load("ball.png").convert_alpha()
ball.set_colorkey(-1, RLEACCEL) # Use the upper-left pixel color as …Run Code Online (Sandbox Code Playgroud)