python基础--画国际象棋棋盘

1.用填充的方法画一个小方块。画一下旋转90度,画四下围成一个小方块

import turtle
turtle.showturtle()

step=20
turtle.begin_fill()
for k in range(4):
    turtle.forward(step)
    turtle.right(90)
turtle.end_fill()


turtle.done()

运行

2.国际象棋是8*8的,所以设置8次的双循环,循环化上面的小方格,颜色先不管,就黑的就可以

import turtle
turtle.showturtle()

step=20
for i in range(8):
    for j in range(8):
        turtle.penup()
        turtle.goto(i*step,j*step)
        turtle.pendown()
        turtle.begin_fill()
        for k in range(4):
            turtle.forward(step)
            turtle.right(90)
        turtle.end_fill()

turtle.done()

运行

3.黑白间隔做一下,用间隔输出就可以

import turtle
turtle.showturtle()

step=20
for i in range(8):
    for j in range(8):
        turtle.penup()
        turtle.goto(i*step,j*step)
        turtle.pendown()
        turtle.begin_fill()
        if (i+j)%2==0:
            turtle.color("white")
        else:
            turtle.color("black")
        for k in range(4):
            turtle.forward(step)
            turtle.right(90)
        turtle.end_fill()

turtle.done()

运行一下:

4.调整下画笔的粗细,显得画面更好看

发布了31 篇原创文章 · 获赞 10 · 访问量 264

猜你喜欢

转载自blog.csdn.net/qq_34240459/article/details/105111691