PDA

View Full Version : Using an integer variable as an array subscript in Python



Chameco
November 21st, 2010, 02:49 PM
Hello. I have recently started programing a small game in python using pygame, which I eventually plan to grow into a not-so-small game. I am competent with pygame (read: I can hobble along), but have recently had a problem. I am trying to organize the properties of various tiles using a two-dimensional nested list. I have a problem with subscripting. I am trying to use a while loop nested within another while loop to check to see if there are any "columns" left (the nested list), and if there are, executing the second loop to write values to all the objects in the column. I tried to use variables as list subscripts, but realized that it was just trying to use the second variable as a subscript to the first. This whole paragraph is probably incomprehensible, so here is my code.

def redrawScreenWithCharPosition(charPositionX, charPositionY):
row = [[0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0]]
moreRowsLeft = 0
moreCollumnsLeft = 0
while moreCollumnsLeft <= 9:
while moreRowsLeft <= 9:
if row[moreCollumnsLeft[moreRowsLeft]] != 1:
row[moreCollumnsLeft[moreRowsLeft]] = 0
pixelCoordinateCollumn = moreCollumnsLeft * 50
pixelCoordinateRow = moreRowsLeft * 50
screen.blit(tile, (pixelCoordinateCollumn, pixelCoordinateRow))
pygame.display.flip()
moreCollumnsLeft = moreCollumnsLeft + 1
moreCollumnsLeft = moreCollumnsLeft + 1
moreRowsLeft = 1

Arndt
November 21st, 2010, 03:11 PM
Hello. I have recently started programing a small game in python using pygame, which I eventually plan to grow into a not-so-small game. I am competent with pygame (read: I can hobble along), but have recently had a problem. I am trying to organize the properties of various tiles using a two-dimensional nested list. I have a problem with subscripting. I am trying to use a while loop nested within another while loop to check to see if there are any "columns" left (the nested list), and if there are, executing the second loop to write values to all the objects in the column. I tried to use variables as list subscripts, but realized that it was just trying to use the second variable as a subscript to the first. This whole paragraph is probably incomprehensible, so here is my code.

def redrawScreenWithCharPosition(charPositionX, charPositionY):
row = [[0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0]]
moreRowsLeft = 0
moreCollumnsLeft = 0
while moreCollumnsLeft <= 9:
while moreRowsLeft <= 9:
if row[moreCollumnsLeft[moreRowsLeft]] != 1:
row[moreCollumnsLeft[moreRowsLeft]] = 0
pixelCoordinateCollumn = moreCollumnsLeft * 50
pixelCoordinateRow = moreRowsLeft * 50
screen.blit(tile, (pixelCoordinateCollumn, pixelCoordinateRow))
pygame.display.flip()
moreCollumnsLeft = moreCollumnsLeft + 1
moreCollumnsLeft = moreCollumnsLeft + 1
moreRowsLeft = 1

So you mean that it's row[moreCollumnsLeft][moreRowsLeft] you want?

("Column" has one l, by the way.)

spjackson
November 21st, 2010, 03:20 PM
I'm not totally sure what you are asking.

First, your array subscripts should look like this:

row[moreCollumnsLeft][moreRowsLeft]or the other way round, because I am unclear whether you mean column to be outer and row to be inner, or the other way round.

Your looping is a bit awry, and as I say, I'm not sure what you mean to be outer and inner but within

while moreRowsLeft <= 9:you need to be incrementing moreRowsLeft and within


while moreCollumnsLeft <= 9:

you need to increment moreCollumnsLeft. You seem to have these mixed up.

Consider a for loop if you have a known number of iterations.

StephenF
November 21st, 2010, 05:11 PM
Thoughts:


rows = [[0]*10 for x in xrange(10)]
for pc_row, row_data in enumerate(rows):
for pc_col, element in enumerate(row_data):
screen.blit(tile, (pc_col * 50, pc_row * 50))
pygame.display.flip()

I still don't get what you want with those zeros. Lots of data going up in smoke. Could reduce further but not sure what you really need.

unknownPoster
November 21st, 2010, 06:36 PM
you may want to consider using numpy arrays.