python - Pygame Pixel Array generator -


i'm trying draw functions , graphs using generators in python (2.7.9), , found problem: can't flush generated pixel data (rgb tuples) pygame pixel array. here's code (which tryied run):

  1. using 2d arrays

    pixarr = pygame.pixelarray(surface) pixarr=[[(0,0,function(x,y)) x in range(1,screenx)] y in range(1,screeny)] del pixarr

here i've realized pixarr isn't ordinary 2d list becouse of failure output (print pixarr)

  1. then i've tryied using range

    pixarr[0:screenx*screeny] = [(0,0,function(curpix%screenx, curpix//screeny)) curpix in range(0,screenx*screeny)]

i've found code on pygame website here, gives me error "valueerror: sequence size mismatch", after playing around became clear works single color (for example pixarr[0:screenx*screeny]=(0,0,255) colors whole screen blue)

tl;dr: how can set each pixel without using loops

for y in range(0,1000):     x in range(0,1000):         gfx.draw(surface,x,y,(0,0,function(a,b))) #or pixarr in line 

i'm using pygame 1.9.1, seems pixelarrays changed lot on 1.9.2 beware.

i tried example , works fine:

pixarr = pygame.pixelarray(surface) def f(i):     return % 255 pixarr[:] = [(0, 0, f(i)) in range(0, len(pixarr))] 

one thing notice doing:

pixarr = [(0, 0, f(i)) in range(0, len(pixarr))] 

does not work because converts pixelarray variable list (as in first snippet of code).

on why getting valueerror, did checks, , curiosly (and surpsiringly enough), noticed len(pixarray) not equal pixarray.surface.get_width() * pixarray.surface.get_height(). recomend checks on before go on.

--

edit:

i thought sharing new snippet got because seems interesting:

def f(i):     return % 255 length = len(pixarr) length2 = len(pixarr[0]) print length, length2 j in range(0, length):     array = [(0, f(j), f(i)) in range(0, length2)]     pixarr[j] = array 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -