[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Fastest way to plot a map.



> I've got some code that currently keeps a map as an array of integers:
> 
> map = [[0] * map_max_y for i in range(map_max_x)]
> 
> The map is larger than the viewport.
>
> f course this is slow.  Screen redraws are on the order of a half a
> second.  Is there something I can do with pynumeric to make this fast
> enough for realtime (atleast 10 fps)?  Can I speed up an order of
> magnitude using map somethow?  Do I need to drop to C?

looking at the code there is something to consider...

each cell in your map represents a single pixel on the screen.
usually when dealing with scrolling tile engines like this each
cell refers to an actual tile image, something like 32x32.

if you think about it, there's not much difference between
what you are doing and just storing the map as a big image.
if you used an 8bit format along with Numeric, your code can
look something like this...

#"map.png" is an 8bit image, with its own colormap
map = pygame.image.load('map.png')

#to display the map...
screen.blit(map, (viewport.x, viewport.y))
pygame.display.flip()


#to check if a section of the map is nonzero...
def mapcollide(map, rect):
    pixels = pygame.surfarray(pixels2d(map))
    section = pixels[rect.left:rect.right, rect.top:rect.bottom]
    if section: return 1
    return 0


anyways, this should run a lot quicker for you. no need to
fallback on C or anything like that. i am slightly worried
about the memory requirements for your map. using a simple
8bit image to store your map will only need about 1MB for
each 1000x1000 of map. using your list of list of ints would
need about 11MB for each 1000x1000 of map data.

anyways, the set_at/get_at functions in pygame are mainly
convenience functions, but they are honestly too slow for any
heavy processing. especially if using them to set the entire
screen.


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org