資訊內(nèi)容
Python turtle 畫蛇
本節(jié)我們將利用畫筆相關(guān)指令來畫一條蛇,如下圖
仔細(xì)觀察這個圖,彎曲的蛇身,可以利用circle命令,來畫弧線。然后向前直行,再畫一個180度的弧線,讓蛇頭轉(zhuǎn)回來,這樣基本上就可以了。
程序示例:
import turtle
def drawSnake(rad, angle, len, neckrad):
for _ in range(len):# 畫弧線,彎曲的蛇身
turtle.circle(rad, angle)
turtle.circle(-rad, angle)
turtle.circle(rad, angle/2)
turtle.forward(rad/2) # 直線前進(jìn)
turtle.circle(neckrad, 180)#回頭
turtle.forward(rad/4) # 直線前進(jìn)
if __name__ == "__main__":
turtle.setup(1500, 1400, 0, 0)
turtle.pensize(30) # 畫筆尺寸
turtle.pencolor("green")
turtle.seth(-40) # 前進(jìn)的方向
drawSnake(70, 80, 2, 15)
本站部分內(nèi)容轉(zhuǎn)載自網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員及時刪除。
