/usr/share/python-visionegg/demo/flames_pygame.py is in python-visionegg 1.2.1-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | #! /usr/bin/python
"""flames.py - Realtime Fire Effect Demo
Pete Shinners, April 3, 2001
Slightly modified by Andrew Straw, July 24, 2003
Ok, this is a pretty intense demonstation of using
the surfarray module and numeric. It uses an 8bit
surfaces with a colormap to represent the fire. We
then go crazy on a separate Numeric array to get
realtime fire. I'll try to explain my methods here...
This flame algorithm is very popular, and you can find
it for just about all graphic libraries. The fire effect
works by placing random values on the bottom row of the
image. Then doing a simplish blur that is weighted to
move the values upward. Then slightly darken the image
so the colors get darker as they move up. The secret is
that the blur routine is "recursive" so when you blur
the 2nd row, the values there are used when you blur
the 3rd row, and all the way up.
This fire algorithm works great, but the bottom rows
are usually a bit ugly. In this demo we just render
a fire surface that has 3 extra rows at the bottom we
just don't use.
Also, the fire is rendered at half the resolution of
the full image. We then simply double the size of the
fire data before applying to the surface.
Several of these techniques are covered in the pygame
surfarray tutorial. doubling an image, and the fancy
blur is just a modified version of what is in the tutorial.
This runs at about 40fps on my celeron-400
"""
import pygame
from pygame.surfarray import *
import pygame.surfarray
import numpy as np
if hasattr(pygame.surfarray,'use_arraytype'):
use_arraytype('numpy')
pygame_array_func = np.asarray
else:
import Numeric
pygame_array_func = Numeric.array
from pygame.locals import *
from numpy.oldnumeric import *
from numpy.oldnumeric.random_array import *
import VisionEgg
VisionEgg.start_default_logging(); VisionEgg.watch_exceptions()
from VisionEgg.Core import FrameTimer
RES = array((240, 180))
MAX = 246
RESIDUAL = 86
HSPREAD, VSPREAD = 26, 78
VARMIN, VARMAX = -2, 3
def main():
"main function called when the script is run"
#first we just init pygame and create some empty arrays to work with
pygame.init()
screen = pygame.display.set_mode(RES, 0, 8)
print screen.get_flags()
setpalette(screen)
flame = zeros(RES/2 + (0,3))
doubleflame = zeros(RES)
randomflamebase(flame)
#the mainloop is pretty simple, the work is done in these funcs
frame_timer = FrameTimer()
quit_now = 0
while not quit_now:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN,MOUSEBUTTONDOWN):
quit_now = 1
modifyflamebase(flame)
processflame(flame)
blitdouble(screen, flame, doubleflame)
pygame.display.flip()
frame_timer.tick()
frame_timer.log_histogram()
def setpalette(screen):
"here we create a numeric array for the colormap"
gstep, bstep = 75, 150
cmap = zeros((256, 3))
cmap[:,0] = minimum(arange(256)*3, 255)
cmap[gstep:,1] = cmap[:-gstep,0]
cmap[bstep:,2] = cmap[:-bstep,0]
screen.set_palette(cmap)
def randomflamebase(flame):
"just set random values on the bottom row"
flame[:,-1] = randint(0, MAX, flame.shape[0])
def modifyflamebase(flame):
"slightly change the bottom row with random values"
bottom = flame[:,-1]
mod = randint(VARMIN, VARMAX, bottom.shape[0])
add(bottom, mod, bottom)
maximum(bottom, 0, bottom)
#if values overflow, reset them to 0
bottom[:] = choose(greater(bottom,MAX), (bottom,0))
def processflame(flame):
"this function does the real work, tough to follow"
notbottom = flame[:,:-1]
#first we multiply by about 60%
multiply(notbottom, 146, notbottom)
right_shift(notbottom, 8, notbottom)
#work with flipped image so math accumulates.. magic!
flipped = flame[:,::-1]
#all integer based blur, pulls image up too
tmp = flipped * 20
right_shift(tmp, 8, tmp)
tmp2 = tmp >> 1
add(flipped[1:,:], tmp2[:-1,:], flipped[1:,:])
add(flipped[:-1,:], tmp2[1:,:], flipped[:-1,:])
add(flipped[1:,1:], tmp[:-1,:-1], flipped[1:,1:])
add(flipped[:-1,1:], tmp[1:,:-1], flipped[:-1,1:])
tmp = flipped * 80
right_shift(tmp, 8, tmp)
add(flipped[:,1:], tmp[:,:-1]>>1, flipped[:,1:])
add(flipped[:,2:], tmp[:,:-2], flipped[:,2:])
#make sure no values got too hot
minimum(notbottom, MAX, notbottom)
def blitdouble(screen, flame, doubleflame):
"double the size of the data, and blit to screen"
doubleflame[::2,:-2:2] = flame[:,:-4]
doubleflame[1::2,:-2:2] = flame[:,:-4]
doubleflame[:,1:-2:2] = doubleflame[:,:-2:2]
to_blit = pygame_array_func( doubleflame.astype(np.uint8))
blit_array(screen, to_blit)
if __name__ == '__main__': main()
|