Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

python - Transparent Sprites in Pygame

I'm working on some Python code that uses Pygame, trying to display a small sprite (a ball) on top of a background. I have that part working, but I'm trying to get the background of the ball sprite to be transparent so it doesn't show up as a "ball within a black square" sprite, but instead shows up with the black pixels not being blitted to the display surface.

Here is my code:

# 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 transparent

# The main loop
while True:

    # 1 - Process all input events
    for event in pygame.event.get():

        # Make sure to exit if the user clicks the X box
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # 2 - Blit images to screen (main display window)
    screen.blit(background, (0,0))
    x,y = pygame.mouse.get_pos()
    x = x - ball.get_width()/2
    y = y - ball.get_height()/2
    screen.blit(ball, (x,y))

    # 3 - Update the main screen (redraw)
    pygame.display.update()

I must be making an obvious mistake, but I can't figure it out. Calling ball.set_colorkey(-1, RLEACCEL) should pick up the color of the upper-left corner of the ball sprite (which happens to be black) and use that as the pixel color "not to blit". Am I missing a step?

Thanks for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's per-pixel alpha, colorkey alpha, and per-surface alpha. You're asking for colorkey.

When you call convert_alpha() it creates a new surface for per-pixel alpha.

And from set_colorkey

The colorkey will be ignored if the Surface is formatted to use per pixel alpha values.

So: Load image with .convert() Since you want to use a color key. Then call set_colorkey.

Also, I saw nothing in the docs about passing "-1" as first argument to set_colorkey.

That's probably from a tutorial, which has a load_image function to grab the topleft pixel's color value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...