Python 是一種高級編程語言,它使用簡單、易讀的語法讓新手輕松上手。Python 特別適合用于圖像處理和數據分析。今天,我們就來學習一下使用 Python 繪制派大星的代碼。
import turtle # 設置畫筆大小和顏色 turtle.pensize(5) turtle.pencolor("purple") # 繪制臉部輪廓 turtle.circle(100) # 繪制眼睛部分 turtle.penup() turtle.goto(35, 130) turtle.pendown() turtle.pencolor("white") turtle.fillcolor("white") turtle.begin_fill() turtle.circle(25) turtle.end_fill() turtle.penup() turtle.goto(-35, 130) turtle.pendown() turtle.begin_fill() turtle.circle(25) turtle.end_fill() turtle.penup() turtle.goto(30, 135) turtle.pendown() turtle.pencolor("black") turtle.fillcolor("black") turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.penup() turtle.goto(-30, 135) turtle.pendown() turtle.begin_fill() turtle.circle(10) turtle.end_fill() # 繪制鼻子部分 turtle.penup() turtle.goto(0, 90) turtle.pendown() turtle.pencolor("black") turtle.fillcolor("red") turtle.begin_fill() turtle.circle(15) turtle.end_fill() # 繪制口部輪廓 turtle.penup() turtle.goto(-70, 30) turtle.pendown() turtle.pencolor("black") turtle.right(90) turtle.circle(70, 180) turtle.penup() turtle.goto(-70, 30) turtle.pendown() turtle.left(90) turtle.circle(-70, 180) # 繪制牙齒部分 turtle.penup() turtle.goto(-50, 30) turtle.pendown() turtle.fillcolor("white") turtle.begin_fill() turtle.right(30) turtle.forward(30) turtle.left(60) turtle.forward(30) turtle.left(120) turtle.forward(30) turtle.end_fill() turtle.penup() turtle.goto(20, 30) turtle.pendown() turtle.fillcolor("white") turtle.begin_fill() turtle.left(30) turtle.forward(30) turtle.right(60) turtle.forward(30) turtle.right(120) turtle.forward(30) turtle.end_fill() # 繪制痣部分 turtle.penup() turtle.goto(80, -20) turtle.pendown() turtle.pencolor("black") turtle.fillcolor("black") turtle.begin_fill() turtle.circle(10) turtle.end_fill() # 完成繪制 turtle.done()
以上就是 Python 繪制派大星的完整代碼。程序使用 turtle 模塊來繪制圖形,首先我們設定了畫筆的大小和顏色,然后通過 turtle.circle 方法繪制圓形輪廓。接著,我們通過 turtle.penup 和 turtle.pendown 方法控制畫筆移動到指定位置,使用 turtle.fillcolor 和 turtle.begin_fill 方法設定填充顏色及開始填充。最后通過 turtle.done 方法來啟動繪制過程,當它執行完成后,程序會一直顯示繪制結果,直到我們手動關閉窗口。