Hi :) First of all, I must apologize for my poor english :)
I'm starting with python and pygame and for testing (and learning) purposes I wrote an small "Map Editor" for a small game project I'm going to start next month. The tilemap editor is working fine, but reading Guido's Van Rossum PYTHON TUTORIAL I found that my game map is "wasting" memory by using about 16 bytes for each item in it, because every item in a python list takes 16 bytes in memory. In the same tutorial, I found references to the "array()" function of the "array" module. I'm trying to change my current working source code so that it works with an array instead of using a list, but I'm not managing to do it. My tilemap is something like (think on 0=empty, 1=floor, 2=rock, and so...): [ [0 ,0, 0, 0, 0, 0, 0, 0 ,0], [0 ,0, 0, 0, 0, 0, 0, 0 ,0], [2 ,0, 0, 0, 0, 2, 2, 0 ,0], (...) [2 ,2, 0, 0, 2, 2, 2, 0 ,0], [1 ,1, 1, 1, 1, 1, 1, 1 ,1], ] This is how I create the tilemap (and the clipboard, a copy of my tilemap): def __init__( self, bw, bh, tiles ): self.width, self.height = bw, bh self.tilemap = [] self.clipboard = [] (...) for i in range(bh): self.tilemap.append([0] * bw) self.clipboard.append([0] * bw) And that's how I'm using it (the functions I'm having trouble to convert to use the array): #------------------------------------- def CopyToClipboard( self ): for j in range( self.height ): self.clipboard[j][:] = self.tilemap[j][:] #------------------------------------- def Draw( self, clear=1 ): screen = pygame.display.get_surface() if clear: self.Clear() for j in range(self.GetHeight()): for i in range(self.GetWidth()): self.DrawBlock( i, j ) #------------------------------------- def DrawBlock( self, x, y ): screen = pygame.display.get_surface() xd = (x * self.TileWidth()) + self.originx; yd = (y * self.TileHeight()) + self.originy; b = self.tilemap[y][x] self.tileset.tiles[b].Draw( screen, xd, yd ) #------------------------------------- def ResizeWidth( self, new ): bw, bh = self.GetWidth(), self.GetHeight() if new > bw: for j in range(bh): for i in range(new-bw): self.tilemap[j].append( 0 ) self.clipboard[j].append( 0 ) self.SetWidth( new ) elif new < bw: for j in range(bh): for i in range(bw-new): del self.tilemap[j][-1] del self.clipboard[j][-1] self.SetWidth( new ) #------------------------------------- def ResizeHeight( self, new ): bw, bh = self.GetWidth(), self.GetHeight() if new > bh: for i in range(new-bh): self.tilemap.append([0] * bw) self.clipboard.append([0] * bw) self.SetHeight( new ) elif new < bh: for i in range(1,bh-new): del self.tilemap[-1] self.SetHeight( new ) In fact, I'm even unable to create the array correctly: I've tried: self.tilemap = array('H', []) for i in range(bh): self.tilemap.append([0] * bw) and: for i in range(bh): for j in range(bw): self.tilemap[i].append(0) But I receive errors like (either defining or using the array): b = self.tilemap[y][x] TypeError: 'int' object is unsubscriptable or: self.tilemap.append( [0] * bw ) TypeError: an integer is required So ... please ... any idea on how to convert my "python object" array of lists to a bidimensional array and how to use it to index [y][x] elements, or even resize it with the ResizeWidth() and Height() functions? Thanks everybody. PS: Please, think that I come from C/C++ so I still have some "C ways of doing things" while there are better/faster ways to do the same in Python. Feel free also to correct any "C-Style" programming way that you can find in my source code above... :-) -- http://mail.python.org/mailman/listinfo/python-list