一、繪製基礎圖形
在Python中,我們可以使用turtle模塊繪製基礎圖形。我們可以使用turtle.pensize()定義畫筆的粗細,使用turtle.pencolor()定義畫筆的顏色,使用turtle.fillcolor()定義填充顏色。
import turtle #繪製一個實心圓 turtle.pensize(2) #定義畫筆粗細 turtle.fillcolor("red") #定義填充顏色 turtle.begin_fill() #開始填充 turtle.circle(50) #繪製圓形 turtle.end_fill() #結束填充
上述代碼會繪製一個實心圓,其中turtle.circle(50)表示繪製半徑為50的圓形。
二、繪製人物頭部
人物頭部通常是一個橢圓形,我們可以使用turtle模塊的ellipse()方法來繪製橢圓形。
import turtle #繪製頭部 turtle.pensize(2) turtle.fillcolor("#FED7F2") #定義填充顏色 turtle.begin_fill() turtle.ellipse(80,50) #繪製橢圓形 turtle.end_fill()
上述代碼會繪製一個橢圓形作為人物的頭部。
三、繪製人物身體
繪製人物身體通常是一個矩形,使用turtle模塊的rectangle()方法可以很方便地繪製矩形。
import turtle #繪製身體 turtle.pensize(2) turtle.fillcolor("#FFE5B4") #定義填充顏色 turtle.begin_fill() turtle.rectangle(100,50) #繪製矩形 turtle.end_fill()
上述代碼會繪製一個矩形作為人物的身體。
四、繪製人物四肢
人物四肢通常是由直線和圓弧組成,我們可以使用turtle模塊的circle()和forward()方法來繪製。
import turtle #繪製左手 turtle.pensize(2) turtle.penup() #抬起畫筆 turtle.goto(-50,-75) #定位到左手的位置 turtle.pendown() #放下畫筆 turtle.right(35) #將畫筆向右旋轉35度 turtle.fillcolor("#FFE5B4") turtle.begin_fill() turtle.circle(40,120) #繪製左手弧形 turtle.right(110) turtle.forward(50) #繪製左手的直線 turtle.right(110) turtle.circle(-40,120) #繪製左手另一側的弧形 turtle.end_fill() #繪製右手,代碼與左手類似 turtle.penup() turtle.goto(50,-75) turtle.pendown() turtle.right(125) turtle.fillcolor("#FFE5B4") turtle.begin_fill() turtle.circle(-40,120) turtle.right(110) turtle.forward(50) turtle.right(110) turtle.circle(40,120) turtle.end_fill() #繪製左腳 turtle.penup() turtle.goto(-30,-125) turtle.pendown() turtle.left(90) turtle.fillcolor("#FFE5B4") turtle.begin_fill() turtle.circle(30,180) turtle.right(90) turtle.forward(50) turtle.right(90) turtle.circle(30,180) turtle.end_fill() #繪製右腳,代碼與左腳類似 turtle.penup() turtle.goto(30,-125) turtle.pendown() turtle.left(90) turtle.fillcolor("#FFE5B4") turtle.begin_fill() turtle.circle(-30,180) turtle.right(90) turtle.forward(50) turtle.right(90) turtle.circle(-30,180) turtle.end_fill()
上述代碼會繪製一個帶有四肢的人物。
五、繪製人物頭髮和面部特徵
人物頭髮和面部特徵可以使用turtle模塊的pencolor()方法來定義顏色,然後使用ellipse()方法來繪製。
import turtle #繪製頭髮 turtle.pensize(2) turtle.penup() turtle.goto(-40,45) turtle.pendown() turtle.pencolor("black") #定義顏色為黑色 turtle.ellipse(30,50) #繪製頭髮 #繪製左眼 turtle.penup() turtle.goto(-25,25) turtle.pendown() turtle.fillcolor("white") turtle.begin_fill() turtle.circle(10) #繪製眼白 turtle.end_fill() turtle.fillcolor("black") turtle.begin_fill() turtle.circle(5) #繪製眼珠 turtle.end_fill() #繪製右眼,代碼與左眼類似 turtle.penup() turtle.goto(25,25) turtle.pendown() turtle.fillcolor("white") turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.fillcolor("black") turtle.begin_fill() turtle.circle(5) turtle.end_fill() #繪製嘴巴 turtle.penup() turtle.goto(-15,0) turtle.pendown() turtle.pencolor("red") #定義顏色為紅色 turtle.right(90) turtle.circle(15,180) #繪製半圓弧
上述代碼會繪製出帶有頭髮和面部特徵的人物。
六、完整代碼
import turtle #繪製一個實心圓 turtle.pensize(2) turtle.fillcolor("red") turtle.begin_fill() turtle.circle(50) turtle.end_fill() #繪製頭部 turtle.pensize(2) turtle.fillcolor("#FED7F2") turtle.begin_fill() turtle.ellipse(80,50) turtle.end_fill() #繪製身體 turtle.pensize(2) turtle.fillcolor("#FFE5B4") turtle.begin_fill() turtle.rectangle(100,50) turtle.end_fill() #繪製左手 turtle.pensize(2) turtle.penup() turtle.goto(-50,-75) turtle.pendown() turtle.right(35) turtle.fillcolor("#FFE5B4") turtle.begin_fill() turtle.circle(40,120) turtle.right(110) turtle.forward(50) turtle.right(110) turtle.circle(-40,120) turtle.end_fill() #繪製右手 turtle.penup() turtle.goto(50,-75) turtle.pendown() turtle.right(125) turtle.fillcolor("#FFE5B4") turtle.begin_fill() turtle.circle(-40,120) turtle.right(110) turtle.forward(50) turtle.right(110) turtle.circle(40,120) turtle.end_fill() #繪製左腳 turtle.penup() turtle.goto(-30,-125) turtle.pendown() turtle.left(90) turtle.fillcolor("#FFE5B4") turtle.begin_fill() turtle.circle(30,180) turtle.right(90) turtle.forward(50) turtle.right(90) turtle.circle(30,180) turtle.end_fill() #繪製右腳 turtle.penup() turtle.goto(30,-125) turtle.pendown() turtle.left(90) turtle.fillcolor("#FFE5B4") turtle.begin_fill() turtle.circle(-30,180) turtle.right(90) turtle.forward(50) turtle.right(90) turtle.circle(-30,180) turtle.end_fill() #繪製頭髮 turtle.pensize(2) turtle.penup() turtle.goto(-40,45) turtle.pendown() turtle.pencolor("black") turtle.ellipse(30,50) #繪製左眼 turtle.penup() turtle.goto(-25,25) turtle.pendown() turtle.fillcolor("white") turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.fillcolor("black") turtle.begin_fill() turtle.circle(5) turtle.end_fill() #繪製右眼 turtle.penup() turtle.goto(25,25) turtle.pendown() turtle.fillcolor("white") turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.fillcolor("black") turtle.begin_fill() turtle.circle(5) turtle.end_fill() #繪製嘴巴 turtle.penup() turtle.goto(-15,0) turtle.pendown() turtle.pencolor("red") turtle.right(90) turtle.circle(15,180) turtle.done()
原創文章,作者:IHKJO,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/371542.html