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()
uhh… a bit late but if you’re still around. Something like this?
I’ve modified it a bit but trying to keep as close to your structure as possible:
Because you wanted a fix number or rows and columns, you can have two variables stating how many rows and columns you need. Then you use those to determine the sizes of the blocks as you have done.
I’ve changed the colours to the array as suggested, personally that’s how I’d do it too (for one it’s shorter and you can read it as a sequence). Also, if you wanted to change the sequence, you just have to move the items around.
Lastly, I used two for loops that use the numrows and numcols as the range limits. If you think of your times tables, including the 0, it creates a perfect grid. Just think of the first loop as the rows and the nested loop as the column.
Well, good luck.