Python自带了一个turtle库,就像名字turtle说的那样,你可以创建一个turtle,然后这个turtle可以前进,后退,左转,这个turtle有一条尾巴,能够放下和抬起,当尾巴放下的时候,turtle走过的地方就留下了痕迹,也就是这只画笔的原理。

下面的表格是基本的一些turtle的方法,这里简单列举了一点。

命令解释turtle.Screen()返回一个singleton object of a TurtleScreen subclass

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()画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆

下面是一个很简单的turtle的例子,我们使用turtle来画一个螺旋的图案,这个函数采用递归的方法,每次递归的画笔减小了5个单位长度,进而形成了一个向内螺旋的图案。

import turtle

my_turtle = turtle.Turtle()

my_win = turtle.Screen()

def draw_spiral(my_turtle, line_len):

if line_len > 0 :

my_turtle.forward(line_len) # turtle前进

my_turtle.right(90) # turtle向右转

draw_spiral(my_turtle, line_len - 5) #turtle继续前进向右转

draw_spiral(my_turtle, 100)

my_win.exitonclick()

画一颗树

接下来,我们用turtle来画一颗树。过程是这样的:

branch_len为树枝的长度,这里的turtle也是采用递归的方法,在树枝需要分叉的地方建立一颗新的子树,而且是左右两颗子树,右子树的长度比左子树的长度要少5个单位。

import turtle

def tree(branch_len, t):

if branch_len > 5:

t.forward(branch_len)

t.right(20)

tree(branch_len - 15, t)

t.left(40)

tree(branch_len - 10, t)

t.right(20)

t.backward(branch_len)

def main():

t = turtle.Turtle()

my_win = turtle.Screen()

t.left(90)

t.up()

t.backward(100)

t.down()

t.color("green")

tree(75, t)

my_win.exitonclick()

main()

The Sierpinski triangle illustrates a three-way recursive algorithm.

The procedure for drawing a Sierpinski triangle by hand is simple. Start

with a single large triangle. Divide this large triangle into four new

triangles by connecting the midpoint of each side. Ignoring the middle

triangle that you just created, apply the same procedure to each of the

three corner triangles

import turtle

def draw_triangle(points, color, my_turtle):

my_turtle.fillcolor(color)

my_turtle.up()

my_turtle.goto(points[0][0],points[0][1])

my_turtle.down()

my_turtle.begin_fill()

my_turtle.goto(points[1][0], points[1][1])

my_turtle.goto(points[2][0], points[2][1])

my_turtle.goto(points[0][0], points[0][1])

my_turtle.end_fill()

def get_mid(p1, p2):

return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2)

def sierpinski(points, degree, my_turtle):

color_map = [‘blue‘, ‘red‘, ‘green‘, ‘white‘, ‘yellow‘,

‘violet‘, ‘orange‘]

draw_triangle(points, color_map[degree], my_turtle)

if degree > 0:

sierpinski([points[0],

get_mid(points[0], points[1]),

get_mid(points[0], points[2])],

degree-1, my_turtle)

sierpinski([points[1],

get_mid(points[0], points[1]),

get_mid(points[1], points[2])],

degree-1, my_turtle)

sierpinski([points[2],

get_mid(points[2], points[1]),

get_mid(points[0], points[2])],

degree-1, my_turtle)

def main():

my_turtle = turtle.Turtle()

my_win = turtle.Screen()

my_points = [[-100, -50], [0, 100], [100, -50]]

sierpinski(my_points, 3, my_turtle)

my_win.exitonclick()

main()

python初始画笔_Python自带Turtle画笔的原理相关推荐

  1. python画国际象棋_python图形工具turtle绘制国际象棋棋盘

    本文实例为大家分享了python图形工具turtle绘制国际象棋棋盘的具体代码,供大家参考,具体内容如下 #编写程序绘制一个国际象棋的棋盘 import turtle turtle.speed(30) ...

  2. python索引例子_Python实现带下标索引的遍历操作示例

    本文实例讲述了Python实现带下标索引的遍历操作.分享给大家供大家参考,具体如下: 代码如下: #coding=utf-8 #python - 实现带下标索引的遍历. str = 'abcdefgh ...

  3. python 录音翻译_python实现带声音的摩斯码翻译实现方法

    本文实例讲述了python实现带声音的摩斯码翻译程序,分享给大家供大家参考.具体分析如下: 这里需要使用PyGame来发出声音. import pygame import time import sy ...

  4. python timeit计时_Python 自带计时工具 timeit

    欢迎大家访问我自己架的博客站点 Python 自带计时工具 timeit Timer 类: __init__(stmt="pass", setup="pass" ...

  5. python常用代码_Python常用算法学习(3)(原理+代码)——最全总结

    1,什么是算法的时间和空间复杂度 算法(Algorithm)是指用来操作数据,解决程序问题的一组方法,对于同一个问题,使用不同的算法,也许最终得到的结果是一样的,但是在过程中消耗的资源和时间却会有很大 ...

  6. python断言失败_python异常处理、自定义异常、断言原理与用法分析

    本文实例讲述了python异常处理.自定义异常.断言原理与用法.分享给大家供大家参考,具体如下: 什么是异常: 当程序遭遇某些非正常问题的时候就会抛出异常:比如int()只能处理能转化成int的对象, ...

  7. python办公自动化模块_Python自动化办公Excel模块openpyxl原理及用法解析

    openpyxl 介绍 openpyxl 是一个直接可用于读写 xlsx .xlsm.xltx.xltm 文件的 Python 内置库,借助它可以利用 Python 语法对本地 xlsx 文件进行自动 ...

  8. python红包程序_Python写随机发红包的原理流程

    首先来说说要用到的知识点,第一个要说的是扩展包random,random模块一般用来生成一个随机数 今天要用到ramdom中unifrom的方法用于生成一个指定范围的随机浮点数通过下面的图简单看下: ...

  9. python turtle隐藏画笔_Python turtle库的画笔控制说明

    turtle.penup() 别名 turtle.pu() :抬起画笔海龟在飞行 turtle.pendown() 别名 turtle.pd():画笔落下,海龟在爬行 turtle.pensize(w ...

最新文章

  1. 《系统集成项目管理工程师》必背100个知识点-14项目章程的内容
  2. MFC中Spin control的使用
  3. 3月16日学习内容整理:metaclass
  4. C之memset、memcpy、strcpy、strncpy区别
  5. 【动态规划】加法最大 (ssl 1595)/乘积最大 (ssl 1007)
  6. php csv转excel 双引号,PHP高效导出Excel(CSV)
  7. linux传输文件命令scp,linux文件传输命令:SCP用法
  8. OpenCV_Find Basis F-Matrix and computeCorrespondEpilines(获取一对图像的基础矩阵及对应极线)
  9. 基于Python的心电图上位机诊断软件
  10. 相机dc内置滤镜 千奇百怪不离其踪 漫谈数码相机内置滤镜
  11. python33个保留字基本含义_Python保留字总结
  12. 参考文献的序号怎么对齐_word序号对齐方式 word中如何让编号自动对齐
  13. csp ccf公共钥匙盒
  14. ARP协议和NDP协议分析
  15. 单个Java文件打成可执行JAR包
  16. 5.8G无线游戏耳机模组方案需要哪些亮点|天惠微科技
  17. MySQL数据库基础知识12,MySQL单列索引和联合索引
  18. linux 内核源码下载
  19. swell word
  20. 民航导航技术发展及北斗应用分析

热门文章

  1. 重启与新生,哪家在线教育的转型最有看点?
  2. 毕业设计源码之WMS--开源仓库管理系统
  3. 精准教学通为什么显示缓存服务器不可用,精准教学通学生端
  4. nagios监控安装配置文档+139邮箱报警
  5. Mac 网页字体优化小议
  6. VSCode安装教程(图文详解,简单搞定)
  7. Oracle安装的一些问题收集[转]
  8. 天翼云网站访问不了解决办法
  9. python序列是几维_numpy中三维阵列的二维切片序列
  10. 面试虚报工资,没想要求提供银行流水怎么办?|智测优聘总结