天天看點

用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

用python的turtle庫給你畫一個頭像

    • 使用到的海龜繪圖指令
  • 源代碼
    • 畫布和畫筆設定
    • 畫臉盤子
    • 取名字
    • 畫眼睛
    • 畫鼻子
    • 畫嘴巴
    • 畫耳朵
    • 畫腮紅
  • 小白感想
  • 獻上完整源代碼

小白練習python的海龜作圖,竟為心愛的ta畫下!!

用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

使用到的海龜繪圖指令

指令 說明
turtle.screensize() 設定螢幕寬高及背景顔色
turtle.setup() 設定畫布大小
turtle.hideturtle() 隐藏畫筆
turtle.pensize() 設定畫筆寬度
turtle.speed() 設定畫筆速度
turtle.pencolor() 設定畫筆顔色
turtle.penup() 提筆
turtle.pendown() 放筆,開始畫
turtle.setheading() 調轉畫筆方向
turtle.forward() 畫筆向前走
turtle.right() 畫筆向右轉
turtle.left() 畫筆向左轉
turtle.goto() 跳轉畫筆位置
turtle.textinput() 輸入文本
turtle.write() 列印文本
turtle.circle() 畫給定半徑的圓
turtle.dot() 畫給定直徑的圓點
turtle.done() 将圖像留下畫布上

話不多說,來看源代碼吧~

源代碼

用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

畫布和畫筆設定

import turtle as a
a.screensize(400,300)//設定螢幕大小
a.setup(width=400,height=300)//設定畫布大小
a.pensize(15)//設定畫筆寬度
a.speed(5)//設定畫筆速度
a.hideturtle()//隐藏畫筆
           
用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

畫臉盤子

a.penup()//提筆
a.goto(-100,100)//移動畫筆位置
a.setheading(180)//設定朝向正西
a.pencolor("pink")
a.pendown()//放筆
a.circle(200)
           
用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

取名字

a.penup()//取名
a.goto(-150,10)
yourname=a.textinput("請老實回答","你的名字是?")
name=yourname+"崽崽"
a.pendown()
a.write(name,font=("elephant",25,"bold"))//列印文本
           
用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

畫眼睛

a.penup()//畫左眼
a.goto(-200,0)
a.pendown()
a.circle(25)
a.penup()
a.goto(-200,-14)
a.pendown()
a.circle(9)
a.penup()//光暈
a.goto(-190,-20)
a.pencolor("white")
a.pendown()
a.dot(10)

a.penup()//畫右眼
a.pencolor("pink")
a.goto(0,0)
a.pendown()
a.circle(25)
a.penup()//光暈
a.goto(0,-14)
a.pendown()
a.circle(9)
a.penup()
a.goto(-10,-20)
a.pencolor("white")
a.pendown()
a.dot(10)
           
用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

畫鼻子

a.penup()//畫鼻子
a.speed(10)//設定畫筆速度
a.pencolor("pink")
a.goto(-150,-75)
a.setheading(45)
a.pendown()
for i in range(90):
    a.forward(1.5)
    a.right(1)
for i in range(3): //圓化棱角,每轉16度向前走3個像素
    a.right(16)
    a.forward(3)
a.forward(15)
for i in range(3): //圓化棱角
    a.right(16)
    a.forward(3)
a.setheading(225)
for i in range(90):
    a.forward(1.5)
    a.right(1)
for i in range(3): //圓化棱角
    a.right(16)
    a.forward(3)
a.forward(15)
for i in range(3): //圓化棱角
    a.right(16)
    a.forward(3)
a.penup()
a.speed(5)//設定畫筆速度
a.goto(-125,-70)//第一條杠
a.setheading(270)
a.pendown()
a.forward(50)
a.penup()
a.goto(-70,-65)//第二條杠
a.pendown()
a.forward(55)
           
用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

畫嘴巴

a.penup()//畫嘴巴
a.speed(10)//設定畫筆速度
a.goto(-135,-165)
a.setheading(305)
a.pendown()
for i in range(120):
    a.forward(1)
    a.left(1)    
           
用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

畫耳朵

a.penup()//畫右耳朵
a.speed(5)
a.setheading(0)
a.goto(-17,90)
a.pendown()
a.forward(60)
a.penup()
a.goto(28,75)//跳到下一筆起始位置
a.setheading(45)
a.pendown()
a.forward(110)
a.right(45)
a.forward(40)
a.setheading(225)
a.forward(40)
a.setheading(270)
for i in range(7): //圓化棱角
    a.right(2.5)
    a.forward(10)
a.forward(80)

a.penup()//畫左耳朵
a.goto(-183,90)
a.setheading(180)
a.pendown()
a.forward(60)
a.penup()
a.goto(-230,75)//跳到下一筆起始位置
a.setheading(135)
a.pendown()
a.forward(110)
a.left(45)
a.forward(40)
a.setheading(-45)
a.forward(40)
a.setheading(270)
for i in range(10): //圓化棱角
    a.left(2.5)
    a.forward(15)
           
用python的turtle庫給你畫一個頭像(豬頭)引源代碼小白感想獻上完整源代碼

畫腮紅

a.penup()//畫左腮紅
a.pencolor("tomato")//設定成番茄色
a.goto(-250,-100)
a.setheading(270)
a.pendown()
a.forward(20)
a.penup()
a.goto(-210,-100)
a.pendown()
a.forward(20)

a.penup()//畫右腮紅
a.goto(10,-100)
a.pendown()
a.forward(20)
a.penup()
a.goto(50,-100)
a.pendown()
a.forward(20)
           

小白感想

雖然圖很簡單,但還是廢了不少功夫,特别是在對坐标位置确定上;而且感覺代碼有點太長了,可能是有些地方做的不夠精簡。繼續加油!

獻上完整源代碼

#!/usr/bin/env python3.7 #指明用什麼可執行程式運作這個檔案代碼
#-*- coding:UTF-8 -*-   #保證能順利解析中文
#author:Boosirit time:2020/4/5

import turtle as a
a.screensize(400,300)#設定螢幕大小
a.setup(width=400,height=300)#設定畫布大小
a.pensize(15)#設定畫筆寬度
a.speed(5)#設定畫筆速度
a.hideturtle()#隐藏畫筆

a.penup()#畫臉
a.goto(-100,100)
a.setheading(180)#設定朝向正西
a.pencolor("pink")
a.pendown()
a.circle(200)

a.penup()#取名
a.goto(-150,10)
yourname=a.textinput("請老實回答","你的名字是?")
name=yourname+"崽崽"
a.pendown()
a.write(name,font=("elephant",25,"bold"))#列印文本

a.penup()#畫左眼
a.goto(-200,0)
a.pendown()
a.circle(25)
a.penup()
a.goto(-200,-14)
a.pendown()
a.circle(9)
a.penup()#光暈
a.goto(-190,-20)
a.pencolor("white")
a.pendown()
a.dot(10)

a.penup()#畫右眼
a.pencolor("pink")
a.goto(0,0)
a.pendown()
a.circle(25)
a.penup()#光暈
a.goto(0,-14)
a.pendown()
a.circle(9)
a.penup()
a.goto(-10,-20)
a.pencolor("white")
a.pendown()
a.dot(10)

a.penup()#畫鼻子
a.speed(10)#設定畫筆速度
a.pencolor("pink")
a.goto(-150,-75)
a.setheading(45)
a.pendown()
for i in range(90):
    a.forward(1.5)
    a.right(1)
for i in range(3):#圓化棱角,每轉16度向前走3個像素
    a.right(16)
    a.forward(3)
a.forward(15)
for i in range(3):#圓化棱角
    a.right(16)
    a.forward(3)
a.setheading(225)
for i in range(90):
    a.forward(1.5)
    a.right(1)
for i in range(3):#圓化棱角
    a.right(16)
    a.forward(3)
a.forward(15)
for i in range(3):#圓化棱角
    a.right(16)
    a.forward(3)
a.penup()
a.speed(5)#設定畫筆速度
a.goto(-125,-70)#第一條杠
a.setheading(270)
a.pendown()
a.forward(50)
a.penup()
a.goto(-70,-65)#第二條杠
a.pendown()
a.forward(55)

a.penup()#畫嘴巴
a.speed(10)#設定畫筆速度
a.goto(-135,-165)
a.setheading(305)
a.pendown()
for i in range(120):
    a.forward(1)
    a.left(1)
    
a.penup()#畫右耳朵
a.speed(5)
a.setheading(0)
a.goto(-17,90)
a.pendown()
a.forward(60)
a.penup()
a.goto(28,75)#跳到下一筆起始位置
a.setheading(45)
a.pendown()
a.forward(110)
a.right(45)
a.forward(40)
a.setheading(225)
a.forward(40)
a.setheading(270)
for i in range(7):#圓化棱角
    a.right(2.5)
    a.forward(10)
a.forward(80)

a.penup()#畫左耳朵
a.goto(-183,90)
a.setheading(180)
a.pendown()
a.forward(60)
a.penup()
a.goto(-230,75)#跳到下一筆起始位置
a.setheading(135)
a.pendown()
a.forward(110)
a.left(45)
a.forward(40)
a.setheading(-45)
a.forward(40)
a.setheading(270)
for i in range(10):#圓化棱角
    a.left(2.5)
    a.forward(15)
    
a.penup()#畫左腮紅
a.pencolor("tomato")#設定成番茄色
a.goto(-250,-100)
a.setheading(270)
a.pendown()
a.forward(20)
a.penup()
a.goto(-210,-100)
a.pendown()
a.forward(20)

a.penup()#畫右腮紅
a.goto(10,-100)
a.pendown()
a.forward(20)
a.penup()
a.goto(50,-100)
a.pendown()
a.forward(20)

a.done()#留存圖像在畫布上

           

愛ta,就給ta畫個豬頭頭像吧!