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

Re: [pygame] Sticky Variables: Terrain Loading



Dave LeCompte wrote:
I did notice that you were using "y" as both a coordinate and as a flag
for whether the current pixel is yellow. Do you still have trouble if you
don't reuse "y" in this way?

Oops. I tried calling it "yellow" instead, and it suddenly works. I don't understand _why_ it works though, and this bothers me. Re-using variable names like that is bad, of course, but in this case the first version of "y" is being used to get the color of the test image at a certain pixel, and then "y" is used to represent whether that pixel is yellow. Since the color is already gotten by the time "y" is changed, using "y" for "yellow" should have no effect -- and I don't see why it would lead to the "sticky variable."

The loop says "for y in range(something)." I thought that loops used each value in the list produced by the range statement, so that Python would be unaffected if I changed y during the loop.


Douglas Bagnall wrote:
There is no point deleting the terrain label.  The trouble is you're
trying to do things the wrong way. Perhaps you should explain why it
matters, and we can show you a different way to deal with that.

My intent was to have a list of N*N entries representing the terrains stored at each spot in an N-size terrain map. I have code that reads those entries and picks graphical tiles based on those. If the entries are recorded according to the pixels of a map file, I can load an image and use that as a set of game terrain. If (as happened here) the act of adding entries to the terrain list just adds the same thing over and over despite my trying to tell it "no, add this new value," then map loading won't work.

In this case the exact thing I was doing doesn't much matter because I wanted to test loading from a particular image, and realistically I plan to use fixed colors (eg. grass is always (0,255,0)) or not use image-loading at all. Still, I'm curious as to what went wrong.