0

So I've been trying to code a tabletop game that I made a long time ago - I'm working on the graphic section now, and I'm trying to draw the 9x7 tile map using nested For loops:

I'm using the numpy library for my 2d array

gameboard = array( [[8, 8, 8, 7, 7, 7, 8, 8, 8],
                [8, 3, 6, 7, 7, 7, 6, 3, 8],
                [0, 1, 1, 6, 6, 6, 1, 1, 0],
                [0, 5, 4, 0, 0, 0, 4, 5, 0],
                [0, 3, 2, 0, 0, 0, 2, 3, 0],
                [8, 8, 1, 0, 0, 0, 1, 8, 8],
                [8, 8, 8, 6, 6, 6, 8, 8, 8]] )
def mapdraw():
for x in [0, 1, 2, 3, 4, 5, 6, 7, 8]:
    for y in [0, 1, 2, 3, 4, 5, 6]:
        if gameboard[(x, y)] == 1:
            #insert tile 1 at location
        elif gameboard[(x, y)] == 2:
            #insert tile 2 at location
        elif gameboard[(x, y)] == 3:
            #insert tile 3 at location
                    #this continues for all 8 tiles
                    #graphics update

When I run this program, i get an error on the line "if gameboard[(x,y)] == 1:" "IndexError: index (7) out of range (0<=index<7) in dimension 0"

I've looked for hours to find what this error even means, and have tried many different ways to fix it: any help would be appreciated.

1
  • 2
    FYI: IndexError means you tried to index with some value outside the array bounds (for example, indexing at [10, 10] in an 8x8 grid). Commented Oct 21, 2012 at 1:51

1 Answer 1

2

You have to index the array using [y,x] because the first coordinate is the row index (which, for you, is the y index).

As an aside, please iterate over a range instead of an explicit list!

for x in range(9):
    for y in range(7):
        if gameboard[y, x] == 1:
            #insert tile 1 at location
        ...
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, thanks very much! I had it set as a range function earlier, but took that out when I thought it was the cause of the error. It all works as expected now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.