Title: Motion Blur Effect

Author: Gareth Noyce (http://www.korruptor.demon.co.uk)
Submission date: August 4, 2002

Description: A nice motion blur effect using Numeric.

Download: pygame-motionblur.zip

pygame version required: Any with Surfarray
SDL version required: Any
Python version required: 2.0 with Numeric

Comments: Mr. Noyce has created an attractive motion blur effect in 8-bit colour, using Numeric arrays as his accumulator. Make sure to read the included readme file; "burn" and truecolor effects are possible, though increasing the bit depth will reduce your framerate.

Messages: 0


"""
Author		Gareth "Korruptor" Noyce
Email		korruptor@mac.com
Web		http://www.korruptor.demon.co.uk
Depnd		Numeric, Surfarray
Desc:

In this example, we create 12 blank surfarrays in the list:
'surface'. These will be used to store historic frames. In addition 
we create an 'acculumator' surfarray, which is again blank... 

Each time we render a new frame;

* we subtract the pixel values in  the 'oldest' surfarray from the 
  'accumulator', 
* blit a few sprites onto a temporary surface [tmp], 
* convert this temporary surface to a surfarray and store it in 
  place of the 'oldest' surfarray in our list. 
* we then add the pixel values of the newly created surfarray to 
  the accumulator and blit the accumulator to screen. 

Note the use of fixed, ordered palettes which handle the end display.
"""


import pygame, pygame.image
from pygame.surfarray import *
from pygame.locals import *
from Numeric import *

# ------------------------------------------------------------------------------------

RES 		= (320,200)
PI 		= 3.14159
DEG2RAD 	= PI/180
NUM_SURFS 	= 12

# ------------------------------------------------------------------------------------
def main():
     
    	# Initialise pygame, and grab an 8bit display.
    	pygame.init()

    	screen 		= pygame.display.set_mode(RES,0, 8)
        tmp		= screen
        accumulator 	= tmp

        # Load our sprites...
        pal 		= pygame.image.load("palette.gif")
        background 	= pygame.image.load("background.gif")
        sprite		= pygame.image.load("sprite.gif")
        sprite2		= pygame.image.load("sprite2.gif")
    

        # Create a series of blank "frames" which we use to record our blits.
        surface 	= []
        oldest 		= 0
        for i in range(0,NUM_SURFS):
                surface.append(zeros(RES))
        
        # Get everything singing from the same palette
        tmp.set_palette(pal.get_palette())
        screen.set_palette(pal.get_palette())
        sprite.set_palette(pal.get_palette())
        sprite2.set_palette(pal.get_palette())
        sprite.set_colorkey([0,0,0])
        sprite2.set_colorkey([0,0,0])
        background.set_palette(pal.get_palette())

        # The accumulator holds the "final" blit, so set this up as a blank array for now
        accumulator = pygame.surfarray.array2d(accumulator)

        xa =0
        ya =0
        xa1=0
        ya1=0
        xa2=0
        ya2=0

        # Fruity loops...
        while 1:
    
            # Have we received an event to quit the program?
            for e in pygame.event.get():
                if e.type in (QUIT,KEYDOWN,MOUSEBUTTONDOWN):
                    return

            # Subtract the oldest frame from the accumulator...
            accumulator = subtract(accumulator,surface[oldest])

            # Now, draw a new frame...
            x    = ((RES[1]/10))*cos((xa*DEG2RAD) * 3)
            y    = ((RES[1]/4))*cos((ya*DEG2RAD))
            x1   = ((RES[1]/3))*cos((xa1*DEG2RAD)*1.7)
            y1   = ((RES[1]/3))*cos((ya1*DEG2RAD)*2.5)
            x2   = (RES[1]/2)*cos((xa2*DEG2RAD))
            y2   = (RES[1]/2)*cos((ya2*DEG2RAD))
            xa  += 1.5
            ya  += 3
            xa1 += 1.2
            ya1 += 2.75
            xa2 += 2.3
            ya2 += 4.8

            tmp.blit(background,(0,0))
            tmp.blit(sprite,(x+20,y+50))
            tmp.blit(sprite2,(x1+140,y1+75))
            tmp.blit(sprite2,(x2+140,y2+75))

            # And replace the oldest frame with our new one...
            surface[oldest] = pygame.surfarray.array2d(tmp)

            # Add this new frame to the accumulator...
            accumulator = add(accumulator,surface[oldest])

            # Inc the pointer to the next oldest frame...
            oldest = (oldest + 1)%NUM_SURFS

            # ...and blit the accumulator to the screen before repeating...
            blit_array(screen, accumulator)
            pygame.display.flip()

# ------------------------------------------------------------------------------------
if __name__ == '__main__': main()
# End of sauce. Pass the chips...


Main - Repository - Submit - News

Feedback