English 中文(简体)
Overlapping partially transparent shapes
原标题:

If I draw any shape onto a surface (with SRCALPHA flag on) in a partially transparent colour it completely replaces what was underneath it, instead of overlapping like you would expect in image editors.

How can I make the shapes overlap properly?

最佳回答

As noted, the draw functions in PyGame don t actually do blending the way you might expect. From the PyGame documentation:

Most of the arguments accept a color argument that is an RGB triplet. These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently.

What you ll probably want to do is essentially what Alex Martelli said--create an intermediate software surface with an alpha channel, draw your shape onto that surface, then blit with blending onto the final surface. If you ve used PyGame s font rendering, notice that it works roughly the same, except that Font.render creates the temporary surface for you.

I m a bit rusty with Python and PyGame these days, but here s a quick example that hopefully doesn t have too many mistakes:

# returns a surface with the circle drawn onto it
def render_transparent_circle(color, radius, width=0):
    size = radius * 2
    temp_surf = pygame.Surface((size, size), SRCALPHA)
    temp_surf.fill(Color(0, 0, 0, 0))
    pygame.draw.circle(temp_surf, color, (radius, radius), radius, width)
    return temp_surf

# draw a half-opaque blue circle of radius 30 to the display at point (50, 100)
def test():
    transp_blue = Color(0, 0, 255, 128)
    screen = pygame.display.get_surface()
    circle = render_transparent_circle(transp_blue, 30)
    screen.blit(circle, (50, 100))

p.s. -- If you re going to display the same shape multiple times, you may want to consider caching the intermediate surface (but don t forget to profile to find out if it helps).

问题回答

You could use blit with appropriate BLEND_* values (keeping the shapes as separate surfaces, for integrity and editing, and only blitting them for display purposes).

Alpha on its own is just an extra value that can serve a purpose. It just so happens that the purpose is typically (but not always) transparency (other uses include reflectance, specular highlighting and even an approach for faking HDR).

You need to specify that you want it to blend (and the equation it should use to do it). A simple Google search for "pygame blend mode" shows a few good resources.

As Alex has pointed out, blit takes a Blending mode as a parameter. This StackOverflow question provides more information.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签