~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to examples/liquid.py

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
"""This examples demonstrates a simplish water effect of an
 
3
image. It attempts to create a hardware display surface that
 
4
can use pageflipping for faster updates. Note that the colormap
 
5
from the loaded GIF image is copied to the colormap for the
 
6
display surface.
 
7
 
 
8
This is based on the demo named F2KWarp by Brad Graham of Freedom2000
 
9
done in BlitzBasic. I was just translating the BlitzBasic code to
 
10
pygame to compare the results. I didn't bother porting the text and
 
11
sound stuff, that's an easy enough challenge for the reader :]"""
 
12
 
 
13
import pygame, os
 
14
from pygame.locals import *
 
15
from math import sin
 
16
 
 
17
 
 
18
#initialize and setup screen
 
19
pygame.init()
 
20
screen = pygame.display.set_mode((640, 480), HWSURFACE|DOUBLEBUF, 8)
 
21
 
 
22
#load image
 
23
imagename = os.path.join('data', 'liquid.bmp')
 
24
bitmap = pygame.image.load(imagename)
 
25
 
 
26
#get the image and screen in the same format
 
27
if screen.get_bitsize() == 8:
 
28
    screen.set_palette(bitmap.get_palette())
 
29
else:
 
30
    bitmap = bitmap.convert()
 
31
 
 
32
#prep some variables
 
33
anim = 0.0
 
34
 
 
35
#mainloop
 
36
while not pygame.event.peek([QUIT, KEYDOWN, MOUSEBUTTONDOWN]):
 
37
    anim = anim + 0.4
 
38
    for x in range(0, 640, 20):
 
39
        for y in range(0, 480, 20):
 
40
            xpos = (x + (sin(anim + x * .01) * 15)) + 20
 
41
            ypos = (y + (sin(anim + y * .01) * 15)) + 20
 
42
            screen.blit(bitmap, (x, y), (xpos, ypos, 20, 20))
 
43
    pygame.display.flip()
 
44
 
 
45
 
 
46
 
 
47
"""BTW, here is the code from the BlitzBasic example this was derived
 
48
from. This code runs a little faster, yet reads a lot slower. again
 
49
i've snipped the sound and text stuff out.
 
50
-----------------------------------------------------------------
 
51
; Brad@freedom2000.com
 
52
 
 
53
; Load a bmp pic (800x600) and slice it into 1600 squares
 
54
Graphics 640,480                                        
 
55
SetBuffer BackBuffer()                          
 
56
bitmap$="f2kwarp.bmp"                                   
 
57
pic=LoadAnimImage(bitmap$,20,15,0,1600)
 
58
 
 
59
; use SIN to move all 1600 squares around to give liquid effect
 
60
Repeat
 
61
f=0:w=w+10:If w=360 Then w=0
 
62
For y=0 To 599 Step 15
 
63
For x = 0 To 799 Step 20
 
64
f=f+1:If f=1600 Then f=0
 
65
DrawBlock pic,(x+(Sin(w+x)*40))/1.7+80,(y+(Sin(w+y)*40))/1.7+60,f 
 
66
Next:Next:Flip:Cls
 
67
Until KeyDown(1)
 
68
"""