ActiveState Code

Recipe 334696: A Sinus Plasma, using Pygame


An old-school demo effect, using sine wave interference patterns. Entertaining for at least a few minutes :)

Modify the freq variable for different patterns.

Python
 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
import pygame
from pygame.locals import *
import random
import Numeric
from math import *

WIDTH = 320     #width of screen
HEIGHT = 240    #height of screen

    
def main():
    pygame.display.init()
    
    screen = pygame.display.set_mode((WIDTH,HEIGHT),DOUBLEBUF,32)
    pixels = pygame.surfarray.pixels3d(screen)
    
    width = len(pixels)-1
    height = len(pixels[0])-1
    freq = 100.0
    
    for y in xrange(height):
        for x in xrange(width):
            z1 = sin(x/freq*1.7*pi)
            z2 = sin((x/3+y)/freq*1.5*pi)
            z3 = sin(y/freq*0.1*pi)
            
            z = abs(z1+z2+z3)*255
            pixels[x,y] = (z,z/4,z*4)

    pygame.display.update()
    done = False
    while not done:
        for e in pygame.event.get():
            if e.type == KEYDOWN:
                done = True
        
if __name__ == "__main__":
    main()

Comments

  1. 1. At 4:41 p.m. on 9 nov 2004, Gregor Lingl said:

    two imports superfluous. A very nice entertainment, indeed!

    The imports of the Numeric an random modules seem to be unnecessary.

    Gregor

  2. 2. At 7:29 p.m. on 9 nov 2004, Dethe Elza said:

    Correction for OS X. On OS X this crashes unless you replace

    pygame.display.init()

    with

    pygame.init()

Sign in to comment