Is it possible (in terms of performance) to have a single multi-dimensional array that contains one 8-bit integer per pixel, for each pixel in the game window? I need to update the game window in a timely manner based on this array.
I’m aiming for something like the following:
import numpy
window_array = numpy.zeros((600, 600), dtype=numpy.int8)
#draw the screen
for (y, x), value in numpy.ndenumerate(window_array):
if value == 1:
rgb = (0, 0, 0)
elif value == 2:
rgb = (50, 50, 50)
blit_pixel(x, y, rgb)
I’d like to be going 30-60 FPS, but so far my tests have yielded results that were much too slow to run at even a bad framerate. Is it possible to do, and if so, how?
I have never used pygame, so take my anwser with a grain of salt…
That said, it seems very unlikely that you are going to get any decent frame rate if you are iterating over 360,000 pixels with a python loop and doing a python function call at each one.
I learned from this other question (read the comment thread) that the
pygame.surfarraymodule will give you a reference to the array holding the actual screen data.pygame.surfarray.pixels3dshould return a reference to an array of shape(rows, cols, 3)where the last dimension holds the RGB values on screen. With that reference, you can change the pixels on screen directly, without needing a python loop doing something like:Not sure if a call to
pygame.display.updateis needed to actually show your changes, but this approach will sustain a much, much higher frame rate than what you had in mind.