Python turtle 绘图入门必知必会


Turtle库是Python语言中一个简单流行的绘图函数库(叫做海龟绘图Turtle Graphics),Turtle库是Python的内部库,它可以让你使用海龟图形(turtle graphics)绘制图像,用导入即可 import turtle。 

海龟绘图(trutle)是向孩子们介绍编程的一种流行方式。它是Wally Feurzig和Seymour Papert于1966年开发的Logo编程语言的一部分。Logo语言是一种早期的编程语言,也是一种与自然语言非常接近的编程语言,它通过“绘图”的方式来学习编程,对初学者特别是青少年儿童进行寓教于乐的教学方式。

想象一只小海龟,在一个横轴为x、纵轴为y的坐标系中。以坐标原点(0,0)开始,它根据输入的一组代码指令,在平面坐标系中移动。从而在它爬行的路径上绘制了各种酷炫、奇特的图形。

Python turtle 绘图入门必知必会

先说明一下turtle绘图的基础知识:

1、画布(canvas)

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

1.1 设置画布大小

turtle.screensize(canvwidth=None, canvheight=None, bg=None)
参数分别为画布的宽(单位像素),高,背景颜色

如:

turtle.screensize(1080, 800, "green")
turtle.screensize() #返回默认大小(400, 300)

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

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

如:

turtle.setup(width=0.8, height=0.8)
turtle.setup(width=800, height=800, startx=100, starty=100)

2、画笔
2.1 画笔的状态

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

2.2 画笔的属性

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

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

    >>> pencolor('brown')
    >>> tup = (0.2, 0.8, 0.55)
    >>> pencolor(tup)
    >>> pencolor()
    '#33cc8c'

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

2.3 绘图命令

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

(1)画笔运动命令:

命令说明
turtle.forward(distance) 向当前画笔方向移动distance像素长
turtle.backward(distance) 向当前画笔相反方向移动distance像素长度
turtle.right(degree) 顺时针移动degree°
turtle.left(degree) 逆时针移动degree°
turtle.pendown() 移动时绘制图形,缺省时也为绘制
turtle.goto(x,y) 将画笔移动到坐标为x,y的位置
turtle.penup() 移动时不绘制图形,提起笔,用于另起一个地方绘制时用
turtle.speed(speed) 画笔绘制的速度范围[0,10]整数
turtle.circle() 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆

(2)画笔控制命令:

命令说明
turtle.pensize(width) 绘制图形时的宽度
turtle.pencolor() 画笔颜色
turtle.fillcolor(colorstring) 绘制图形的填充颜色
turtle.color(color1, color2) 同时设置pencolor=color1, fillcolor=color2
turtle.filling() 返回当前是否在填充状态
turtle.begin_fill() 准备开始填充图形
turtle.end_fill() 填充完成;
turtle.hideturtle() 隐藏箭头显示;
turtle.showturtle() 与hideturtle()函数对应

(3) 全局控制命令:

命令说明
turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset() 清空窗口,重置turtle状态为起始状态
turtle.undo() 撤销上一个turtle动作
turtle.isvisible() 返回当前turtle是否可见
stamp() 复制当前图形
turtle.write(s[,font=("font-name",font_size,"font_type")]) 写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;font为可选项, font的参数也是可选项

3、命令详解

3.1 turtle.circle(radius, extent=None, steps=None)
描述: 以给定半径画圆
参数:
radius(半径); 半径为正(负),表示圆心在画笔的左边(右边)画圆
extent(弧度) (optional);
steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)

举例:
circle(50) # 整圆;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圆

4、绘图举例

4.1长方形:

#导入turtle包的所有内容:
from turtle import  *#设置笔刷宽度:
width(9)

#前进:
forward(300)
#右转90度:
right(90)

#笔刷颜色:
pencolor('red')
forward(200)
right(90)

pencolor('green')
forward(600)
right(90)

pencolor('blue')
forward(200)
right(90)

pencolor('black')
forward(300)
right(90)

#调用done()使得窗口等待被关闭,否则将立刻关闭窗口:
done()

Python turtle 绘图入门必知必会

4.2 三个正方形:

import turtle
turtle.screensize(canvwidth=None, canvheight=None,  bg="blue")
from turtle import *
width(10)
def  drawSquare(sides,length):
  angle = 360/sides
  for again in  range(sides):     
      turtle.forward(length)
       turtle.right(angle)
def moveTurtle(x,y):
  turtle.penup()
   turtle.goto(x,y)
   turtle.pendown()
drawSquare(4,160)
moveTurtle(200,200)
drawSquare(4,160)
moveTurtle(-200,200)
drawSquare(4,160)
turtle.done()

Python turtle 绘图入门必知必会 

4.3 正弦波

import turtle
import math
# turtleSet function makes the code a bit more  compact
def turtleSet(name, shape, speed, color, pen, pos_x, pos_y):
     name.ht()
    name.up()
    name.shape(shape)
     name.speed(speed)
    name.color(color)
    name.pensize(pen)
     name.setposition(pos_x, pos_y)
    name.down()
    name.st()
wn =  turtle.Screen()
background =  turtle.Turtle()
background.speed(0)
background.up()
background.goto(325,  325)
background.down()
background.right(90)
background.fillcolor("red")
background.begin_fill()
for  i in range(4): # Draw a 650 x 650 square
    background.forward(650)
     background.right(90)
background.end_fill()
height = 200  #amplitude
period = 25 # Period, but without any specific unit.
circle =  turtle.Turtle()
turtleSet(circle, 'circle', 10, "green", 2, height,  0)
circle.left(90)
sine = turtle.Turtle()
turtleSet(sine, 'circle', 10,  "black", 2, -325, 0)
sine.st()
sine.left(45)
line =  turtle.Turtle()
turtleSet(line, 'circle', 10, "blue", 2, -286,  0)
line.left(90)
time = turtle.Turtle()
turtleSet(time, 'arrow', 10,  "yellow", 2, -325, 0)
time.right(0)
for i in range(650):
    x =  height*math.cos(i/period)
    y = height*math.sin(i/period)
     sine.goto(i-325, y)
    circle.goto(x, y)
    line.goto(-286, y)
     time.goto(i-325, 0)
wn.exitonclick()

Python turtle 绘图入门必知必会

4.3 鹦鹉螺

from turtle import *
speed(0)
bgcolor("white")  
pencolor("MediumAquamarine")

h = 10

for j in  range(360):

    for i in range(4):
        forward(h)
         right(90)

    right(3)
    h = h*1.01
turtle.done()

Python turtle 绘图入门必知必会

https://www.linuxboy.net/topicnews.aspx?tid=17

linuxboy的RSS地址:https://www.linuxboy.net/rssFeed.aspx

本文永久更新链接地址:https://www.linuxboy.net/Linux/2020-01/161988.htm

相关内容