I am trying to draw lines in Pygame by calling point coordinates from an array index. However, this returns an error:
Traceback (most recent call last): File "C:/Python33/Games/lineTest.py", line 33, in pygame.draw.line(windowSurface,BLACK,(0,0), (0, list[j]), 3) IndexError: list index out of range
Here is my code:
import pygame, sys, random, time
from pygame.locals import *
# sets up pygame
pygame.init()
# sets up the window
windowSurface = pygame.display.set_mode((500, 500), 0, 32)
pygame.display.set_caption('Line Test')
# sets up the colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# draw white background
windowSurface.fill(WHITE)
print('Please enter a number:')
number = input()
# generate random numbers for array
i = 0
list = []
while int(number) > i:
i = i+1
x = random.randint(1, 500)
list.append(x)
# draw lines
j = 0
while int(number) > j:
j = j+1
pygame.draw.line(windowSurface,BLACK,(0,0), (0, list[j]), 3)
# Draw the window to the screen
pygame.display.update()
I was wondering if anyone might have a solution for getting past this error?
j = j+1andpygame.draw.line(windowSurface,BLACK,(0,0), (0, list[j]), 3)Also look at docs.python.org/2/tutorial/controlflow.html#the-range-function instead ofwhileloops.