Source

pygame / examples / vgrade.py

ahvezda 99d32cc 






































































#test out surface arrays

import pygame
from pygame.locals import *

try:
    from Numeric import *
    from RandomArray import *
    import pygame.surfarray
except ImportError:
    raise SystemExit, 'This example requires Numeric and the pygame surfarray module'


timer = 0
def stopwatch(message = None):
    "simple routine to time python code"
    global timer
    if not message:
        timer = pygame.time.get_ticks()
        return
    now = pygame.time.get_ticks()
    runtime = (now - timer)/1000.0 + .001
    print message, runtime, ('seconds\t(%.2ffps)'%(1.0/runtime))
    timer = now



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, typecode=Float)/height
    column = repeat(column[:, NewAxis], [3], 1)
    # create a single column of gradient
    column = topcolor + (diff * column).astype(Int)
    # make the column a 3d image column by adding X
    column = column.astype(UnsignedInt8)[NewAxis,:,:]
    # stretch the column into a full image
    return resize(column, (width, height, 3))



def DisplayGradient(surf):
    "choose random colors and show them"
    stopwatch()
    colors = randint(0, 255, (2, 3))
    grade = VertGrad3D(surf, colors[0], colors[1])
    pygame.surfarray.blit_array(surf, grade)
    pygame.display.flip()
    stopwatch('Gradient:')



def main():
    pygame.init()
    size = (640, 480)
    screen = pygame.display.set_mode(size, 0, 32)
    finished = 0
    pygame.event.set_blocked(MOUSEMOTION) #keep our queue cleaner
    while 1:
        if pygame.event.poll().type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
            break
        DisplayGradient(screen)
        pygame.time.delay(500)

    

if __name__ == '__main__': main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.