用python畫豬頭的方法:首先設(shè)置畫布和畫筆,代碼為【a.screensize(400,300)a.setup(width=400,height=300)】;然后畫臉型,代碼為【.goto(-100,100)】;最后留存圖像在畫布上即可。
【相關(guān)學(xué)習(xí)推薦:python視頻】
用python畫豬頭的方法:
畫布和畫筆設(shè)置
import turtle as aa.screensize(400,300)//設(shè)置屏幕大小a.setup(width=400,height=300)//設(shè)置畫布大小a.pensize(15)//設(shè)置畫筆寬度a.speed(5)//設(shè)置畫筆速度a.hideturtle()//隱藏畫筆畫臉盤子
a.penup()//提筆a.goto(-100,100)//移動(dòng)畫筆位置a.setheading(180)//設(shè)置朝向正西a.pencolor("pink")a.pendown()//放筆a.circle(200)取名字
a.penup()//取名a.goto(-150,10)yourname=a.textinput("請(qǐng)老實(shí)回答","你的名字是?")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)//設(shè)置畫筆速度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): //圓化棱角,每轉(zhuǎn)16度向前走3個(gè)像素 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)//設(shè)置畫筆速度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)//設(shè)置畫筆速度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")//設(shè)置成番茄色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() //留存圖像在畫布上獻(xiàn)上完整源代碼
#!/usr/bin/env python3.7 #指明用什么可執(zhí)行程序運(yùn)行這個(gè)文件代碼#-*- coding:utf-8 -*- #保證能順利解析中文#author:boosirit time:2020/4/5import turtle as aa.screensize(400,300)#設(shè)置屏幕大小a.setup(width=400,height=300)#設(shè)置畫布大小a.pensize(15)#設(shè)置畫筆寬度a.speed(5)#設(shè)置畫筆速度a.hideturtle()#隱藏畫筆a.penup()#畫臉a.goto(-100,100)a.setheading(180)#設(shè)置朝向正西a.pencolor("pink")a.pendown()a.circle(200)a.penup()#取名a.goto(-150,10)yourname=a.textinput("請(qǐng)老實(shí)回答","你的名字是?")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)#設(shè)置畫筆速度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):#圓化棱角,每轉(zhuǎn)16度向前走3個(gè)像素 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