I can't figure out what's happening here. I've got a few lists set up, one for each individual color with corresponding RGB values as its member, and then the list, colors[], that contains each individual color list. Then I've got a nested for loop: the outer loop creates columns of color-filled rectangles and the inner loop advances the row. Not complicated, as I see it.
I'm trying to use numdown to traverse the colors[] list so that every two rows the color changes to the member corresponding in colors[].
The problem is that when I use the inner list's numover, it works fine, except obviously I get the wrong color pattern (colors advance across rather than down). If I use numdown to traverse the list, only the member white seems to be accessed, even though if in the inner for-loop I 'print(numdown)' or even 'print(colors[numdown])' the correct value is printed.
Why is this the case? Why if I use the inner-for's numover are the list member's accessed correctly, but if I use the outer-for's numdown it breaks?
It occurs to me that this might have something to do with pygame, though I wouldn't have any idea what.
(Additionally, as really I am just starting with Python, if you see anything else worth jumping on, method or style-wise, please feel free to point it out.)
import pygame, sys
from pygame.locals import *
#initialize pygame
pygame.init()
#assign display window dimensions
winwidth = 400
winheight = 700
#number of rows, number of colums
numrows = range(1,11)
numcols = range(1,11)
#Keeping brick size proportionate to the window size
brickwidth = winwidth / (len(numrows))
brickheight = winheight / 4
#Pixel space above the breakout area
bricktopspace = winheight / 7
#Set display window width, height
windowSurface = pygame.display.set_mode((winwidth, winheight), 0, 0)
brickxcoord = 0
blue = [0, 0, 255]
green = [0, 255, 0]
yellow = [255, 255, 0]
red = [255, 0, 0]
white = [255, 255, 255]
colors = range(0,11)
colors[1] = white
colors[2] = white
colors[3] = red
colors[4] = red
colors[5] = green
colors[6] = green
colors[7] = yellow
colors[8] = yellow
colors[9] = blue
colors[10] = blue
class Setup():
for numdown in numcols:
for numover in numrows:
print(numdown)
pygame.draw.rect(windowSurface, colors[numdown], (brickxcoord,
bricktopspace, brickwidth, brickheight))
brickxcoord = brickxcoord + brickwidth
bricktopspace = bricktopspace + brickheight
class Main():
Setup()
pygame.display.update()
for... inwill iterate over your lists starting with index0, not1. That might be your problem. Please post more code.