python库_python第十五课:turtle库

点击蓝字

e71bad375738e05f3a5615b05460a001.gif

关注我们

05e566d6a346d3f947324f895a8567e9.png b779753a6174556f8b81d0e53da4a4c2.png 803975a9e50346f2dde2055bfaf34145.png

python第十五课

turtle库

07fe40190c9b759982a9da020fce3334.png b779753a6174556f8b81d0e53da4a4c2.png 414423865c1748dbd2188ee876b9bcaa.png

python拥有丰富的生态库,今天就带大家了解一下吧。

turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

265a77f54d21e9f87441e914cd688591.png

import函数

650ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

请输入

6a0ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

在正式讲turtle库之前需要带大家了解一个函数——import函数,在使用python库时需要使用到import函数将其导入,import函数有三种用法:(具体使用方式见详解turtle实例)

(1)import XX:常规导入, 在import之后跟上你要导入的模块名即可。还可以一条语句导入多个模块, 模块之间用逗号(,)分隔。例如:import turtle导入turtle库

(2)from xxx import yyy:很多时候往往只是想要使用模块中的一部分内容,这个时候就可以使用from xxx import yyy。此时使用模块中的内容就无需使用加入模块名,可以直接使用xxx中的yyy函数。例如:from math import exp 导入math库中的exp函数。

(3)import xxx as yyy:将xxx库取名为yyy,再使用时直接只用xxx库的别名yyy,这种方法简化了调用代码量,非常方便。

导入库之后就可以使用库内函数了,接下来我们了解一下turtle吧。

01

650ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

画布

画布就是turtle为我们展开用于绘图区域,我们可以设置它的大小和初始位置。

设置画布大小:turtle.screensize(canvwidth=None, canvheight=None, bg=None),参数分别为画布的宽(单位像素), 高, 背景颜色。例如:

import方式1:

ed884584d8e4013e887f9e17076fe9bd.png

import方式2:

8fb9e8bf51cdc365231ddaf5e55f7c44.png

import方式3:

c4ecd375958a161ebe4f96fa27339440.png

弹出画布窗口:

66c9d6beb36f2cf342adaba9bc2db2ca.png

02

650ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

画笔

1.画笔的状态

在画布上,默认有一个坐标原点为画布中心的坐标轴,坐标原点上有一只面朝x轴正方向小乌龟。这里我们描述小乌龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向), turtle绘图中,就是使用位置方向描述小乌龟(画笔)的状态。

2.画笔的属性

画笔(画笔的属性,颜色、画线的宽度等)

1) turtle.pensize():设置画笔的宽度;

2) turtle.pencolor():没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", "red",也可以是RGB 3元组。

3) turtle.speed(speed):设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。

3.绘图命令

操纵海龟绘图有着许多的命令,这些命令大致可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令。

画笔运动命令

8c72a4b979178dd830424b48e2b875ea.png

画笔控制命令

3a718408c8f7ddf8887cc9b59f0bf343.png

全局控制命令

804af8310b84fc929ad1564fd54bbab2.png

实例演示

650ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

请输入

6a0ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

说了这么多,为大家带来几个小例子:

实例1:画一条小蛇

84fe92196ed42ee259cf0a4118fc5eb5.png 0056a4f35605b9b61d9a499026e8f4e4.png

实例2:奥运5环

b601e248be975133609da96bc9ad6796.png 7b0f0e0d61dcedf1acdd4e764c75ce03.png 17e05ba6aab48dbd10514586d9a619f9.png 795e03d31d319f9a0b9e58c1b9046933.gif

接下来就是进阶版

c91809aec50aac9a3dc0c82b3a109457.gif fe2b9aab20414e69296dda508b66d1a2.png

进阶版实例演示:科赫雪花

650ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

请输入

6a0ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

科赫雪花实例代码

import turtle

def koch(size,n):

    if n==0:

        turtle.fd(size)

    else:

        for angle in [0,60,-120,60]:

            turtle.left(angle)

            koch(size/3,n-1)

def main():

    turtle.setup(600,600)

    turtle.penup()

    turtle.goto(-200,100)

    turtle.pendown()

    turtle.pensize(2)

    level = 3

    koch(400,level)

    turtle.right(120)

    koch(400,level)

    turtle.right(120)

    koch(400,level)

    turtle.hideturtle()

    turtle.done()

main()

turtle库

科赫雪花效果图

02d9f18e4e58bb5c8882bcdb919148ab.png

进阶实例演示:皮卡丘

650ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

请输入

6a0ef7f6-5f13-eb11-8da9-e4434bdf6706.svg

皮卡丘实例代码

import turtle as t

def infoPrt():

    print('coordinate: ' + str(t.pos()))

    print('angle: ' + str(t.heading()))

t.pensize(3)

t.hideturtle()

t.colormode(255)

t.color("black")

t.setup(700, 650)

t.speed(10)

t.st()

#t.dot()

t.pu()

#t.goto(-150,100)

t.goto(-210,86)

t.pd()

infoPrt()

# 头

print('头')

t.seth(85)

t.circle(-100,50)

#t.seth(78)

#t.circle(-100,25)

infoPrt()

t.seth(25)

t.circle(-170,50)

infoPrt()

# 右耳

print('右耳')

t.seth(40)

#t.circle(-250,52)

t.circle(-250,30)

infoPrt()

# 右耳尖

t.begin_fill()

# 左

t.circle(-250,22)

#t.fillcolor("pink")

# 右

t.seth(227)

t.circle(-270, 15)

prePos = t.pos()

infoPrt()

# 下

t.seth(105)

t.circle(100, 32)

t.end_fill()

t.pu()

t.setpos(prePos)

t.pd()

t.seth(212)

t.circle(-270, 28)

prePos = t.pos()

t.pu()

t.goto(t.xcor()+5,t.ycor()-2)

t.pd()

# 躯干

print('躯干')

t.seth(280)

t.circle(500, 30)

infoPrt()

# 臀部

print('臀部')

t.seth(120)

#t.circle(150, -55)

t.circle(150, -11)

p_tail=t.pos()

t.circle(150, -44)

p_butt=t.pos()

infoPrt()

# 尾巴

t.pu()

t.setpos(p_tail)

t.pd()

t.begin_fill()

t.seth(50)

t.fd(25)

t.seth(-50)

t.fd(30)

p_tail1=t.pos

t.seth(-140)

t.fd(36)

t.end_fill()

t.seth(39)

# 右尾和h1

t.fd(72)

# 右尾和v1

t.seth(125)

t.fd(48)

# 右尾和h2

t.seth(40)

t.fd(53)

# 右尾和v2

t.seth(88)

t.fd(45)

# 右尾和h3

t.seth(35)

t.fd(105)

# 右尾和v3

t.seth(105)

t.circle(850, 8)

#t.fd(105)

t.seth(215)

#t.fd(125)

t.circle(850, 11)

t.seth(280)

t.fd(110)

t.seth(220)

t.fd(50)

t.seth(309)

t.fd(56)

# 底盘

print('底盘')

t.pu()

t.setpos(p_butt)

t.pd()

t.seth(20)

t.circle(120, -45)

infoPrt()

t.seth(330)

t.circle(-150, -30)

infoPrt()

prePos = t.pos()

t.pu()

t.goto(t.xcor()+20,t.ycor())

t.pd()

t.seth(230)

t.circle(-70, 120)

p_bot=t.pos()

# 两脚-right

t.pu()

t.setpos(p_butt)

t.setpos(t.xcor()+5,t.ycor()+5)

t.pd()

t.seth(-86)

t.fd(30)

t.seth(-93)

t.fd(33)

t.seth(-225)

t.circle(-150, 22)

# 两脚-left

t.pu()

t.setpos(p_bot)

t.setpos(t.xcor()+85,t.ycor()-43)

t.pd()

t.seth(-105)

t.fd(50)

t.seth(-225)

t.circle(-150, 22)

# 左躯干

print('躯干')

t.pu()

t.setpos(p_bot)

t.pd()

t.seth(90)

t.circle(450, 13)

p_lfhd = t.pos()

t.circle(450, 5)

t.pu()

t.circle(450, 5)

t.pd()

t.circle(450, 6)

infoPrt()

# 左脸

print('左脸')

t.seth(330)

t.circle(50, -90)

infoPrt()

# 左酒窝

t.seth(30)

t.circle(-15, 120)

t.seth(-70)

t.circle(-30, 90)

# 左手

t.pu()

t.setpos(p_lfhd)

t.pd()

t.seth(160)

t.circle(150, 30)

infoPrt()

t.seth(180)

t.circle(-30, 150)

t.fd(67)

t.pu()

t.setpos(t.xcor()-40,t.ycor()-60)

t.pd()

t.seth(200)

t.circle(-5, 180)

# 右手

t.pu()

t.setpos(p_lfhd)

t.setpos(t.xcor()+180,t.ycor()+5)

t.pd()

t.seth(200)

t.circle(-50, 100)

t.pu()

t.circle(-50, 15)

t.pd()

t.circle(-50, 65)

t.pu()

t.setpos(t.xcor()+10,t.ycor()-45)

t.pd()

#t.seth(270)

#t.circle(-30, -180)

t.seth(80)

t.fd(10)

t.seth(165)

t.circle(10, 60)

t.seth(90)

t.fd(5)

t.seth(165)

t.circle(10, 60)

t.seth(95)

t.fd(5)

t.seth(185)

t.circle(10, 60)

t.seth(105)

t.fd(10)

t.seth(230)

t.fd(20)

t.seth(145)

t.fd(10)

t.seth(285)

t.fd(20)

# 右酒窝

t.pu()

t.setpos(t.xcor()-40,t.ycor()+110)

t.pd()

t.circle(27, 360)

# 嘴

t.pu()

t.setpos(t.xcor()-30,t.ycor()+28)

t.pd()

t.seth(280)

t.circle(-130, 30)

t.seth(270)

t.circle(-6, 160)

t.seth(130)

t.circle(-130, 30)

t.pu()

t.setpos(t.xcor()-5,t.ycor()+5)

t.pd()

t.seth(160)

t.circle(-20, -70)

t.seth(160)

t.circle(-30, -60)

t.pu()

t.setpos(t.xcor(),t.ycor()-28)

t.pd()

t.seth(200)

t.circle(50, 58)

# 左眼

t.pu()

t.setpos(t.xcor()-40,t.ycor()+90)

t.pd()

t.circle(5)

t.pu()

t.setpos(t.xcor()+5,t.ycor()+10)

t.pd()

t.begin_fill()

t.seth(190)

t.circle(15, 130)

t.seth(310)

t.circle(10, 15)

t.seth(0)

t.circle(17, 133)

t.seth(90)

t.circle(10, 15)

t.end_fill()

t.pu()

t.setpos(t.xcor()+2,t.ycor()-15)

t.pd()

t.color("white")

t.begin_fill()

t.circle(5)

t.end_fill()

# 右眼

t.pu()

t.setpos(t.xcor()+85,t.ycor()+15)

t.pd()

t.color("black")

t.circle(5)

t.pu()

t.setpos(t.xcor()+5,t.ycor()+10)

t.pd()

t.begin_fill()

t.seth(190)

t.circle(20, 130)

t.seth(310)

t.circle(10, 15)

t.seth(0)

t.circle(22, 133)

t.seth(90)

t.circle(13, 15)

t.end_fill()

t.pu()

t.setpos(t.xcor()-7,t.ycor()-15)

t.pd()

t.color("white")

t.begin_fill()

t.circle(7)

t.end_fill()

# 左耳

t.color("black")

t.pu()

t.goto(-210,86)

t.setpos(t.xcor()+15,t.ycor()+38)

t.pd()

t.seth(90)

t.circle(-250,30)

t.begin_fill()

# 左

t.circle(-250,18)

# 右

t.seth(270)

t.circle(-270, 12)

prePos = t.pos()

# 下

t.seth(180)

t.circle(100, 30)

t.end_fill()

t.pu()

t.setpos(prePos)

t.pd()

t.seth(270)

t.circle(-270, 18)

t.done()

turtle库

皮卡丘效果图

12c807d93299cfec8347103d9b2a299e.png 312710111851ead49d3891f0b2981aae.png 795e03d31d319f9a0b9e58c1b9046933.gif

尽情发挥你的想象力,创作出属于你的图案吧

c91809aec50aac9a3dc0c82b3a109457.gif 6ca6f7c9188618c4d20d106b38d3be69.png

长按识别二维码,关注我们

编辑:谢寿春

责编:郭锦洋

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
from turtle import * def star(center_point,first_vertex,radius): """根据圆心坐标及其第一个顶点坐标绘制五角星""" up() seth(0) goto(center_point) angle = towards(first_vertex) goto(first_vertex) lt(angle) rt(90) # 确定五个顶点坐标 five_vertex_points = [first_vertex] for _ in range(4): circle(-radius,360/5) five_vertex_points.append(pos()) # 开始绘制五角星 goto(first_vertex) color('yellow') down() begin_fill() for index in range(len(five_vertex_points)): goto(five_vertex_points[(index*2)%len(five_vertex_points)]) goto(first_vertex) end_fill() def China_Flag(height,start_x = None,start_y = None): tracer(0) # 设置高宽 width = (height / 2) * 3 if start_x is None and start_y is None: # 设置绘制起点 start_x = -(width/2) start_y = -(height/2) up() goto(start_x,start_y) down() # 绘制矩形旗面 setheading(0) color('red') begin_fill() for i in range(2): fd(width) lt(90) fd(height) lt(90) end_fill() # 确定五颗星的中心坐标 five_star_center_points = [(start_x+width/2/15*5,start_y+(1/2+5/20)*height), (start_x+width/2/15*10,start_y+(1/2+8/20)*height), (start_x+width/2/15*12,start_y+(1/2+6/20)*height), (start_x+width/2/15*12,start_y+(1/2+3/20)*height), (start_x+width/2/15*10,start_y+(1/2+1/20)*height),] # 确定五颗星的第一个顶点坐标 big_radius = height/2/10*3 # 大五星外接圆半径 small_radius = height/2/10 # 小五星外接圆半径 up() goto(five_star_center_points[0]) setheading(90) fd(big_radius) p = pos() first_vertex_points = [p] # 第一个顶点坐标 for point in five_star_center_points[1:]: goto(point) seth(0) angle = towards(five_star_center_points[0]) lt(angle) fd(small_radius) first_vertex_points.append(pos()) up() # 绘制五角星 # 大五角星 star(five_star_center_points[0], first_vertex_points[0], big_radius) # 4个小五角星 for i in range(1,5): star(five_star_center_points[i],first_vertex_points[i],small_radius) if __name__ == '__main__': screensize(600, 400) # 画布大小 bgcolor('black') # 背景颜色为黑色 speed(0) # 速度为最快 China_Flag(192,50,15) hideturtle() done()
03-23

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值