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