ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:resources:people:jens_horst:colordemo

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:resources:people:jens_horst:colordemo [2014/01/09 11:07] (current)
Line 1: Line 1:
 +====== colordemo ======
  
 +
 +===== Description =====
 +
 +
 +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 =====
 +{{:​en:​part2:​unsorted:​colordemo1.jpg|screenshot of colordemo}}
 +
 +
 +====== Download ======
 +
 +{{:​en:​part2:​unsorted:​colordemo.zip|zip file including source code and graphic files}}
 +
 +<code python colordemo.py>​
 +#​!/​usr/​bin/​env python
 +# -*- coding: utf-8 -*-
 +"""​ colordemo
 +    a simple demo about alpha values.
 +    Author: Horst JENS, 9/2008
 +    email: horst.jens@spielend-programmieren.at
 +    mousewheel code by Lexi
 +    per-pixel-alpha code by Claudio Canepa <​ccanepacc@gmail.com>​
 +
 +    license: public domain"""​
 +import pygame
 +import os
 +if pygame.ver < "​1.8.1":​
 +    print "your pygame version is too old"
 +    raise SystemExit()
 +pygame.init()
 +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:
 +        clock.tick(30)
 +        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
 +        ​
 +        ​
 +        horstbild2.set_alpha(alpha)
 +        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__":​
 +    main()
 +
 +</​code>​
 +
 +
 +====== things to know ======
 +stuff 
 +
 +
 +
 +----
 +<​html>​
 +<script src=http://​www.spielend-programmieren.at/​config/​lang/​de/​counter/​cnt.php?​cid=ipBAGK></​script>​ / 
 +<script src=http://​www.spielend-programmieren.at/​config/​lang/​de/​counter/​cnt.php?​cid=mc9mTY></​script>​ /
 +<script src=http://​www.spielend-programmieren.at/​config/​lang/​de/​counter/​cnt.php?​cid=jyKCiQ></​script>​
 +</​html>​
 +
 +~~NOCACHE~~
/var/www/horst/thepythongamebook.com/data/pages/en/resources/people/jens_horst/colordemo.txt ยท Last modified: 2014/01/09 11:07 (external edit)