I开发者_JAVA百科 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()
I can't say anything about the pygame method, but a more idiomatic way to declare the list would be
colors = []
colors.append(white)
...
rather than creating and then overwriting a range. You could even do
colors = [white, white, ...]
though that would probably be an ugly line. Also, your first list element is always at position "0", so you will wind up with a list that has elements [0, white, white, ...]
rather than [white, white, ...]
since you start defining colors at colors[1].
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:
import pygame, sys
from pygame.locals import *
#assign display window dimensions
winwidth = 400
winheight = 700
#number of rows, number of colums
numrows = 10
numcols = 10
#Keeping brick size proportionate to the window size
brickwidth = winwidth / numcols
brickheight = winheight / numcols
#initialize pygame
pygame.init()
#Set display window width, height
windowSurface = pygame.display.set_mode((winwidth, winheight), 0, 0)
#Colours
blue = [0, 0, 255]
green = [0, 255, 0]
yellow = [255, 255, 0]
red = [255, 0, 0]
white = [255, 255, 255]
colours = [white, white, red, red, green, green, yellow, yellow, blue, blue]
class Setup():
def __init__(self):
#Setup nest for loop to generate 2d array of blocks.
for y in range(0, numrows):
for x in range(0, numcols):
#Using modulo to get the different colours for rows, we use y as the changing key
col_index = y % len(colours)
pygame.draw.rect(windowSurface, colours[col_index], (x*brickwidth, y*brickheight, brickwidth, brickheight))
class Main():
Setup()
pygame.display.update()
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.
精彩评论