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

Re: [pygame] Sticky Variables: Terrain Loading



Kris Schnee wrote:
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.

"for y in range(self.size)" is the outer loop. So y is an index for the first pass of the inner x loop. Then its value is clobbered, replaced with some color test. So the next pass through the x loop it is no longer an index.

--
Lenard Lindstrom
<len-l@xxxxxxxxx>