from graphics import GraphicsWindow
win=GraphicsWindow(400,400)
canvas=win.canvas()
canvas.setFill("black")
#first line
canvas.drawRect(10,10,10,10)
canvas.drawRect(30,10,10,10)
canvas.drawRect(50,10,10,10)
canvas.drawRect(70,10,10,10)
#second line
canvas.drawRect(20,20,10,10)
canvas.drawRect(40,20,10,10)
canvas.drawRect(60,20,10,10)
canvas.drawRect(80,20,10,10)
#third line
canvas.drawRect(10,30,10,10)
canvas.drawRect(30,30,10,10)
canvas.drawRect(50,30,10,10)
canvas.drawRect(70,30,10,10)
#fourth line
canvas.drawRect(20,40,10,10)
canvas.drawRect(40,40,10,10)
canvas.drawRect(60,40,10,10)
canvas.drawRect(80,40,10,10)
#fifth line
canvas.drawRect(10,50,10,10)
canvas.drawRect(30,50,10,10)
canvas.drawRect(50,50,10,10)
canvas.drawRect(70,50,10,10)
#sixth line
canvas.drawRect(20,60,10,10)
canvas.drawRect(40,60,10,10)
canvas.drawRect(60,60,10,10)
canvas.drawRect(80,60,10,10)
#seventh line
canvas.drawRect(10,70,10,10)
canvas.drawRect(30,70,10,10)
canvas.drawRect(50,70,10,10)
canvas.drawRect(70,70,10,10)
#eighth line
canvas.drawRect(20,80,10,10)
canvas.drawRect(40,80,10,10)
canvas.drawRect(60,80,10,10)
canvas.drawRect(80,80,10,10)
win.wait()
所以这将创建一个带有 64 个黑色方块的棋盘,但我觉得这似乎是更多的代码而不是必要的。如何更改它,以便仅通过增加 y 坐标(x、y、高度、宽度)而不具有这个疯狂的数量来打印第 1、3、5、7 行和第 2、4、6、8 行代码?
像这样的东西?
canvas.drawRect(10,,10,10).formatrange(y,10,30,50,70)
canvas.drawRect(30,,10,10)
canvas.drawRect(50,,10,10)
canvas.drawRect(70,,10,10)
请记住,我的代码确实有效,我只是想让它看起来更好,更简洁。