Python用turtle画棋盘

简单记录一下Python作业,用30行以内画完棋盘。

  1. 注意用circle画正方形的时候,第一个参数是正方形对角线的一半,也就是说,你想要边长60的正方形,你的输入值应该为30 * ^{\sqrt{2}}
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()

运行结果如下图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值