turtle的使用以及画小黄人

turtle的使用以及画小黄人

在使用turtle之前需要导入turtle库 导入方式如下:

import turtle

以下是在使用turtle的时候一些常用代码:

# 创建画布
# 编辑画布大小
turtle.setup(888,888)
隐藏笔头的图片
turtle.hideturtle() #或者可以用简写 turtle.ht()
turtle.showturtle() # turtle.st()
# 设置笔
turtle.pencolor('orange') #修改笔的颜色(默认是黑色)
# 设置线条的宽细
turtle.width(3)
# 设置笔的移动速度 1~10由慢及快  0最快
turtle.speed(2)

# 移动笔
# 前进
turtle.forward(100) #forward(100)或者fd(100)括号里的数字表示移动距离
turtle.fd(100)

# 后退
turtle.back(300)
turtle.bk(100)

# 移动到指定位置
turtle.goto(0,200) #移动到(0,200)
turtle.goto(200,0) #移动到(200,0)

# 修改坐标
turtle.setx(300) #y轴不变修改x轴
turtle.sety(-100) #x轴不变修改y轴

#坐标回到原点
turtle.home()

# 隐藏笔头
turtle.hideturtle()
# 笔的宽度&笔移动的速度
turtle.width(10)
turtle.speed(2)

# 改变笔的方向(默认向右)
turtle.left(45) #向左旋转90°
turtle.fd(100)

turtle.right(90) #向右旋转180°
turtle.fd(200)

# 设置成指定的角度
turtle.seth(90) #设置笔的角度
turtle.fd(100)

# 画圆环
turtle.circle(50) #画一个半径为50的圆
turtle.circle(50, 180)#画圆环 这里表示画一个半径为50,弧度为180°的圆环    数值可以改效果也不一样

# 画实习圆
turtle.dot(100) #指画一个直径为100的实心圆,!!!!这里是直径
# 需要注意的是圆环是在笔的起始位置逆时针画   而实心圆是以笔的位置为半径直接画实心圆

# 抬笔和放笔
turtle.up() #抬起笔头
turtle.down() #放下笔头

# 填充颜色
turtle.fillcolor('yellow') #设置需要填充的颜色
turtle.begin_fill() #开始填充颜色
turtle.end_fill() #结束填充颜色

如果需要画一些其他的图像那么我们就要进行循环去操作

例如画曲线:

# 原理
# 前进的过程中不断的改变笔的方向
import turtle
turtle.pencolor('yellow')
turtle.speed(5)
turtle.width(3)


for i in range(5):
    turtle.fd(100)
    turtle.left(-144)


例如画椭圆:

import turtle

pen = turtle.Turtle()  # 定义画笔实例
a = 1
for i in range(120):
 if 0 <= i < 30 or 60 <= i < 90:  # 控制a的变化
  a = a + 0.3
  pen.lt(3)  # 向左转3度
  pen.fd(a)  # 向前走a的步长
 else:
  a = a - 0.3
  pen.lt(3)
  pen.fd(a)

print(pen)
turtle.mainloop()

这种椭圆方式不是太正规 ,也只是看上去像椭圆 具体的椭圆方式我还没有学到 大家可以参考其他博客进行学习

下面是我们上课画了有意思的东西可以给大家:

小黄人:

import turtle
turtle.setup(800,800)
#turtle.hideturtle()
turtle.speed(5)
turtle.width(1)

# 大轮廓
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.up()
turtle.goto(150,150)
turtle.down()
turtle.left(90)
turtle.circle(150,180)
turtle.fd(300)
turtle.circle(150,180)
turtle.fd(300)
turtle.end_fill()



# 眼镜框
turtle.up()
turtle.goto(0,150)
turtle.down()
turtle.width(3)
turtle.fillcolor('white')
turtle.begin_fill()
turtle.circle(40)
turtle.circle(-40)
turtle.end_fill()
turtle.up()
turtle.setx(80)
turtle.down()
turtle.right(90)
turtle.width(15)
turtle.fd(70)
turtle.up()
turtle.setx(-80)
turtle.down()
turtle.fd(-70)

# 眼睛
turtle.up()
turtle.setx(-50)
turtle.dot(40)
turtle.up()
turtle.setx(30)
turtle.dot(40)
turtle.pencolor('white')
turtle.up()
turtle.setx(-41)
turtle.dot(16)
turtle.up()
turtle.setx(39)
turtle.dot(16)
turtle.down()


# 嘴巴
turtle.pencolor('red')
turtle.width(3)
turtle.up()
turtle.goto(-50,50)
turtle.right(45)
turtle.down()
turtle.circle(70,90)


# 衣服  176185
turtle.pencolor('black')
turtle.fillcolor('#176185')
turtle.begin_fill()
turtle.right(45)
turtle.up()
turtle.goto(-150,-150)
turtle.down()
turtle.width(1)
turtle.fd(50)
turtle.goto(-100,-100)
turtle.fd(200)
turtle.goto(100,-150)
turtle.fd(50)
turtle.right(90)
turtle.circle(-150,180)
turtle.end_fill()
turtle.up()
turtle.goto(-90,-100)
turtle.down()
turtle.fillcolor('#176185')
turtle.begin_fill()
turtle.goto(-150,-35)
turtle.bk(15)
turtle.goto(-100,-110)
turtle.goto(-90,-100)
turtle.end_fill()
turtle.up()
turtle.goto(90,-100)
turtle.down()
turtle.fillcolor('#176185')
turtle.begin_fill()
turtle.goto(150,-35)
turtle.bk(15)
turtle.goto(100,-110)
turtle.goto(90,-100)
turtle.end_fill()

# 口袋
turtle.up()
turtle.goto(-50,-130)
turtle.down()
turtle.width(3)
turtle.goto(50,-130)
turtle.bk(30)
turtle.circle(50,-180)
turtle.bk(30)


# 头发
turtle.up()
turtle.goto(0,300)
turtle.down()
turtle.bk(50)
turtle.up()
turtle.goto(10,299)
turtle.down()
turtle.goto(13,340)
turtle.up()
turtle.goto(-10,299)
turtle.down()
turtle.goto(-14,340)
turtle.up()
turtle.goto(-15,298)
turtle.down()
turtle.goto(-17,335)
turtle.up()
turtle.goto(15,298)
turtle.down()
turtle.goto(18,330)

turtle.mainloop()

谢谢大家! 希望对大家有帮助

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值