[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Need help with Surfarray



Oh sorry, here's my plasma.py demo -

import os, pygame, math
from pygame.locals import *

try:
    from pygame.surfarray import *
    from Numeric import *
except Importerror:
    raise ImportError, "Numeric and Surfarray are required for this demo."


sinc = zeros(1800)
pal = zeros((256, 3))     #current palette used for image
screenpal = zeros((320, 200, 3))

def PreSin():
    for i in range(0, 1800):
        sinc[i] = (math.sin((3.1416*i)/180)*1024);

def PrePal():
    r = g = b = 0
    for i in range(0, 42):
        pal[i] = (r, g, b)
        r += 1
    for i in range(42, 84):
        pal[i] = (r, g, b)
        g += 1
    for i in range(84, 126):
        pal[i] = (r, g, b)
        b += 1
    for i in range(126, 168):
        pal[i] = (r, g, b)
        r -= 1
    for i in range(168, 210):
        pal[i] = (r, g, b)
        g -= 1
    for i in range(210, 252):
        pal[i] = (r, g, b)
        b -= 1

def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""
#Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode((320, 200))
    pygame.display.set_caption('Plasma Demo   Luke \'05')
    pygame.mouse.set_visible(0)

    background = "">     background = "">     background.fill((250, 250, 250))

    screen.blit(background, (0,0))

    PreSin()
    PrePal()

    #Initial Variables
    g = h = b = e = f = 0
    a = 1

    for j in range(1, 256):
        for k in range(0, 200):
            screenpal[j][k] = pal[j]
    blit_array(screen, screenpal)
    pygame.display.flip()

    clock = pygame.time.Clock()

    e = zeros(320)
    f = zeros(200)

    while(1):

        #clock.tick(60)

        a += 1
        if a > 720:
            a = 0

##        e[:] = (75+((sinc[("current index of e" << 1)+(a >> 1)]+sinc["current index of e"+(a << 1)]+(sinc["current index of e" >> 1)+a] << 1)) >> 6))
##        f[:] = (75+(((sinc["current index of f"+(a << 1)] << 1)+sinc[("current index of f" << 1)+(a >> 1)]+(sinc["current index of f"+a] << 1)) >> 5))
##
##        screenpal[:] = ((multiply(e,f)) >> 5)

        for c in range(0, 320):
            e=75+((sinc[(c << 1)+(a >> 1)]+sinc[c+(a << 1)]+(sinc[(c >> 1)+a] << 1)) >> 6)
            for d in range(0, 200):
                f=75+(((sinc[d+(a << 1)] << 1)+sinc[(d << 1)+(a >> 1)]+(sinc[d+a] << 1)) >> 5)
                screenpal[c-1][d] = ((e*f) >> 5)
               
        blit_array(screen, screenpal)

        #check for user input to quit
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN \
        and event.key == K_ESCAPE:
                return
       
        pygame.display.flip()

if __name__ == '__main__': main()


On 10/4/05, Guillaume Proux <gproux@xxxxxxxxxxxxxx> wrote:
Luke Arntson wrote:
> So I'm currently working on making a plasma demo for python, I'm using a
> C source as basically my how-to.  The only problem is the C source of
> course has to use a for loop w/ a nested for loop, so in all there are
> 64000 calls.  In C++ this is absolutely no problem, however in Pygame
> this makes for a SLOW SLOW plasma effect.

Post some working code, it is easier to collectively optimize something
by all testing different strategy on our own computers.

cheers,

giyokun