<p>呃。。。有点晚了,但如果你还在的话。像这样?在</p>
<p>我做了一点修改,但尽量靠近你的结构:</p>
<pre><code>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()
</code></pre>
<p>因为您需要一个固定的数字或行和列,所以可以有两个变量来说明需要多少行和列。然后使用这些来确定块的大小。在</p>
<p>我已经按照建议改变了数组的颜色,我个人也是这么做的(比如它更短,你可以把它当作一个序列来读)。另外,如果你想改变顺序,你只需移动项目。在</p>
<p>最后,我使用了两个for循环,它们使用numrows和numcols作为范围限制。如果你想想你的时间表,包括0,它会创建一个完美的网格。只需将第一个循环视为行,将嵌套循环视为列。在</p>
<p>好吧,祝你好运。在</p>