Python画国际象棋盘

自己写的方法

import turtle as t
def square(n):#画一个正方形,pendown和penup可选择是否显示路径
    for i in range(4):
        t.pendown()
        t.fd(n)
        t.left(90)
        t.penup()

def BTW():#先白后黑
    for i in range(8):
        if i % 2 != 0:
            t.begin_fill()
            square(n)
            t.fd(n * 2)
            t.end_fill()

def WTB(): #先黑后白
    for i in range(8):
        if i % 2 == 0:
            t.begin_fill()
            square(n)
            t.fd(n * 2)
            t.end_fill()

n = eval(input()) #小方块的边长由用户决定
#t.setup(600,600)    #设置窗口大小
t.speed(30) #海龟移动速度
t.penup()
t.color("black")
j=-4
for i in range(4):
    t.goto(-4*n, j * n)
    j += 1
    BTW()
    t.goto(-3*n,j * n)
    WTB()
    j += 1
t.goto(-4*n,-4*n)
t.pendown()
square(8*n)
t.hideturtle()#t.ht()
t.done()

网上参考的方法

import turtle
def drawSquare():
    turtle.pendown()
    for i in range(4):
        turtle.forward(50)
        turtle.left(90)
    turtle.penup()

turtle.speed(30)
turtle.penup()
off = True
for y in range(-200, 150 + 5, 50):
    for x in range(-200, 150 + 5, 50):
        if off:
            turtle.goto(x, y)
            turtle.begin_fill()
            turtle.color("black")
            drawSquare()
            turtle.end_fill()
            turtle.penup()
        else:
            turtle.goto(x, y)
            drawSquare()
        off = bool(int(off) - 1)
    off = bool(int(off) - 1)
turtle.hideturtle()
turtle.done()

参考链接:https://blog.csdn.net/qq_36143023/article/details/75093960

posted @ 2020-10-20 21:44  英魂  阅读(1071)  评论(0)    收藏  举报