python小绵羊怎么画_使用Python的turtle画小绵羊

本文介绍如何使用Python的turtle模块绘制2D小绵羊,虽然原计划是实现3D效果,但通过详细步骤展示了从羊头到身体、眼睛、嘴巴等各个部分的画法,包括圈函数的使用技巧。
摘要由CSDN通过智能技术生成

今天学习使用turtle画图,本来想实现个3D效果,结果2D都画了半天,画圆被绕晕了

目标图片:

c306a89b439cdfdac81c0beb86e23937.png

实现代码:

#-*- coding:utf-8 -*-#__author__ :kusy#__content__:文件说明#__date__:2018/8/21 13:08

importturtleimporttimeimportmath as mclassSheep(object):def __init__(self,xsize):

self.t=turtle.Turtle()

self.xsize=xsize

t=self.t#画笔设置

t.screen.screensize(canvwidth=1000,canvheight=500,bg='white')

t.pensize(2)

t.speed(5)

t.hideturtle()#初始化画笔位置

t.penup()

t.setpos(self.xsize,0)

t.pendown()#设置画笔坐标

defsetxy(self,x,y):

t=self.t

t.penup()

pos_x=t.position()[0]

pos_y= t.position()[1]

t.setpos(pos_x+ x,pos_y +y)

t.pendown()defcreate_sheep(self):

t=self.t#羊头

self.setxy(-200,0)

t.fillcolor('black')

t.begin_fill()

t.circle(100)

t.end_fill()#眼睛

#眼白

print(t.position())

self.setxy(-30,120)

t.fillcolor('white')

t.begin_fill()

t.seth(45)

t.circle(19,-280)

t.seth(45)

t.circle(-25,295)

t.end_fill()#眼珠

self.setxy(3,12)

t.fillcolor('black')

t.begin_fill()

t.seth(85)

t.circle(10)

t.seth(85)

t.circle(-10)

t.end_fill()#眼心

t.fillcolor('white')

t.begin_fill()

t.seth(85)

t.circle(3)

t.seth(85)

t.circle(-3)

t.end_fill()#嘴

self.setxy(-10,-50)

t.seth(300)

t.forward(5)

t.seth(0)

t.circle(120,20)

t.seth(120)

t.forward(5)#耳朵

#略

#身体

self.setxy(10,100)

t.seth(45)

t.circle(-150,120)

t.seth(30)

t.circle(-100,30)

t.seth(60)

t.circle(-15,320)

t.seth(330)

t.circle(-80,180)

t.seth(210)

t.circle(-180,70)#4条腿

for leg in range(4):

self.setxy(10+20*leg,-10)

t.seth(270)

t.forward(50)

t.seth(0)

t.forward(5)

t.seth(90)

t.forward(50)if __name__ == '__main__':for x in (0,300):

sheep=Sheep(x)

sheep.create_sheep()

time.sleep(5)

实际效果(2只羊):

dcd1779e84ab96d20e53ae71b4bb0270.png

PS:circle函数使用记录

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

描述: 以给定半径画圆

参数:

radius(半径); 半径为正(负),表示圆心在画笔的左边(右边)画圆

extent(弧度) (optional);

steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)

radius > 0 ,逆时针画圆

radius < 0,顺时针画圆

extent > 0,取正方向的圆

extent < 0,取反方向的圆

参考资料

https://ibaotu.com/sucai/187303.html

https://www.cnblogs.com/nowgood/p/turtle.html#_nav_6

https://www.cnblogs.com/yu-liang/p/9166003.html

https://blog.csdn.net/qq_33733970/article/details/80373247

https://www.jb51.net/article/130181.htm

https://www.jb51.net/article/139349.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值