Question re: objects and square grids

Dave Angel davea at davea.name
Wed May 15 20:17:48 EDT 2013


Please put new comments AFTER the part you're quoting.  In other words, 
don't top-post.  Also please trim off the stuff that's no longer 
relevant, so people don't have to read through it all wondering where 
your implied comments are.

On 05/15/2013 06:48 PM, Andrew Bradley wrote:
> ok, now I have tested this more thoroughly, and it seems i can only do the
> grid[x][y] function up to grid[9][9], when i really should be able to be
> doing up to grid[10][20].

No, you shouldn't expect it to go to 10,20.  Remember I said that Python 
is zero-based.  So it goes from 0 to 9 inclusive, and from 0 to 19 
inclusive.  Upper left corner is grid[0][0], while lower right is 
grid[9][19].  Check to make sure that range works, which I think it will.


> What exactly is the function of this row_squares list?

It's a temporary to hold one row.  You could delete it after the outer 
loop ends, if you like. Normally, if this whole thing were inside a 
function, the variable would go away when the function ended, and you'd 
be returning the grid list only.

You could have avoided the separate name by doing some tricky syntax. 
But I'm trying to show you the clearest way of doing things from a 
beginner perspective.

>
>
>
> On Wed, May 15, 2013 at 4:35 PM, Andrew Bradley <abradley201 at gmail.com>wrote:
>
>> Now I want to show you what I have written:
>>
>> row = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>> column = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
>> 18, 19, 20)

No point in initializing them to tuples, since you're not going to use 
those.  Besides, in a moment row and column become ints, and this just 
confuses things.  The range() function below builds a temporary list 
which looks just like what you tediously typed in (except it's square 
brackets instead of round).

>> SQUARESIZE = 43
>>
>> grid = []
>> for row in range(10):
>>      row_squares = []
>>      for column in range(20):
>>          rect = Rect(12 + column * SQUARESIZE, 10 + row * SQUARESIZE,
>> SQUARESIZE, SQUARESIZE)
>>          row_squares.append(rect)
>>      grid.append(row_squares)
>>
>> It appears to be working (that is, the program still runs without
>> crashing).

Sorry, but that's no criteria.  Question is whether it's doing what you 
want.  Are the rows 20 across and are there 10 of them?  Do the values 
of each individual rect look right?  print is your friend.

>> So now, how can I utilize this new grid list? Thank you for the
>> help so far, I feel like the entire grid is now being worked out.
>> -Andrew
>>

That's a Pygame question, and I told you at the beginning, I can't 
really help with that.  I'd like to learn, but not this week.

Others - can you show some minimal code to use these grid parameters to 
color selected squares of the pygame window?

-- 
DaveA



More information about the Python-list mailing list