Python語言在人工智能、數據分析等領域有廣泛應用,同時還能通過一些第三方庫進行創意編程,例如用Python畫小雞6。
import turtle # 設置畫布大小 turtle.setup(800, 600) # 將海龜移至小雞的頭部位置,并面向東方 turtle.penup() turtle.goto(-150,0) turtle.pensize(5) turtle.pencolor('orange') turtle.pd() # 畫頭部 turtle.begin_fill() turtle.setheading(60) # 小雞頭的30度角度 turtle.circle(-100, 60) turtle.circle(-10, 180) turtle.circle(-100, 60) turtle.end_fill() turtle.pu() turtle.goto(-80,37) turtle.pd() turtle.fillcolor('white') # 畫眼睛 turtle.begin_fill() turtle.circle(45) turtle.end_fill() turtle.pu() turtle.goto(-75,70) turtle.pd() turtle.fillcolor('black') turtule.begin_fill() turtle.circle(20) turtle.end_fill() # 畫嘴巴 turtle.pu() turtle.goto(-10,-5) turtle.pd() turtle.setheading(270) turtle.circle(20,180) turtle.pu() turtle.goto(-5,-25) turtle.pd() turtle.forward(10) # 畫身體 turtle.pu() turtle.goto(60,-40) turtle.pd() turtle.fillcolor('orange') turtle.begin_fill() turtle.setheading(-50) turtle.circle(50, 240) turtle.setheading(0) turtle.forward(120) turtle.setheading(180) turtle.circle(-50, 240) turtle.setheading(180) turtle.forward(120) turtle.end_fill() # 畫翅膀 turtle.pu() turtle.goto(-70,-80) turtle.pd() turtle.fillcolor('white') turtle.begin_fill() turtle.setheading(50) turtle.circle(30, 240) turtle.setheading(0) turtle.forward(60) turtle.setheading(180) turtle.circle(-30, 240) turtle.setheading(180) turtle.forward(60) turtle.end_fill() # 畫腳 turtle.pu() turtle.goto(20, -150) turtle.pd() turtle.setheading(270) turtle.pensize(8) turtle.pencolor('orange') turtle.forward(70) turtle.pu() turtle.goto(75, -150) turtle.pd() turtle.forward(70) # 結束繪畫 turtle.done()
運行以上代碼,可以在畫布上看到一個可愛的小雞形狀。在這段代碼中,我們用turtle庫繪制出小雞的頭部、眼睛、嘴巴、身體、翅膀、腳等各個部分,同時調整海龜的狀態和位置。
Python編程可以讓你發揮創意,畫出各種精美的圖形,同時提高編程技能和邏輯思維能力。如果你對這個領域感興趣,不妨開始學習Python編程吧!