The Python Game Book

code games. learn Python.

User Tools

Site Tools






This small demo shows the difference between a picture with transparent background (horst.png) and a picture without any transparency at all (horst.jpg). Both picture's alpha-value can be changed with the Page-Up or Page-Down Key or with the mouse wheel. The demo requires pygame ver 1.8.1 or better to run correctly.


screenshot of colordemo


zip file including source code and graphic files

#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" colordemo
    a simple demo about alpha values.
    Author: Horst JENS, 9/2008
    mousewheel code by Lexi
    per-pixel-alpha code by Claudio Canepa <>

    license: public domain"""
import pygame
import os
if pygame.ver < "1.8.1":
    print "your pygame version is too old"
    raise SystemExit()
screen=pygame.display.set_mode((640, 400))
# background
background = pygame.Surface(screen.get_size()).convert()
background.fill((255, 255, 255))     #fill the background white
bike = pygame.image.load("Motorrad.jpg").convert()
background.blit(bike, (50, 25)) # blit bike in the background, but do not blit background yet
#horstbild1.png does not change it's alpha value, no matter what alpha-value you set
#because horstbild1.png already contains transparent background
#horstbild1 needs per-pixel-alpha set (see the function get_alphaed, requires
#pygame version 1.8.1 or higher
horstbild1 = pygame.image.load("horst.png").convert_alpha()
#horstbild2.jpg is a normal jpg without transparent background,
#we can set and modify the alpha value for the whole picture
horstbild2 = pygame.image.load("horst.jpg").convert()

def get_alphaed( surf, alpha=128):
    """returns a copy of a surface object with user-defined alpha-value (0-255)"""
    tmp = pygame.Surface( surf.get_size(), pygame.SRCALPHA, 32)
    tmp.fill( (alpha,alpha,alpha,alpha) )
    tmp.blit(surf, (0,0), surf.get_rect(), pygame.BLEND_RGBA_MULT)
    return tmp

def main():
    clock = pygame.time.Clock()
    mainloop = True
    alpha = 128
    while mainloop:
        screen.blit(background, (0,0)) # draw background every frame
        pygame.display.set_caption("use mousewheel or press PgUP or PgDown. Current alpha-value = % i " % alpha)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                mainloop = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    mainloop = False
            elif event.type==pygame.MOUSEBUTTONDOWN:
                if event.button==4:
                    alpha += 1
                elif event.button==5:
                    alpha -=1
        pressed_keys = pygame.key.get_pressed()
        if pressed_keys[pygame.K_PAGEUP]:
            alpha += 1
        elif pressed_keys[pygame.K_PAGEDOWN]:
            alpha -= 1
        if alpha < 0:
            alpha = 0
        if alpha > 255:
            alpha = 255
        screen.blit(horstbild2, (5,50)) # this line always worked
        #horstbild1.set_alpha(alpha) # this line is useless and do not work
        #screen.blit(horstbild1, (300,200)) # this line also does not work
        tmp = get_alphaed(horstbild1, alpha)
        screen.blit(tmp, (300,200))
        pygame.display.flip()       # flip the screen 30 times a second
if __name__ == "__main__":

things to know


/ /

en/secret/people/jens_horst/colordemo.txt · Last modified: 2020/05/03 21:35 by horst