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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM