[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Pixel alpha blends tutorial
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Pixel alpha blends tutorial
- From: James Hofmann <jwhinfinity@xxxxxxxxx>
- Date: Sat, 25 Mar 2006 23:10:37 -0800 (PST)
- Delivered-to: archiver@seul.org
- Delivered-to: pygame-users-outgoing@seul.org
- Delivered-to: pygame-users@seul.org
- Delivery-date: Sun, 26 Mar 2006 02:10:41 -0500
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=Ku0Ty8asVpWsvxXIRn00Q+xtGtQftD+MnVqh4218ymIEFGsP8COJpCub/rebTx1obHfwdSWYuXyKcy/ZVN6TzVFpegEljPJPNCgwyuQLV45bnkKqTNq4RG4xXdj6x9C4td3+RdnQOikTnpQUicC/u4cc8qb1/s+INJ25STatzoA= ;
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
# A pixel-alpha sprite tutorial for pygame
# by James Hofmann 2006
# public-domain distributable
# This tutorial was made for myself to see how I could
use alpha-transparent
# sprites for blending, e.g. between tiles on a map. I
don't see similar tuts
# focused on this use, so I cleaned it up a little and
added comments.
# There are probably many ways to improve on this.
# To use this tutorial:
# 1. Make sure pygame and Numeric are installed, and
that pygame.Image
# supports the "extended" formats. (if not, PIL can
also be used to load images)
# 2. Create an image called "edge.tga" with GIMP or
Photoshop to these specs:
# (I use GIMP)
# a. Format: Resolution 256x16, 32-bit color.
# b. Colored neither black nor white.
# c. Some part of the image should use pixel
alpha(this is easily done with
# GIMP's eraser tool)
# d. the .tga should be saved without compression.
import pygame
import Numeric
from pygame.locals import *
pygame.init()
pygame.display.set_mode((320,200))
### Make the surfaces
screen = pygame.display.get_surface()
gradient = pygame.Surface((320,200)) # gradient is a
little background to blit over
for y in range(200):
pygame.draw.line(gradient, (y,0,0), (0,y),(320,y))
orig = pygame.image.load("edge.tga")
alphaarray = pygame.surfarray.array_alpha(orig) # try
printing alphaarray
newcol = pygame.Surface((256,16), SRCALPHA, 32) # you
must force the surface to 32bit
newcol.fill((255,255,255)) # we are giving the surface
"new color" for the demo
screen.blit(orig, (0,0))
screen.blit(newcol, (0,16))
newalpha = pygame.surfarray.pixels_alpha(newcol) #
pixels_alpha gives us a ref
for n in range(len(newalpha)):
newalpha[n] = alphaarray[n] # and so we simply
replace each referenced pixel
del newalpha # and then clean up (this unlocks the
surface)
# main issue with this implementation: it expects
same-dimension surfaces.
# best workaround is to always work with subsurfs the
same size as the alpha.
screen.blit(newcol, (0,32))
pygame.display.update()
### Two demos, when running them press spacebar to
continue:
# demo 1 shows first the original edge.tga image,
followed by the white-filled
# "newcol" surface. Finally, it makes newcol use the
alpha of edge.tga using
# surfarray.
#
# demo 2 is a performance-sanity test: it recreates
newcol many times over
# a gradient background. Simply blitting newcol is
very fast; it is the
# blending that is the bottleneck.
cont = 1
clock = pygame.time.Clock()
while cont>0:
for e in pygame.event.get():
if e.type==KEYDOWN:
if e.key==K_SPACE:
cont = 0
clock.tick(30)
cont = 1
while cont>0:
cont+=1
for e in pygame.event.get():
if e.type==KEYDOWN:
if e.key==K_SPACE:
cont = 0
screen.blit(gradient, (0,0))
for y in range(20):
newcol.fill((0,int((y/20.00)*255),0))
newalpha =
pygame.surfarray.pixels_alpha(newcol)
for n in range(len(newalpha)):
newalpha[n] = alphaarray[n]
del newalpha
screen.blit(newcol, (cont,y*16))
pygame.display.update()
if cont>320:
cont=1
clock.tick(120)
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com