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

Re: [pygame] bidimensional arrays or the quick way to maps



Jasper ha scritto:
Richard Jones wrote:
On Wed, 5 Sep 2007, pistacchio wrote:
maps are certainly a very common feature in game development. the
easiest way to trace maps (and the one that i've used in other languages
in tha past) is to use multidimensional arrays. so, a tile-map, can be
stored and worked on as a simple array (or list) that goes like this:

tile_map[x][y] = tile_number

tile_map = {}
tile_map[x, y] = tile_number
cell = tile_map.get(x, y)

or to automatically default cells to None:

import collections
tile_map = collections.defaultdict()
cell = tile_map[x, y]

or if you don't want it sparse:

tile_map = [[None]*x_dim for i in range(y_dim)]

I prefer the sparse approach.


    Richard


The dictionary approach is what I use as well. It has some nice touches, such as the ability to have non-square maps without using "this cell is illegal" filler, and the ability to work in special case cells that are outside of the standard grid coords. The resulting code tends to be more readable as well, as you can use expressions like "if cellLoc in tileMap:", rather than "x,y = cellLoc", "if tileMap[x][y] != None and 0 <= x <= WIDTH and 0 <= y <= HEIGHT".

The downside is you can't use array slicing tricks (especially the funkier NumPy ones), but in practice that doesn't seem to be something you need for Tile Maps.

-Jasper

thanks to you all, it is working beautifully ^__^