-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Lamonte Harris wrote: > class map_: > def __init__(self,block=100,wndow=(300,300)): > self.BLOCK = block > self.size = wndow > self.map = [] > self.surfaces = [[],[]] > self.window = pygame.display.set_mode(self.size) > pygame.init() > def create_box(self,size,color,rect): > box = pygame.Surface((size)) > pygame.draw.rect(box,color,box.get_rect()) > self.surfaces[0].append(box) > self.surfaces[1].append(rect) > > Can you explain a little more.
I can explain a lot more. In your code, you have a structure called "surfaces", which is kept in the map class. This structure is a list with two elements. These two elements are themselves lists -- the first one is a list of Surface objects, representing images, and the second one is a list of some type representing location of these images on-screen. In the code snippet above, your map "create_box" method adds an image and a location to this "surfaces" structure. These two things, image and location, are "paired", in a sense; the image has a location, and putting a different image at the same location, or putting the same image at a different location, would be a bug. Pairing information in this way is very common in all kinds of programs, not just games, and there are many mechanisms for showing this kind of pairing. You have implemented a mechanism which I will call "parallel arrays"; in order to access an image and its corresponding location, you look up the same index in two lists (surfaces[0][i] and surfaces[1][i]). This is a simple mechanism, and it works fine, but there are some complications. For instance, let's say you have five images, and five locations. Now you want to delete an image, but forget to delete its location. Now you have four images and five locations, and it may not be possible to figure out which image goes with which location. I am proposing the use of another structure, which is called "sprite" here but can really be called anything, which combines both the image and the location. If you were to structure your map_ class this way, instead of having "structures" be a list of two lists, it would be one list of sprites. To access the image and the location, you would do surfaces[i].image and surfaces[i].rect. In addition, you no longer have to worry about deleting all the parts of the sprite object; once the sprite is removed from the "surfaces" list, it's gone, not "half-there" like in the hypothetical "forgot to delete something" case. There is more to pygame sprites, but the reason I suggested you use them is that they keep the image and its location together. This is the "why". The "how" is outlined in the code I sent -- it creates one sprite, and uses its "image" and "rect" attributes to simplify moving it around onscreen (in an admittedly trivial way). As it applies to your own code you can probably figure out on your own. HTH, Ethan -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFG5hRXhRlgoLPrRPwRAkA+AKC2ys7nnDAbdX6Gd3NzVTf0eIZH2QCdFZK2 G9xndlCnp/vvSqr+Hb8K9vY= =lAFx -----END PGP SIGNATURE-----