-5

I want to draw 20 rectangles (for my brick breaker game) and I have them in a list. But when I run it, it gives me an index error.

def setup():
    numRects = 20

    rectInfo = [rX, rY, rW, rH] 
    allRectInfo = [rectInfo[:] for i in range(numRects)]
    allRectInfo[0] = [45, 20, 110, 30]
    allRectInfo[1] = [45, 60, 110, 30]
    allRectInfo[2] = [45, 100, 110, 30]
    allRectInfo[3] = [45, 140, 110, 30]
    allRectInfo[4] = [195, 20, 110, 30]
    allRectInfo[5] = [195, 60, 110, 30]
    allRectInfo[6] = [195, 100, 110, 30]
    allRectInfo[7] = [195, 140, 110, 30]
    allRectInfo[8] = [345, 20, 110, 30]
    allRectInfo[9] = [345, 100, 110, 30]
    allRectInfo[10] = [345, 60, 110, 30]
    allRectInfo[11] = [345, 140, 110, 30]
    allRectInfo[12] = [495, 140, 110, 30]
    allRectInfo[13] = [495, 100, 110, 30]
    allRectInfo[14] = [495, 60, 110, 30]
    allRectInfo[15] = [495, 20, 110, 30]
    allRectInfo[16] = [645, 20, 110, 30]
    allRectInfo[17] = [645, 60, 110, 30]
    allRectInfo[18] = [645, 100, 110, 30]
    allRectInfo[19] = [645, 140, 110, 30]


def draw():

    for i in range (numRects):
        rect (allRectInfo[i][0], allRectInfo[i][1], allRectInfo[i][2], allRectInfo[i][3], allRectInfo[i][4], allRectInfo[i][5], allRectInfo[i][6], allRectInfo[i][7], allRectInfo[i][8], allRectInfo[i][9], allRectInfo[i][10], allRectInfo[i][11], allRectInfo[i][12], allRectInfo[i][13], allRectInfo[i][14], allRectInfo[i][15], allRectInfo[i][16], allRectInfo[i][17], allRectInfo[i][18], allRectInfo[i][19])

I think I set the range to 20 and I have 20 rectangles. I am still VERY new to python so I have no idea what's wrong. The error message is; IndexError: Index out of range: 4

(Im using processing 3.5.3)

David Sidarous
  • 1,202
  • 1
  • 10
  • 25
trashcan
  • 23
  • 3
  • Each sublist has a length 4 but your indices go up to 19. The error comes from index 4 – Sheldore May 23 '19 at 21:35
  • You're trying to index a 5th, 6th... and 20th element of each item in the list. Maybe you mean `rect(allRectInfo[i][0], allRectInfo[i][1], allRectInfo[i][2], allRectInfo[i][3])` ? – Ruzihm May 23 '19 at 21:35

3 Answers3

1

allRectInfo is a list of 20 sub-lists, each of which containing 4 values.

So at most you can access allRectInfo[19][3].

Some common sense in addition to that:

By the name of it, rect is most likely a function which takes 4 parameters.

You are calling it with 20(!!!) parameters, which should ring an alarm bell...

goodvibration
  • 5,980
  • 4
  • 28
  • 61
1

Each list element within allRectInfo contains only 4 elements In your loop you try to access elements after the fourth which don't exist.

Apart from that problem, why are you passing 20 element? I think what you want to do is to pass the values to rect function this way

rect (allRectInfo[i][0], allRectInfo[i][1], allRectInfo[i][2], allRectInfo[i][3], allRectInfo[i][4]) 
David Sidarous
  • 1,202
  • 1
  • 10
  • 25
0

allRectInfo contains 20 lists. Each one of these sublists has a length of only 4, meaning that index 3 is the maximum accessible index. That is why you're getting an error at index 4.

You may have meant to use allRectInfo[0][i]

This might be what you're looking for:

for i in range(numRects):
    for x in range(20):
        rect(allRectInfo[x][i])
Alec
  • 8,529
  • 8
  • 37
  • 63