1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python turtle 画老鼠_Python Turtle绘图 鼠年画老鼠爷

python turtle 画老鼠_Python Turtle绘图 鼠年画老鼠爷

时间:2023-08-20 13:28:16

相关推荐

python turtle 画老鼠_Python Turtle绘图 鼠年画老鼠爷

Python Turtle绘图 鼠年画老鼠爷

前言

效果图

必要知识

源代码

后记

前言

本文介绍的是如何运用Python的turtle画一只老鼠。为什么会无聊想到做这个呢?还不是想获得24小时的请假时长学会turtle,掌握技术。

这是我们系举办的一个“对称图形编程大赛活动”,此次做这个初期也只是为了参赛,但做一件事情都要把它做好嘛,所以花费了一些时间和精力。

由于近期在自学Python,觉得turtle挺有意思的,刚好今年是鼠年,就想到用turtle画只老鼠。

由于本人画画功底渣+刚入门的小白,所以画出来的老鼠略微丑萌,与实际参考图有一定差距,但是没关系,自己女朋友看着开心就行!

在画之前,先参考了画小猪佩奇的方法,有了初步的理解和认识。网上有很多优秀的作品也可供大家参考。

通过这项活动我浅学会了Python Turtle这个神奇又强大的工具,操作起来真的就跟画笔没两样,牛批!

代码不难,难得是要找角度,不过这玩意还是很好入门的。博主愚钝,在女朋友十万分的鼓励和不断提出修改建议的情况下,敲了好久才有这只小老鼠。

所以哈,大家会儿借鉴的时候,稍微,就稍微啊,标一下出处。感恩啊!

效果图

下面先来看一下效果图吧,丑萌丑萌的,勿喷(开心就好)。

必要知识

要想画出老鼠,必须掌握以下知识,也是一些turtle的基本操作,下面我对知识点做了一些说明,便于大家读懂。

命令

说明

turtle.right(degree) 简称:rt()

向右转degree°

turtle.left(degree) 简称:lt()

向左转degree°

turtle.forward(dis) 简称:fd()

向当前画笔方向移动dis像素长度

turtle.backward(dis) 简称:bd()

向当前画笔相反方向移动dis像素长度

turtle.penup() 简称:pu()

提起笔移动,不绘制图形,用于另起一个地方绘制

turtle.pendown() 简称:pd()

移动时绘制图形,缺省时也为绘制

turtle.goto(x,y)

将画笔移动到坐标为x,y的位置

turtle.fillcolor(colorstring)

绘制图形的填充颜色

turtle.color(color1, color2)

同时设置pencolor=color1, fillcolor=color2

turtle.setheading(angle) 简称:seth()

设置当前朝向为angle角度

turtle.circle(radius, extent=None, steps=None)

radius(半径):半径为正(负),表示圆心在画笔的左边(右边)画圆;extent(弧度) (optional);steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)。

源代码

大家心心念念的源代码来了,感兴趣的小伙伴可以自行下载运行,编程中的快乐“鼠”于你!

我在代码中标明了注释,大家可以根据注释来理解代码的含义。

"""

作品名称:运鼠帷幄

前言:以下内容都是作者一点点测试出来的,如需转载,请附明源出处!

"""

import turtle as t

t.screensize(300,600)

def ears(dir): # 耳朵,dir用来设置方向,左右耳对称

#大圈

t.pu()

t.goto((0 - dir) * 150, 120)

t.setheading(0)

t.pd()

t.fillcolor('#F2D391')

t.begin_fill()

t.circle(80)

t.end_fill()

#小圈

t.pu()

t.goto((0 - dir) * 120, 120)

t.setheading(0)

t.pd()

t.fillcolor('#F9ECF5')

t.begin_fill()

t.circle(60)

t.end_fill()

def face(): # 画脸

#加底图

t.pu()

t.goto(-45,216)

t.pd()

t.color("#FFE4B5","#FFE4B5")#前一个是笔的颜色,后一个是填充的颜色

t.begin_fill()

t.goto(45,216)

t.goto(65, -20)

t.goto(-65, -20)

t.end_fill()

t.color("black","#FFE4B5")#底图增加完毕,笔的颜色置回黑色

#右边脸颊

t.pu()

t.goto(65,-20)

t.pd()

t.fillcolor('#FFE4B5')

t.begin_fill()

t.setheading(10)

t.circle(120,180)

#左边脸颊

t.pu()

t.goto(-65,-20)

t.pd()

t.setheading(170)

t.circle(-120,180)

#下巴

t.pu()

t.goto(-65,-20)

t.pd()

t.goto(65,-20)

t.end_fill()

def mouth(): # 嘴巴

#右边嘴巴

t.pu()

t.goto(0, 30)

t.pd()

t.setheading(-70)

t.circle(30,180)

#左边嘴巴

t.pu()

t.goto(0, 30)

t.pd()

t.setheading(-110)

t.circle(-30,180)

def eyes(dir): # 画眼睛,dir用来设置方向,左右眼对称

#大圈

t.pu()

t.goto((0 - dir) * 30, 140)

t.setheading(90)

t.pd()

t.fillcolor('white')

t.begin_fill()

t.circle(dir * 30)

t.end_fill()

#小圈

t.pu()

t.goto((0 - dir) * 40, 135)

t.setheading(90)

t.pd()

t.fillcolor('#AAC9E3')

t.begin_fill()

t.circle(dir * 17)

t.end_fill()

#小小圈

t.pu()

t.goto((0 - dir) * 45, 135)

t.setheading(90)

t.pd()

t.color("white","white")

t.begin_fill()

t.circle(dir * 5)

t.end_fill()

t.color("black","white")

def nose(): # 画鼻子

t.pu()

t.goto(14, 80)

t.setheading(90)

t.pd()

t.fillcolor('red')

t.begin_fill()

t.circle(14)

t.end_fill()

def beard(): #画胡须

#右边胡须

t.pu()

t.goto(90, 80)

t.pd()

t.setheading(-15)

t.fd(150)

t.pu()

t.goto(90, 60)

t.pd()

t.setheading(-30)

t.fd(150)

#左边胡须

t.pu()

t.goto(-90, 80)

t.pd()

t.setheading(-165)

t.fd(150)

t.pu()

t.goto(-90, 60)

t.pd()

t.setheading(-150)

t.fd(150)

def hat(): #帽子

#小的半圆

t.pu()

t.goto(50, 265)

t.pd()

t.setheading(90)

t.fillcolor('red')

t.begin_fill()

t.circle(50,180)

t.end_fill()

#大的半圆

t.fillcolor('red')

t.begin_fill()

t.pu()

t.setheading(0)

t.goto(-80, 210)

t.pd()

t.fd(160)

t.setheading(90)

t.circle(80,180)

t.end_fill()

#铜钱大圆

t.fillcolor('yellow')

t.begin_fill()

t.pu()

t.goto(0, 220)

t.setheading(0)

t.pd()

t.circle(30)

t.end_fill()

#铜钱小方块

t.fillcolor('red')

t.begin_fill()

t.pu()

t.goto(-15, 235)

t.setheading(0)

t.pd()

t.fd(30)

t.lt(90)

t.fd(30)

t.lt(90)

t.fd(30)

t.lt(90)

t.fd(30)

t.end_fill()

def clothes(): #衣服

#下边衣角

t.pu()

t.goto(-120,-168)

t.pd()

t.setheading(-90)

t.fillcolor('red')

t.begin_fill()

t.fd(40)

t.lt(90)

t.fd(240)

t.lt(90)

t.fd(40)

t.end_fill()

#加底图

t.color("red","red")##+42

t.pu()

t.goto(-110, -3)

t.pd()

t.begin_fill()

t.goto(-110, -188)

t.goto(110, -188)

t.goto(110, -3)

t.end_fill()

t.color("black","red")

#左边袖子

t.pu()

t.goto(-110, -3)

t.pd()

t.setheading(-150)

t.begin_fill()

t.circle(100,160)

t.setheading(90)

t.fd(100)

t.lt(60)

t.fd(70)

t.end_fill()

#右边袖子

t.pu()

t.goto(110, -3)

t.pd()

t.setheading(-30)

t.begin_fill()

t.circle(-100,160)

t.setheading(90)

t.fd(100)

t.rt(60)

t.fd(70)

t.end_fill()

#中间条纹

t.pu()

t.goto(-20, -8)

t.pd()

t.fillcolor('yellow')

t.begin_fill()

t.setheading(-90)

t.fd(200)

t.lt(90)

t.fd(40)

t.lt(90)

t.fd(200)

t.lt(90)

t.fd(40)

t.end_fill()

#袖口条纹

t.pu()

t.goto(-43, -188)

t.pd()

t.setheading(90)

t.begin_fill()

t.fd(100)

t.lt(60)

t.fd(40)

t.setheading(-90)

t.fd(120)

t.end_fill()

t.pu()

t.goto(43, -188)

t.pd()

t.setheading(90)

t.begin_fill()

t.fd(100)

t.rt(60)

t.fd(40)

t.setheading(-90)

t.fd(120)

t.end_fill()

def hand(): #手

#左手

t.pu()

t.goto(-43, -168)

t.pd()

t.setheading(0)

t.fillcolor('#FFE4B5')

t.begin_fill()

t.circle(40,180)

t.end_fill()

#左手手指条纹

t.pu()

t.goto(-5, -118)

t.pd()

t.fd(28)

t.pu()

t.goto(-3, -128)

t.pd()

t.fd(30)

t.pu()

t.goto(-5, -138)

t.pd()

t.fd(28)

#右手

t.pu()

t.goto(43, -88)

t.pd()

t.setheading(180)

t.begin_fill()

t.circle(40,180)

t.end_fill()

#右手手指条纹

t.pu()

t.goto(5, -118)

t.pd()

t.fd(28)

t.pu()

t.goto(3, -128)

t.pd()

t.fd(30)

t.pu()

t.goto(5, -138)

t.pd()

t.fd(28)

def trousers(): #裤子

#左边裤子

t.pu()

t.goto(-110,-208)

t.pd()

t.setheading(-100)

t.fillcolor('red')

t.begin_fill()

t.fd(100)

t.setheading(0)

t.fd(90)

t.goto(0,-208)

#右边裤子

t.pu()

t.goto(110,-208)

t.pd()

t.setheading(-80)

t.fd(100)

t.setheading(180)

t.fd(90)

t.goto(0,-208)

t.end_fill()

def shoes(): #鞋子

#左边鞋子

t.pu()

t.goto(-125,-307)

t.pd()

t.setheading(-90)

t.fillcolor('yellow')

t.begin_fill()

t.circle(40,180)

t.end_fill()

#左边鞋子条纹

t.pu()

t.goto(-85,-347)

t.pd()

t.setheading(90)

t.fd(30)

#右边鞋子

t.pu()

t.goto(125,-307)

t.pd()

t.setheading(-90)

t.begin_fill()

t.circle(-40,180)

t.end_fill()

#右边鞋子条纹

t.pu()

t.goto(85,-347)

t.pd()

t.setheading(90)

t.fd(30)

def Originator(): #就系我啦

t.pu()

t.goto(0,-350)

t.pd()

t.write("作者:二君爱读书", move=False, align="center", font=("Arial", 10, "normal"))

t.pu()

t.goto(0,-360)

t.pd()

t.setheading(90)

t.pensize(2)

clothes()

hand()

trousers()

shoes()

ears(1)

ears(-1)

face()

mouth()

eyes(1)

eyes(-1)

nose()

beard()

hat()

Originator()

t.done()

后记

初稿只画了老鼠头,觉得画得不像,害怕大家认不出来,但询问身边的好友,大家都猜出来了哈哈出乎意料!

敲了好久才有这只小老鼠。女朋友说长得特别像老鼠爷,哈哈哈也算不负她望,圆满了!希望自己能获个小奖哈,到时候向大家公布!

本人是新手小白。有不足的地方欢迎各位大佬指正,大家共同交流学习。在春节之际,祝愿大家year year no bug!

作者:二君爱读书

日期:1月21日

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。