自己寫的方法
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