Python 畫蛇教程是一份Python愛好者不容錯(cuò)過的完整Python實(shí)例項(xiàng)目。它綜合了各個(gè)方面的Python知識(shí)與技巧,完美地將Python編程與美術(shù)設(shè)計(jì)結(jié)合起來,讓大家看到了Python的無限可能性。
畫蛇教程的代碼部分可謂是精彩紛呈,尤其是貪吃蛇的繪制部分。這里我們簡(jiǎn)單分享一下如何用Python繪制一條蛇的代碼:
import turtle def draw_snake(radius,angle,length): turtle.seth(-40) for i in range(length): turtle.circle(radius,angle) turtle.circle(-radius,angle) turtle.circle(radius,angle/2) turtle.fd(20) turtle.circle(5,180) turtle.fd(20) turtle.circle(20,180) turtle.fd(20) turtle.circle(5,180) turtle.fd(70) def main(): turtle.setup(1300,800,0,0) turtle.pensize(30) turtle.pencolor("green") draw_snake(40,80,5) main()
代碼解釋:
import turtle # 引入turtle庫(kù),建立畫布 def draw_snake(radius,angle,length): # 定義函數(shù),傳入?yún)?shù) turtle.seth(-40) # 設(shè)置開始方向 for i in range(length): # 循環(huán)繪制蛇身 turtle.circle(radius,angle) turtle.circle(-radius,angle) turtle.circle(radius,angle/2) # 繪制頭部線條 turtle.fd(20) turtle.circle(5,180) turtle.fd(20) turtle.circle(20,180) turtle.fd(20) turtle.circle(5,180) turtle.fd(70) def main(): turtle.setup(1300,800,0,0) # 設(shè)置畫布大小 turtle.pensize(30) # 設(shè)置畫筆尺寸 turtle.pencolor("green") # 設(shè)置畫筆顏色 draw_snake(40,80,5) # 調(diào)用函數(shù) main() # 執(zhí)行程序
以上代碼簡(jiǎn)單精悍,容易理解,大家可以嘗試運(yùn)行一下,看看效果如何。
上一篇css中盒子空隙問題