+"""This example demonstrates creating an image with Numeric
+python, and displaying that through SDL. You can look at the
+method of importing numeric and pygame.surfarray. This method
+will fail 'gracefully' if it is not available.
+I've tried mixing in a lot of comments where the code might
+not be self explanatory, nonetheless it may still seem a bit
+strange. Learning to use numeric for images like this takes a
+bit of learning, but the payoff is extremely fast image
+If using an SDL version at least 1.1.8 the window will have
+The code also demonstrates use of the timer events."""
+from pygame.locals import *
+ from RandomArray import *
+ raise SystemExit, 'This example requires Numeric and the pygame surfarray module'
+def stopwatch(message = None):
+ "simple routine to time python code"
+ timer = pygame.time.get_ticks()
+ now = pygame.time.get_ticks()
+ runtime = (now - timer)/1000.0 + .001
+ print message, runtime, ('seconds\t(%.2ffps)'%(1.0/runtime))
+def VertGrad3D(surf, topcolor, bottomcolor):
+ "creates a new 3d vertical gradient array"
+ topcolor = array(topcolor, copy=0)
+ bottomcolor = array(bottomcolor, copy=0)
+ diff = bottomcolor - topcolor
+ width, height = surf.get_size()
+ # create array from 0.0 to 1.0 triplets
+ column = arange(height, dtype=Float)/height
+ column = repeat(column[:, NewAxis], , 1)
+ # create a single column of gradient
+ column = topcolor + diff * column
+ # make the column a 3d image column by adding X (optional, since map_array
+ # can also map a list of colours; performance diff negligible)
+ column = column.astype(UInt32)[NewAxis,:,:]
+ #3d array into 2d array
+ column = pygame.surfarray.map_array(surf, column)
+ # stretch the column into a full image
+ return resize(column, (width, height))
+ "choose random colors and show them"
+ colors = randint(0, 255, (2, 3))
+ grade = VertGrad3D(surf, colors, colors)
+ # Workaround for numpy bug
+ grade = grade.transpose().copy().transpose()
+ pygame.surfarray.blit_array(surf, grade)
+ os.environ['SDL_VIDEO_CENTERED'] = '1'
+ screen = pygame.display.set_mode(size, NOFRAME, 0)
+ pygame.event.set_blocked(MOUSEMOTION) #keep our queue cleaner
+ pygame.time.set_timer(USEREVENT, 500)
+ event = pygame.event.wait()
+ if event.type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
+ elif event.type == USEREVENT:
+if __name__ == '__main__': main()