[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Blitting image with alpha channel on a transparent background



On Fri, Apr 23, 2010 at 2:59 AM, Greg Ewing <greg.ewing@xxxxxxxxxxxxxxxx> wrote:
> Lee Buckingham wrote:
>>
>> Instead of using set_alpha(), try using a fill, like:
>>
>>  backgroundsurface.fill((0,0,0,100))
>
> Note that you will also have to create the background surface with
> the SRCALPHA flag, so that it will have a per-pixel alpha channel.

Thank you guys, it worked :-)
I've combined the comments from Lee and Greg and came up with this
little piece of test code for a semi transparent button like image which
I just post here in case anyone else needs such a thing.

import pygame
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((400,400))
screen.fill([70, 70, 70]) # blank the screen.

# button background
back = pygame.Surface((200,200),SRCALPHA)
back.fill((0,0,0,50))
pygame.draw.rect(back,(0,0,0),back.get_rect(),2)
# random image
image = pygame.image.load('test1.png').convert_alpha()
back.blit(image,(10,10))

# draw some lines to get stuff in the background
for y in range(0,300,10):
    pygame.draw.line(screen,(0,0,0),(0,y),(300,y))

screen.blit(back,(10,10))
pygame.display.update()

while True:
    pygame.time.wait(200)


Thanks,
Stas

-- 
Free-source educational programs for schools
http://www.schoolsplay.org  and http://wiki.laptop.org/go/Schoolsplay
http://gvr.sf.net and http://wiki.laptop.org/go/Guido_van_Robot