Title: "Splurge" effect moduleAuthor: Gareth Noyce (g at korruptor.demon.co.uk) Description: Implements a horizontal or vertical pixmap flood fill.
Download: splurge.zip pygame version required: Any (with Surfarray) Comments: Another nicely commented demo effect script from Mr. Noyce. This is a pretty, flexible effect, and the code is quite clear - it's a nice place to start if you're just learning to use Numeric and surfarray. |
""" splurge.py -- Gareth Noyce (aka Korruptor) v1.0 -- Testcode for SealBasher... http://www.korruptor.demon.co.uk Splurge is another old time 2D effect, creating a nice flood fill for bitmaps. This is especially nice when used to introduce logos, or hi-score tables, or other menu type objects that have transparency -- when they appear over an animated backdrop. The algo is simplicity itself, and two directional splurges are shown in the 'splurge' function. Vertical: -- For each /row/ in the surface, we loop horizontally across each pixel, inserting the colour found into each pixel below it. Some simple array slices perform this "flood copy"... Horizontal: -- Similar to vertical, except this time we loop for each /column/ in the surface, and using slices copy the contents of this column into every 'column' after it. Viewing the effect probably explains it better than I can, and the source is trivial. The beauty of splurges being, you don't need to erase stuff, just carry on splurging! Extensions for Alpha controls (fading alpha on splurge given distance) and other such features are left out of this for simplicity...""" #------------------------------------------------------------------------------------- import pygame, pygame.image from pygame.surfarray import * from pygame.locals import * from Numeric import * # ------------------------------------------------------------------------------------ # Glob decs # Screen resolution... RES = array((338,100)) # ------------------------------------------------------------------------------------ def main(): "Inisalises display, loads the image, sets up the working surf arrays, converts loaded image to an array and calls the splurge repeatedly..." # Initialise pygame, and grab an 8bit display. pygame.init() # setup a surface for the display screen_surface = pygame.display.set_mode(RES, 0, 8) # Load a simple image to use for the effect... image_surface = pygame.image.load("pygame_small.gif") # Convert this to a surf array for reference... image_map = pygame.surfarray.array2d(image_surface) # Save the x/y values for later... map_size = image_surface.get_size() # Initialise a new surfarray which we'll use as our working env... working_map = zeros(map_size) # Take the palette from the image and apply it to the screen... screen_surface.set_palette(image_surface.get_palette()) while 1: # Have we received an event to close the window? for e in pygame.event.get(): if e.type in (QUIT,KEYDOWN,MOUSEBUTTONDOWN): return # Splurge horiz and vertical until Quit... splurge(map_size,image_map,working_map,screen_surface, 1) splurge(map_size,image_map,working_map,screen_surface, 0) # ------------------------------------------------------------------------------------ def splurge(size,imap,omap,screen,dir): # Initialise out offset vars to 0 x_cut, y_cut = 0,0 if(dir): # Go for a horizontal splurge... for i in range(0,size[1]): for x in range(x_cut,size[0]): # Set all pixels below the current x,y to the value of this x,y omap [x][y_cut:size[1]] = imap[x][y_cut] # we've done a row, move to the next... y_cut += 1 # display this frame blit_array(screen, omap) pygame.display.update() else: for i in range(0,size[0]): for x in range(x_cut,size[0]): # set all pixels in the columns (y slice) right of this x to be the same as this column (y slice) # This one is actually the easier slice to figure out... Start here if you're confused... omap[x][:size[1]] = imap[x_cut][:size[1]] # Move to next 'column' x_cut += 1 # display this frame blit_array(screen, omap) pygame.display.update() # ------------------------------------------------------------------------------------ # ------------------------------------------------------------------------------------ # Splurge away baby... if __name__ == '__main__': main() # End of sauce. Pass the chips...
Main - Repository - Submit - News |