天天看点

用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画个猪头头像吧!