Python入门示例系列32 Turtle 入门

 B站视频教程:https://www.bilibili.com/video/bv1yb4y147SN

 

1 安装 turtle

python 默认已经安装了 turtle ,turtle 是 python 的标准库,不需要额外安装。

可以在IDLE或Pycharm中编写和测试turtle代码。

 

简单的常用函数示例

import turtle ## 导入turtle包
turtle.goto(50,50) # 从当前位置到 x=50,y=50
turtle.showturtle() #默认,显示画笔箭头
turtle.hideturtle() # 隐藏画笔箭头
turtle.left(90) # 左转90度
turtle.right(90) # 右转90度
turtle.forward(100) # 向前画100像素
turtle.backward(100) # 向后画100像素
turtle.penup() # 提起画笔
turtle.pendown() # 落下画笔(默认)
turtle.circle(100,360) # 画圆、圆弧、多边形
turtle.done() # 保持窗口

 

To make the turtle move in Python, we can use the forward() function. In the code snippet below, we have added a call to the forward() function while passing in an integer value of 75. This tells the turtle to move 75 steps beginning from the middle of the canvas. A step is equivalent to a pixel. By default, the turtle is actually an arrow shape, and it moves to the right unless you change turtle’s direction. The fd() function can be used as a shorthand if preferred.

 

turtle.exitonclick() 函数:exitonclick 函数的作用就是让海龟画完图像之后保持住,不要退出,直到我们点击窗口的时候 (onclick),才退出 (exit)。


2 基础概念
2.1 画布(canvas)
画布就是 turtle 绘图区域, 我们可以设置它的大小和背景颜色。

(1)turtle.screensize(canvwidth=None, canvheight=None, bg=None)

参数:分别为画布(单位为像素), (单位为像素), 背景颜色bg(background color)

作用:设置画布的大小和背景颜色。

例:

import turtle
turtle.screensize(800, 600, "green") 
# 宽(800像素), 高(600像素), 背景颜色为绿色
turtle.done() #  turtle.done() 暂停程序,停止画笔绘制,但绘图窗体不关闭,直到用户关闭Python Turtle图形化窗口为止;
#### 它的目的是给用户时间来查看图形,没有它,图形窗口会在程序完成是立即关闭。

 

 

import turtle
print(turtle.screensize())  # 返回画布的大小;画布的默认大小(400, 300)
turtle.done()

 
(2)turtle.setup(width=0.5, height=0.75, startx=None, starty=None)

作用:设置窗体的大小和位置。

          窗体里面包含画布,如果画布大于窗体,窗体会出现滚动条;如果画布小于窗体,画布上画的图可以超过画布的范围?-- 可以超过指定的画布范围,不能超过窗体的大小。

参数:
    width, height: 输入宽和高整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例
    (startx, starty): 这一坐标表示 矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心。

 例:

import turtle
turtle.setup(width=0.6, height=0.6) # # 宽占屏幕0.6(60%) , 高占屏幕0.6(60%) 
turtle.done()

 

import turtle
turtle.setup(width=800, height=800, startx=100, starty=100)
# 宽(800像素), 高(800像素), 画布的左上角在屏幕的位置为(100,100)
turtle.done()

 


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

(1)画笔的属性
画笔有颜色、画线的宽度等属性。
    1 turtle.pensize():设置画笔的宽度;实际上最小为1像素(设置为小于1的数,实际按1画图),最大不限制。

import turtle
turtle.pensize(0.2)
turtle.forward(100)
turtle.pensize(2)
turtle.forward(100)
turtle.pensize(20)
turtle.forward(100)
turtle.done()

详细示例:https://www.cnblogs.com/emanlee/p/15916036.html

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

import turtle
turtle.pencolor('brown') # 颜色名称
turtle.forward(100)
tup = (0.2, 0.8, 0.55)  # RGB 三元组,采用百分比形式表示
turtle.pencolor(tup) # RGB 三元组
turtle.forward(100)
turtle.pencolor('#33cc8c') # 十六进制的颜色值
turtle.forward(100)
turtle.done()

可以用的颜色名称:https://www.cnblogs.com/emanlee/p/15916036.html

 

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

import turtle
turtle.speed(1)
turtle.forward(100)
turtle.speed(5)
turtle.forward(100)
turtle.speed(10)
turtle.forward(100)
turtle.done()

实际测试,0最快,超过10最快,0到10之间越大越快。如果设置大于10的数值,或,<=0.5的数都会和0一样的速度。

 

turtle空间坐标体系

绝对坐标

 
重要函数:
turtle.goto(x,y) 让任何位置的海龟到达某一个坐标位置。如果是pendown状态会画图。

import turtle
turtle.goto(50,50)
turtle.done()

 


海龟坐标

 
重要函数:
turtle.fd(distance)  向海龟的正前方向运行;turtle.forward(distance)
turtle.bk(distance)  向海龟的反方向运行;turtle.backward(distance)
turtle.circle(radis,angle)   画线(后面详解)
注:前后左右是相对海龟来说,不是相对屏幕

import turtle
turtle.speed(1)
turtle.forward(100)
turtle.backward(200)
turtle.done()

 

3 turtle角度坐标体系

绝对角度

 

重要函数:
turtle.seth(angle) 改变海龟行进方向。注意:这个是绝对角度(如上图所示),不是相对角度(不是相对当前的行进方向的角度;而是相对x轴正方向的角度,逆时针角度)。

import turtle
turtle.forward(100)
turtle.seth(-90)
turtle.forward(100)
turtle.seth(0)
turtle.forward(100)
turtle.seth(90)
turtle.forward(100)
turtle.done()

 
海龟角度

 
重要函数:

turtle.left(angle)
turtle.right(angle)

left() 和 right() 都是相对角度,相对当前行进的方向的角度。
如上图所示

import turtle
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(-90)
turtle.forward(100)
turtle.done()

 
3 turtle库绘图命令

画笔运动命令:
turtle.pendown()    移动时绘制图形, 缺省时也为绘制(即: 没有做设置时,默认状态是 pendown )
turtle.penup()     移动时不绘制图形,提起笔,用于另起一个地方绘制时用

import turtle
turtle.forward(100)
turtle.penup()
turtle.forward(100)
turtle.pendown()
turtle.forward(100)
turtle.done()


turtle.stamp()     把乌龟(箭头)画出来.

# import package
import turtle
# forward turtle by 100
turtle.forward(100)
# print the turtle shape
turtle.stamp() # 把乌龟画出来
# and then continue
# forward turtle by 100
turtle.forward(100)
turtle.done()

 

 

# import package
import turtle
# forward turtle by 100
turtle.forward(100)
# print the turtle shape
#turtle.stamp()
# and then continue
# forward turtle by 100
turtle.forward(100)
turtle.done()

 
4 绘图举例

正方形(每个内角为90度)

from turtle import *
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
turtle.done()

 

等边三角形(每个锐角内角为60度)

from turtle import *
forward(100)
left(120)
forward(100)
left(120)
forward(100)
left(120)
turtle.done()

 

五角星(每个锐角内角为36度)



import turtle
turtle.speed(1)
for i in range(5):
turtle.forward(100)
turtle.left(180-36)

turtle.done()
 
 

  =================================================

四、海龟方法详解
1.海龟动作
(1)移动和绘制
turtle.forward(distance)
turtle.fd(distance)
参数:
      distance – 一个数值 (整型或浮点型)
作用:
      海龟前进 distance 指定的距离(像素),方向为海龟的朝向


turtle.back(distance)
turtle.bk(distance)
turtle.backward(distance)
参数:
      distance – 一个数值
作用:
      海龟后退 distance 指定的距离(像素),方向与海龟的朝向相反。不改变海龟的朝向。


turtle.right(angle)
turtle.rt(angle)
参数:
     angle – 一个数值 (整型或浮点型)
作用:
     海龟右转 angle 个单位。(单位默认为角度,但可通过 degrees() 和 radians() 函数改变设置。)

turtle.left(angle)
turtle.lt(angle)
参数:
      angle – 一个数值 (整型或浮点型)
作用:
      海龟左转 angle 个单位。(单位默认为角度,但可通过 degrees() 和 radians() 函数改变设置。)


turtle.goto(x, y=None)
turtle.setpos(x, y=None)
turtle.setposition(x, y=None)
参数:
     x – 一个数值或数值对/向量
     y – 一个数值或 None
     如果 y 为 None,x 应为一个表示坐标的数值对或 Vec2D 类对象 (例如 pos() 返回的对象).
作用:
      海龟移动到一个绝对坐标。如果画笔已落下将会画线。不改变海龟的朝向。
例子:

import turtle
# x和y
turtle.setpos(100,100)
# x为元组
turtle.setpos((100,0))
# x为pos()返回的元组
tp = turtle.pos()
turtle.setpos(tp)
turtle.done()

 
turtle.setx(x)
参数:
      x – 一个数值 (整型或浮点型)
作用:
     设置海龟的横坐标为 x,纵坐标保持不变。


turtle.sety(y)
参数:
     y – 一个数值 (整型或浮点型)
作用:
     设置海龟的纵坐标为 y,横坐标保持不变。

turtle.setheading(to_angle)
turtle.seth(to_angle)
参数:
     to_angle – 一个数值 (整型或浮点型)
作用:
     设置海龟的朝向为 to_angle。


turtle.home()
作用:
     海龟移至初始坐标 (0,0),并设置朝向为初始方向 (由海龟模式确定,参见 mode())。

import turtle
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.home()
turtle.stamp()
turtle.done()

 


turtle.circle(radius, extent=None, steps=None)
参数:
radius – 一个数值 (整型或浮点型),表示半径
    如果 radius 为值则圆心在乌龟的左边;如果 radius 为负值,圆心在乌龟的右边。
extent – 一个数值 (整型或浮点型),表示角度
    如未指定 extent则绘制整个圆,指定角度(可以大于360°)则绘制圆弧。
    如果 extent为正值则按乌龟的前进的方向,如果 extent为负值,按乌龟的倒退的方向。
steps – 一个整型数
    如未指定 steps则绘制圆弧,指定则绘制多边形(边数大于0;如果为3绘制三角形)。
作用:
     绘制一个 radius 指定半径的圆环、圆弧、或多边形部分。


例子:

# 绘制在左边的、按前进方向的
import turtle
turtle.circle(50,270)
turtle.done()

 

# 绘制在右边的、按前进方向的
import turtle
turtle.circle(-50,270)
turtle.done()

 

# 绘制在左边的、按后退方向的
import turtle
turtle.circle(50,-270)
turtle.done()

 

# 绘制在右边的、按后退方向的
import turtle
turtle.circle(-50,-270)
turtle.done()

# 绘制三角形
import turtle
turtle.circle(50,360,3) # 50是三角形外接圆环的半径,50像素
turtle.done()

 

### circle 示例
import
turtle turtle.setup(500,300) turtle.screensize(500,300) import turtle turtle.speed(1) turtle.circle(50,360) turtle.pencolor("red") turtle.circle(50,360,3) # 50是三角形外接圆环的半径,50像素 turtle.pencolor("green") turtle.circle(50,360,4) # 50是四边形外接圆环的半径,50像素 turtle.pencolor("blue") turtle.circle(50,360,5) # 50是五边形外接圆环的半径,50像素 turtle.done()

 

 

 


turtle.dot(size=None, *color)
参数:
    size – 一个整型数 >= 1 (如果指定)
    如果 size 未指定,则直径取 pensize+4 和 2*pensize 中的最大值。
    color – 一个颜色字符串或颜色数值元组
    未指定则是画笔颜色。
作用:
     绘制一个直径为 size,颜色为 color 的圆点。
例子:
turtle.dot(),turtle.dot(50),turtle.dot("blue"),turtle.dot(50,"blue")

import turtle
turtle.dot(50,"blue")
turtle.done()

 
turtle.undo()
作用:
     撤消 (或连续撤消) 最近的一个 (或多个) 海龟动作。可撤消的次数由撤消缓冲区的大小决定。

turtle.speed(speed=None)
参数:
    speed – 一个 0到10 范围内的整型数或速度字符串
    无参:返回当前海龟的速度
speed对应表:
   如果输入数值大于 10 或小于 0.5 则速度设为 0
   字符串    数字    含义
   “fastest”    0    最快
   “fast”    10    快
   “normal”    6    正常
   “slow”    3    慢
   “slowest”    1    最慢
   速度值从 1 到 10,画线和海龟转向的动画效果逐级加快。speed = 0 表示没有动画效果(转向),但绘制还是需要时间,最快。
作用:
     设置海龟移动的速度或返回当前海龟的速度。


(2)获取海龟状态

turtle.position()
turtle.pos()
作用:
     返回海龟当前的坐标 (x,y) (为 Vec2D 矢量类对象)。

import turtle
turtle.dot(100,"blue")
print(turtle.position())
turtle.done()

(0.00,0.00)


turtle.towards(x, y=None)
参数:
    x – 一个数值或数值对/矢量,或一个海龟实例
    y – 一个数值——如果 x 是一个数值,否则为 None
     返回一个角度,从当前海龟位置到 (x,y)的矢量海龟初始朝向的矢量(x轴正向)夹角
例子:

import turtle
turtle.goto(50,50)
print(turtle.towards(0,0))
turtle.done()
# 225.0
# 海龟朝向0°,从(50,50)到(0,0)的矢量朝向225°,两者相差225.0

 

 180+45=225



turtle.xcor()
作用:
     返回海龟的 x 坐标。

turtle.ycor()
作用:
     返回海龟的 y 坐标。

turtle.heading()
作用:
     返回海龟当前的朝向的角度

turtle.distance(x, y=None)
参数:
    x – 一个数值或数值对/矢量,或一个海龟实例
    y – 一个数值——如果 x 是一个数值,否则为 None
作用:
     返回从海龟位置到 (x,y)的单位距离。


(3)度量单位设置

turtle.degrees(fullcircle=360.0)
参数:
     fullcircle – 一个数值
作用:
     设置一个圆周为多少 “度”。默认值为 360 度。

>>> turtle.home()
>>> turtle.left(90)
>>> turtle.heading()
90.0

>>> turtle.degrees(400.0)
>>> turtle.heading()
100.0
# 原来的朝向占一个圆的四分之一,90/360。现在,还要占四分之一,那就是400/4=100°


turtle.radians()
设置角度的度量单位为弧度。其值等于  度 ÷ 180 × π  。

>>> turtle.home()
>>> turtle.left(90)
>>> turtle.heading()
90.0
>>> turtle.radians()
>>> turtle.heading()
1.5707963267948966
# 90/180*3.14=1.57...


2.画笔控制
(1)绘图状态

turtle.pendown()
turtle.pd()
turtle.down()
作用:
     画笔落下 – 移动时将画线。

turtle.penup()
turtle.pu()
turtle.up()
作用:
     画笔抬起 – 移动时不画线。

turtle.pensize(width=None)
turtle.width(width=None)
参数:
    width – 一个正数值
    无参:返回画笔的线条粗细。
作用:
     设置线条的粗细为 width 或返回该值。

turtle.isdown()
作用:
如果画笔落下返回 True,如果画笔抬起返回 False。


(2)颜色控制

turtle.pencolor(*args)   
参数:
    pencolor()
    返回表示当前画笔颜色的颜色描述字符串或元组。

    pencolor(colorstring)
    设置画笔颜色为 colorstring 指定的 Tk 颜色描述字符串。
    如:turtle.pencolor("brown")或turtle.pencolor('#32c18f')

    pencolor((r, g, b))
    设置画笔颜色为以 r, g, b 元组表示的 RGB 颜色。r, g, b 的取值范围应为0…colormode。
    如:turtle.pencolor((0.2, 0.8, 0.55))或turtle.pencolor((51.0, 204.0, 140.0))

    pencolor(r, g, b)
    设置画笔颜色为以 r, g, b 表示的 RGB 颜色。r, g, b 的取值范围应为 0…colormode。
    如:turtle.pencolor(0.2, 0.8, 0.55)或turtle.pencolor(51.0, 204.0, 140.0)

作用:
     返回或设置画笔颜色。

turtle.fillcolor(*args)
参数:同turtle.pencolor(*args)
    fillcolor()
    fillcolor(colorstring)
    fillcolor((r, g, b))
    fillcolor(r, g, b)
作用:
     返回或设置填充颜色。

turtle.color(*args)
参数:
    color()
    返回以一对颜色描述字符串或元组表示的画笔颜色和填充颜色,两者可分别由 pencolor() 和 fillcolor() 返回。

    color(colorstring)

    color((r,g,b))

    color(r,g,b)


    同时设置画笔颜色 填充颜色为指定的值。
    color(colorstring1, colorstring2)

    color((r1,g1,b1), (r2,g2,b2))
    相当于pencolor(colorstring1) 加 fillcolor(colorstring2)

作用:
     返回或设置画笔颜色和填充颜色。


(3)填充

turtle.filling()
作用:
     返回填充状态 (填充为 True,否则为 False)。

turtle.begin_fill()
作用:
     在绘制要填充的形状之前调用。要配合turtle.end_fill()使用。

turtle.end_fill()
作用:
     填充上次调用 begin_fill() 之后绘制的形状。要配合turtle.begin_fill()使用。

 

turtle.fillcolor(colorstring)    绘制图形的填充颜色
turtle.color(color1, color2)    同时设置pencolor=color1, fillcolor=color2,第二个参数为填充颜色
turtle.filling()    返回当前是否在填充状态
turtle.begin_fill()    准备开始填充图形
turtle.end_fill()    填充完成;


例子:(Python实验一.pdf

import turtle
turtle.color("black", "red") ## 画笔颜色为 black, 填充颜色为 red
turtle.begin_fill()
turtle.circle(80)
turtle.end_fill()
turtle.done()


(4)更多绘图控制

turtle.reset()
作用:
     从屏幕中删除海龟的绘图,海龟回到原点并设置所有变量为默认值。

turtle.clear()
作用:
     从屏幕中删除指定海龟的绘图。不移动海龟。海龟的状态和位置以及其他海龟的绘图不受影响。

import turtle
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
#turtle.clear()

 

 

上图没有 clear,下图 clear,可以看出:删除海龟的绘图,海龟的状态和位置不受影响。

import turtle
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.clear()

 

 

 
turtle.write(arg, move=False, align="left", font=("Arial", 8, "normal"))
参数:
    arg – 要写到 TurtleScreen 的对象
    如:"something",123,(1,2),[1,2,3]等

    move – True/False
    如果 move 为 True,画笔会移动到文本的右下角,这样文字就不会叠在一块了。

    align – 字符串 “left”, “center” 或 “right”
    align 指定对齐方式 (“left”, “center” 或 right")

    font – 一个三元组 (fontname, fontsize, fonttype)
    font 指定字体

书写文本 - arg 指定的字符串 - 到当前海龟位置。

例子:

import turtle
turtle.write("Home = ",True, align="center")
turtle.write( [123,456], True)
turtle.done()

import turtle
turtle.setup(500,300)
turtle.screensize(500,300)
import turtle
turtle.write("1234567890",False, align="right")
turtle.sety(-30)
turtle.write("1234567890",False, align="left")
turtle.sety(-60)
turtle.write("1234567890",False, align="center")
turtle.done()

 

import turtle
turtle.setup(500,300)
turtle.screensize(500,300)
import turtle
turtle.write("1234567890",True, align="right")
turtle.sety(-30)
turtle.write("1234567890",True, align="left")
turtle.sety(-60)
turtle.write("1234567890",True, align="center")
turtle.done()

 

 



3.海龟状态
(1)可见性

turtle.hideturtle()
turtle.ht() 
作用:
     使海龟不可见。当绘制复杂图形时,隐藏海龟可显著加快绘制速度。

turtle.showturtle()
turtle.st() 
作用:
     使海龟可见。

turtle.isvisible()  
作用:
     如果海龟显示返回 True,如果海龟隐藏返回 False。


(2)外观

turtle.shape(name=None)
  参数:
    name – 一个有效的形状名字符串
    无参:返回当前的形状名

设置海龟形状或返回当前的形状名。

常用的形状名有 arrow(向右的等腰三角形)、turtle(海龟)、circle(实心圆)、square(实心正方形)、triangle(向右的正三角形) 或 classic(箭头) 等 6 种,如下图所示:
在这里插入图片描述


turtle.register_shape(name, shape=None)
turtle.addshape(name, shape=None)
 
调用此函数有三种不同方式:
    name 为一个 gif 文件的文件名, shape 为 None: 安装相应的图像形状。:
    注解:当海龟转向时图像形状 不会 转动,因此无法显示海龟的朝向!

    name 为指定的字符串,shape 为由坐标值对构成的元组: 安装相应的多边形形状。

import turtle
turtle.register_shape("myshape", ((-20,20), (0,-10), (50,0)))
turtle.shape("myshape")
turtle.done()

 

 
    name 为指定的字符串, 为一个 (复合) Shape 类对象: 安装相应的复合形状。

作用:
     将一个海龟形状加入 TurtleScreen 的形状列表。只有这样注册过的形状才能通过执行 shape(shapename) 命令来使用。

Write a program that draws squares/triangle/five-pointed star in the center of the screen, as shown in Figure .

=================================================

REF

Python turtle.circle()函数

pycharm turtle无法识别turtle. 没有自动提示

https://blog.csdn.net/sandalphon4869/article/details/99443949

https://blog.csdn.net/weixin_46451496/article/details/107686486

https://docs.python.org/zh-cn/3/library/turtle.html

https://www.it610.com/article/1291589116017844224.htm

https://www.freesion.com/article/30191163480/

https://blog.csdn.net/weixin_44997802/article/details/107929807

https://blog.csdn.net/cnds123/article/details/113915180 (write)

https://blog.csdn.net/Commander_WingT/article/details/88748970 (write)

posted @ 2022-02-11 16:04  emanlee  阅读(3007)  评论(0编辑  收藏  举报