Python之turtle畫同心圓和棋盤


畫餅圖

import turtle

t = turtle.Pen()

for i in range(5):
    t.penup()
    t.goto(0, -i*30)
    t.pendown()
    t.circle(i*30+30)

turtle.done()

 

畫棋盤

import turtle

t = turtle.Pen()

widthall = 200
width = 20
num = widthall // 20 * 2 + 1

t.speed(10)

for r in range(num):
    t.penup()
    t.goto(-widthall, widthall - width * r)
    t.pendown()
    t.goto(widthall, widthall - width * r)

for c in range(num):
    t.penup()
    t.goto(-widthall + width * c, widthall)
    t.pendown()
    t.goto(-widthall + width * c, -widthall)

turtle.done()

 

 

 

海龜繪圖
繪制簡單的五角星。
導入turtle模塊
默認情況下,海龜的開始位置在窗口的中間,朝向右下方,筆是向下的。
然后,控制海龜進行多次轉彎,畫出線段。
星形的中心是正五邊形,正五邊形的每個內角為108°。
五個等腰山叫醒連接在五邊形的外部。
因為五邊形的一側形成三角形延伸,每個三角形的底角為72°(補角:180°-108°)
等腰三角形的兩個底角度數相同,加起來是144°。所以第三個角必須是36°。
為了實現急轉彎,在星形的每個頂點需要轉144°(即180°-36°)。
因此在每個頂點,有turtle.right(144)。
import turtle
t = turtle.Pen()

t.forward(100)
t.right(144)
t.forward(100)
t.right(144)
t.forward(100)
t.right(144)
t.forward(100)
t.right(144)
t.forward(100)

turtle.done()

 

謝謝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM