学习目标
python具有很多方便的工具库
一旦拥有,轻松出效果
本节内容目标:
- 体验模块的用法
- 自定义绘图
- 直接使用代码一键出效果图
了解turtle模块
-
翻译turtle
-
turtle神龟
乌龟绘图坐标系
导入模块
import 模块名
模块即工具包
在当前的文件中使用了导入模块的语句
相当于把工具包拿到了身边
然后需要什么功能,直接使用工具包中的相关工具即可
使用工具
语法
模块名.工具名(参数)
注:有的工具需要参数,有的工具不需要参数
turtle模块中的’工具’简览
接下来是绘图的时间
使用工具包中的一些工具来完成
相关工具功能如下:
导入turtle库
?
import turtle
移动到坐标100,100
?
turtle.goto(100,100)
画一个半径为30的圆形
?
turtle.circle(30)
!
默认是逆时针画
画一个半径为30的圆形,只画一半
?
turtle.circle(30,180)
!
参数二是角度
向前100
?
turtle.fd(100)
turtle.forward(100)
向后100
?
turtle.bk(100)
turtle.backward(100)
向左转45度
?
turtle.left(45)
向右转90度
?
turtle.right(90)
绘制一个填充色为蓝色的圆形
?
turtle.fillcolor("blue")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
画笔提起,让乌龟飞起来
?
turtle.penup()
画笔放下,让乌龟落地
?
turtle.pendown()
让画布停住
?
turtle.done()
更详细的turtle相关的使用方法,可参看:
https://www.cnblogs.com/Archer-Xin/p/12215490.html
练习:自定义绘图
- 绘一个圆形
- 绘一个三角形
- 绘一个长方形
大中型绘图
五角星
import turtle
import time
turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("red")
turtle.begin_fill()
for _ in range(5):
turtle.forward(200)
turtle.right(144)
turtle.end_fill()
time.sleep(2)
turtle.penup()
turtle.goto(-150, -120)
turtle.color("violet")
turtle.write("Done", font=('Arial', 40, 'normal'))
turtle.mainloop()
圆形图堆叠
#coding=utf-8
import turtle
spiral=turtle.Turtle()
ninja=turtle.Turtle()
ninja.speed(10)
for i in range(100):
ninja.forward(100)
ninja.right(30)
ninja.forward(20)
ninja.left(60)
ninja.forward(50)
ninja.penup()
ninja.setposition(0,0)
ninja.pendown()
ninja.right(2)
玫瑰花
import turtle
#渐大
def increases(a,z,f):
#a:画笔起始大小;z:画笔终止大小;f:渐变拉伸距离
for i in range(a,z):
p.pensize(i)
p.forward(f)
#渐小
def smaller(a,z,f):
for i in range(a,z,-1):
p.pensize(i)
p.forward(f)
#花蕊
def flower():
#右下
p.up()
p.home()
p.goto(0,0)
p.pencolor('red')
p.left(15)
p.down()
increases(1,7,5)
p.circle(50,70)
p.forward(60)
p.circle(-100,15)
smaller(7,1,5)
#左下
p.up()
p.home()
p.goto(-20,0)
p.left(180)
p.down()
increases(1,7,5)
p.circle(-60,85)
p.forward(60)
p.circle(100,15)
smaller(7,1,5)
#右边
p.up()
p.home()
p.goto(80,250)
p.left(10)
p.down()
increases(1,5,5)
p.circle(-20,120)
p.circle(-130,20)
p.forward(50)
p.circle(100,15)
smaller(5,1,6)
#左边
p.up()
p.home()
p.goto(-110,240)
p.left(180)
p.down()
increases(1,5,5)
p.circle(30,130)
p.circle(130,15)
p.forward(20)
p.circle(-100,35)
smaller(5,1,7)
#左上
p.up()
p.home()
p.goto(0,270)
p.left(150)
p.down()
increases(1,5,5)
p.circle(60,120)
p.circle(60,30)
p.circle(-50,25)
smaller(5,1,5)
#右上
p.up()
p.home()
p.goto(8,271)
p.left(10)
p.down()
increases(1,5,5)
p.circle(-40,80)
p.circle(-30,90)
p.forward(5)
p.circle(250,25)
smaller(4,1,6)
#右中
p.up()
p.home()
p.goto(65,215)
p.left(-95)
p.down()
increases(1,5,5)
p.circle(200,6)
smaller(5,1,7)
#顶右1
p.up()
p.home()
p.goto(-10,260)
p.left(10)
p.down()
increases(1,5,5)
p.circle(-25,120)
p.circle(-20,40)
p.forward(15)
smaller(4,1,6)
#顶右2
p.up()
p.home()
p.goto(-20,240)
p.left(10)
p.down()
increases(1,5,5)
p.circle(-10,200)
smaller(4,1,6)
#顶左1
p.up()
p.home()
p.goto(-20,255)
p.left(165)
p.down()
increases(1,5,5)
p.forward(10)
p.circle(35,190)
p.circle(90,25)
smaller(4,1,5)
#顶左2
p.up()
p.home()
p.goto(-25,240)
p.left(170)
p.down()
increases(1,5,5)
p.circle(15,230)
smaller(4,1,6)
def leaf():
#叶子
#左1
p.pencolor('Green')
p.up()
p.home()
p.goto(-80,0)
p.left(220)
p.down()
increases(1,5,5)
p.circle(80,50)
p.circle(-80,60)
smaller(4,1,5)
#左2
p.right(210)
increases(1,5,5)
p.circle(70,80)
p.circle(-100,40)
smaller(4,1,5)
#左3
p.right(100)
increases(1,5,5)
p.circle(-200,40)
smaller(4,1,5)
#左4
p.left(155)
increases(1,5,5)
p.circle(200,45)
smaller(4,1,5)
#右1
p.up()
p.home()
p.goto(45,8)
p.right(45)
p.down()
increases(1,5,5)
p.circle(-300,20)
p.circle(100,40)
smaller(4,1,5)
#右2
p.left(200)
increases(1,5,5)
p.circle(-100,60)
p.circle(70,20)
smaller(5,1,7)
#小叶
p.up()
p.home()
p.goto(70,30)
p.left(20)
p.down()
increases(1,5,5)
p.circle(50,30)
smaller(4,1,5)
p.right(150)
increases(1,5,5)
p.circle(-50,70)
smaller(4,1,5)
#花柄
p.up()
p.home()
p.goto(-30,-60)
p.down()
p.right(80)
increases(1,5,5)
p.circle(-700,20)
p.fd(60)
smaller(4,1,5)
p.up()
p.home()
p.goto(10,-170)
p.down()
p.right(90)
increases(1,5,5)
p.circle(-700,10)
p.fd(55)
smaller(4,1,5)
#刺
p.up()
p.home()
p.goto(-25,-250)
p.down()
p.left(125)
increases(1,5,5)
p.fd(10)
smaller(5,1,5)
p.left(165)
increases(1,5,5)
p.fd(40)
smaller(5,1,5)
#画布设置
s = turtle.Screen()
s.bgcolor('LightYellow')
s.setup(1400,800)
s.title('送你花花')
s.tracer(1,10)
#画笔设置
p = turtle.Turtle()
p.shape('turtle')
p.speed('fastest')
p.ht()
flower()
leaf()
s.mainloop()
时钟
import turtle as t
import datetime as dt
#画出背景
game = t.Screen()
game.bgcolor("white")
game.setup(600,600)
game.tracer(0)
#定义画笔属性
pen = t.Turtle()
pen.speed(10)
pen.ht()
pen.up()
def draw_clock(h,m,s):
#画圈
pen.clear()
pen.up()
pen.color("black")
pen.pensize(3)
pen.seth(0)
pen.goto(0,-210)
pen.down()
pen.circle(210)
#画刻度
pen.up()
pen.goto(0,0)
pen.seth(90)
#大刻度
for _ in range(12):
pen.fd(190)
pen.down()
pen.fd(20)
pen.up()
pen.goto(0,0)
pen.rt(30)
#小刻度
for _ in range(60):
pen.up()
pen.goto(0,0)
pen.rt(6)
pen.fd(200)
pen.down()
pen.color('black')
pen.pensize(2)
pen.fd(10)
#画秒针
pen.up()
pen.home()
pen.down()
pen.color("red")
pen.pensize(3)
pen.seth(90)
pen.rt(s/60*360)
pen.fd(160)
pen.stamp()
#画分针
pen.up()
pen.home()
pen.down()
pen.color("Gold")
pen.pensize(3)
pen.seth(90)
pen.rt(m/60*360)
pen.fd(120)
pen.stamp()
#画时针
pen.up()
pen.home()
pen.down()
pen.color("Maroon")
pen.pensize(3)
pen.seth(90)
pen.rt(h/12*360)
pen.fd(80)
pen.stamp()
#问候字体
pen.up()
pen.goto(-175,250)
pen.color('orange')
font1 = ('宋体',20,'bold')
hello = "{}年你好!今天是{}月{}日".format(now.year,now.month,now.day)
pen.write(hello,"center",font=font1)
while True:
game.update()
now = dt.datetime.now()
draw_clock(now.hour,now.minute,now.second)
game.mainloop()
小猪佩奇
# coding: utf-8
import turtle as t
t.screensize(400, 300)
t.pensize(4) # 设置画笔的大小
t.colormode(255) # 设置GBK颜色范围为0-255
t.color((255, 155, 192), "pink") # 设置画笔颜色和填充颜色(pink)
t.setup(840, 500) # 设置主窗口的大小为840*500
t.speed(10) # 设置画笔速度为10
# 鼻子
t.pu() # 提笔
t.goto(-100, 100) # 画笔前往坐标(-100,100)
t.pd() # 下笔
t.seth(-30) # 笔的角度为-30°
t.begin_fill() # 外形填充的开始标志
a = 0.4
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill() # 依据轮廓填充
t.pu() # 提笔
t.seth(90) # 笔的角度为90度
t.fd(25) # 向前移动25
t.seth(0) # 转换画笔的角度为0
t.fd(10)
t.pd()
t.pencolor(255, 155, 192) # 设置画笔颜色
t.seth(10)
t.begin_fill()
t.circle(5) # 画一个半径为5的圆
t.color(160, 82, 45) # 设置画笔和填充颜色
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()
# 头
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30) # 顺时针画一个半径为300,圆心角为30°的园
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill()
# 耳朵
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()
# 眼睛
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
# 腮
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
# 嘴
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)
# 身体
t.color("red", (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100, 10)
t.circle(300, 30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300, 30)
t.circle(100, 3)
t.color((255, 155, 192), (255, 100, 100))
t.seth(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.end_fill()
# 手
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300, 15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20, 90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300, 15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20, 90)
# 脚
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
# 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.done()