简单记录一下Python作业,用30行以内画完棋盘。
- 注意用circle画正方形的时候,第一个参数是正方形对角线的一半,也就是说,你想要边长60的正方形,你的输入值应该为30 *
。
import math
import turtle
def smallTrangle(x,y,color):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
turtle.color(color)
turtle.circle(30 * math.sqrt(2), steps=4)
turtle.end_fill()
def edge():
turtle.penup()
turtle.goto(-240,-240)
turtle.pendown()
turtle.circle(240 * math.sqrt(2),steps=4)
turtle.right(45)
turtle.speed(300)
countX = 0
countY = 0
for x in range(-240,180 + 1,60):
for y in range(-240,180 + 1,60):
if (countX + countY) % 2 == 0:
smallTrangle(x,y,"black")
else:smallTrangle(x,y,"white")
countY += 1
countX += 1
edge()
turtle.hideturtle()
turtle.done()
运行结果如下图: