为什么这个网格不均匀?
Why is this grid not even?
这可能是非常明显的事情,但我似乎无法找到为什么网格中的前到列相同。
grid = [[1]*8 for n in range(8)]
cellWidth = 70
def is_odd(x):
return bool(x - ((x>>1)<<1))
def setup():
size(561, 561)
def draw():
x,y = 0,0
for xrow, row in enumerate(grid):
for xcol, col in enumerate(row):
rect(x, y, cellWidth, cellWidth)
if is_odd(xrow+xcol):
fill(0,0,0)
else:
fill(255)
x = x + cellWidth
y = y + cellWidth
x = 0
def mousePressed():
print mouseY/cellWidth, mouseX/cellWidth
print is_odd(mouseY/cellWidth + mouseX/cellWidth)
我从上面的代码得到的结果是:
有什么想法吗?
看起来 fill
命令不会改变您最后绘制的矩形的颜色;相反,它会更改其后所有绘制调用的颜色。根据the docs:
Sets the color used to fill shapes. For example, if you run fill(204, 102, 0), all subsequent shapes will be filled with orange.
所以你所有的颜色都落后一格。就好像所有的瓷砖都向右移动了一个,除了最左边的一行向下移动了一个,八个向左移动了。这使得该行与所有其他行不匹配。
尝试将您的 fill
电话放在 rect
电话之前:
for xcol, col in enumerate(row):
if is_odd(xrow+xcol):
fill(0,0,0)
else:
fill(255)
rect(x, y, cellWidth, cellWidth)
x = x + cellWidth
这可能是非常明显的事情,但我似乎无法找到为什么网格中的前到列相同。
grid = [[1]*8 for n in range(8)]
cellWidth = 70
def is_odd(x):
return bool(x - ((x>>1)<<1))
def setup():
size(561, 561)
def draw():
x,y = 0,0
for xrow, row in enumerate(grid):
for xcol, col in enumerate(row):
rect(x, y, cellWidth, cellWidth)
if is_odd(xrow+xcol):
fill(0,0,0)
else:
fill(255)
x = x + cellWidth
y = y + cellWidth
x = 0
def mousePressed():
print mouseY/cellWidth, mouseX/cellWidth
print is_odd(mouseY/cellWidth + mouseX/cellWidth)
我从上面的代码得到的结果是:
有什么想法吗?
看起来 fill
命令不会改变您最后绘制的矩形的颜色;相反,它会更改其后所有绘制调用的颜色。根据the docs:
Sets the color used to fill shapes. For example, if you run fill(204, 102, 0), all subsequent shapes will be filled with orange.
所以你所有的颜色都落后一格。就好像所有的瓷砖都向右移动了一个,除了最左边的一行向下移动了一个,八个向左移动了。这使得该行与所有其他行不匹配。
尝试将您的 fill
电话放在 rect
电话之前:
for xcol, col in enumerate(row):
if is_odd(xrow+xcol):
fill(0,0,0)
else:
fill(255)
rect(x, y, cellWidth, cellWidth)
x = x + cellWidth