[Python] Turtle库的运用, 创作精美绘画

更多示例代码下载地址 : https://github.com/Amd794/Python123
前言
- 最初来自于 Wally Feurzig 和 Seymour Papert 于 1966 年所创造的 Logo 编程语言
- 可以通过相关的指令, 轻松地绘制出精美的形状和图案
- 可以培养学习计算机的兴趣, 以一种娱乐的方式了解计算机的趣味
- Python 海龟创意绘画, Turtle库创作精美图画
- 通过阅读本文, 你可以学习到LSystem的应用和掌握绘制一些图形 , 有关介绍: https://en.wikipedia.org/wiki/L-system
turtle 基础教程
- 个人推荐看官方的文档, https://docs.python.org/zh-cn/3/library/turtle.html
部分效果预览









简单的例子应用
turtle文字的应用
一个很简单的随机文字
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
from turtle import *import randomstr_ = """守一段情 念一个人。时光不老 我们不散。厮守终生 不离不弃。天暗下来 你就是光。亡魂溺海 止于终老。生死挈阔 与子成说。柔情似水 佳期如梦。我中有你 你中有我。青山不老 为雪白头。心若向阳 无畏悲伤。一人一心 白首不离。心如荒岛 囚我终老。我的世界 只有你懂。你若安好 便是晴天。心有灵犀 一点就通。厮守海角 非你不娶。执子的手 漫漫的走。执子之手 与子偕老。山河拱手 为君一笑。红尘初妆 山河无疆。千秋功名 一世葬你。既不回头 何必不忘。既然无缘 何须誓言。今日种种 似水无痕。明夕何夕 君已陌路。才会相思 便害相思。人来人往 繁华似锦。回首万年 情衷伊人。生能尽欢 死亦无憾。执手若无 泪溅花上。花开花落 人世无常。入我心者 待以君王。为醉而醉 似醉非醉。伤心鸿影 爱已惘然。只要你要 只要我有。日久生情 日久情疏。忧佳相随 风雨无悔。有生之年 誓死娇宠引喻山河 指日可诚。水上鸳鸯 云中翡翠。天荒地老 海誓山盟。生则同襟 死则同穴。生有此女 夫复何求""".split("。")setup(1280,720) # 设置窗口大小colormode(255) # 使用的颜色模式, 整数还是小数up()a, b = -500, 280goto(a,b)bgcolor("black")down()def w(str_,b): bgcolor( random.randint(0,255),random.randint(0,255),random.randint(0,255)) # 随机生成RGB值, 每次调用函数改变背景颜色 for i in range(len(str_)): up() goto(a+100*i,b) down() size = random.randint(12,68) # 随机字体大小 color( random.randint(0,255),random.randint(0,255),random.randint(0,255)) # 随机字体颜色 write(str_[i], align="center",font=("楷体",size)) for k in range(4): for i in range(7): w(str_[i+7*k],b-100*i) reset() # 清屏 for i in range(7): w(str_[i+7*4],b-100*i) |

简单的图形例子
主要是通过改变角度和位置偏移, 做出各种效果
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
from turtle import *speed(0)bgcolor("white") pencolor("MediumAquamarine")h = 10for j in range(360): for i in range(4): forward(h) right(90) right(3) h = h*1.01 |




Lsystem 的应用
实质是通过不断变换规则, 绘制出各种发杂图形
以下符号字符的几何解释。
| # | 字符 | 含义 |
|---|---|---|
| 1 | F |
按行绘制一条线向前移动 |
| 2 | f |
按线条长度向前移动而不绘制线条 |
| 3 | + |
通过转动角度向左转动 |
| 4 | - |
通过转动角度向右转动 |
| 5 | / | 反向(即:转动180度) |
| 6 | [ |
将当前绘图状态推入堆栈 |
| 7 | ] |
从堆栈弹出当前绘图状态 |
| 8 | # |
按线宽增量增加线宽 |
| 9 | ! |
通过线宽增量减小线宽 |
| 10 | @ |
绘制带有线宽半径的点 |
| 12 | } |
关闭多边形并用填充颜色填充 |
| 13 | < |
将线长乘以线长比例因子 |
| 14 | > |
将线长除以线长比例因子 |
| 15 | & |
交换+和 - 的含义 |
| 16 | ( |
通过转动角度增量减小转动角度 |
| 17 | ) |
通过转动角度增量来增加转动角度 |
| 18 | { |
打开多边形 |
相关说明
1 draw_path(length, angle, path, expalnation)
2 主要用来绘制海龟行走路径
3 length ---->每次行走的距离
4 angle ---->偏移的角度
5 path ---->初始路径图案,即0阶的形状
6 expalnation ---->用来记录打印每一步操作
7 apply_rules(path, rules)
8 主要是转换每一阶段的path
9 path ---->初始路径图案,即0阶的形状
10 rules ---->转换的规则
11 getColor()
12 提供一个随机rgb值
13 initialization()
14 初始化各种参数
15 Introduction(x=-600, y=-350)
16 注解
17 默认海龟初始位置(-600,-350)
18 run(n,angle,length,path,rules)
19 启动程序
实现如下:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# -*- coding: utf-8 -*-# Time : 2019/4/5 22:20# Author : Mifen# Email : 2952277346@qq.com# Github : https://github.com/Amd794import timeimport turtle as tfrom turtle import *setup(1280,720)t.speed(0)t.pensize(1)length = 5path = 'FX'angle = 25up()color("#262626;")goto(-600, 300)write('Author:Mifen', font=("微软雅黑", 18))goto(-600, 250)write('E-mail :2952277346@qq.com', font=("微软雅黑", 18))goto(-600, 200)write('Code :https://github.com/Amd794/Python123', font=("微软雅黑", 18))goto(-600, -350)down()expalnation = { 'F': '画线', 'x': '-', '+': '逆时针旋转', '-': '顺时针旋转', '[': '记录当前位置', ']': '恢复上一个位置', 'a': '上色', 'b': '上色', 'c': '上色'}rules = { 'F': 'aFF-[b-F+F]+[c+F-F]', 'X': 'aFF+[b+F]+[c-F]'}def draw_path(path, expalnation): posList, angleList = [], [] t.up() t.goto(0, -350) t.down() t.lt(90) for symbol in path: if symbol == 'F': t.forward(length) elif symbol == '+': t.left(angle) elif symbol == '-': t.rt(angle) elif symbol == '[': posList.append(t.pos()) angleList.append(t.heading()) elif symbol == 'a': t.pensize(3) t.color("#8c503c") elif symbol == 'b': t.pensize(2) t.color("#4ab441") elif symbol == 'c': t.pensize(2) t.color("#18b418") elif symbol == ']': t.up() t.home() t.goto(posList.pop()) t.left(angleList.pop()) t.down()def apply_rules(path, rules): L = [_ for _ in path] for i in range(len(L)): symbol = L[i] if symbol == 'F': L[i] = rules[symbol] if symbol == 'X': L[i] = rules[symbol] path = ''.join(L) return pathfor _ in range(5): path = apply_rules(path, rules)draw_path(path, expalnation) |
综合以上, 来一个整合:
|
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# -*- coding: utf-8 -*-# Time : 2019/4/6 22:45# Author : Mifen# Email : 2952277346@qq.com# Github : https://github.com/Amd794from turtle import *import timeimport turtle as tdef gotopos(x, y): up() goto(x, y) down() ht()def author(): pensize(2) gotopos(610, -315) lt(-90) fd(80) pensize(1) lt(-270) gotopos(525, -330) color("#772a2b") write("Mifen", font=("华文隶书", 24)) gotopos(409, -360) write("2952277346@qq.com", font=("华文隶书", 18)) gotopos(250, -390) write("https://github.com/Amd794/Python123", font=("华文隶书", 18))def apply_rules(path, rules): L = [_ for _ in path] for i in range(len(L)): symbol = L[i] if symbol == 'F': L[i] = rules[symbol] if symbol == 'X': L[i] = rules[symbol] path = ''.join(L) return pathdef draw_path(path): posList, angleList = [], [] for symbol in path: if symbol == 'F': t.forward(length) elif symbol == '+': t.left(angle) elif symbol == '-': t.rt(angle) elif symbol == '[': posList.append(t.pos()) angleList.append(t.heading()) elif symbol == 'a': t.pensize(3) t.color("#867b68") elif symbol == 'b': t.pensize(2) t.color("#867b68") elif symbol == 'c': t.pensize(2) t.color("#867b68") elif symbol == ']': t.up() t.home() t.goto(posList.pop()) t.left(angleList.pop()) t.down()def writez(x, y, str_, size=56, font="华文行楷"): gotopos(x, y) write(str_, font=(font, size))setup(1280, 800)speed(5)bgcolor("#9c917f")color("#afa697")begin_fill()gotopos(0, -400)circle(400)end_fill()author()color("#7d776d")s = "愿天化作比翼鸟"s2 = "在地愿为连理枝"for i in range(len(s)): writez(560, 350 - i * 50, s[i], 36)for i in range(len(s2)): writez(460, 350 - i * 50, s2[i], 36)color("#888475")writez(-50, 100, "我")writez(-50, 40, "的")writez(-160, 0, "心", 96)writez(-50, 0, "月", 176)writez(33, -30, "代", 62)writez(-18, -95, "表", 78)writez(-213, -210, "亮", 196)gotopos(249, -26)color("#867b68")speed(0)gotopos(-650, -100)length = 6path = 'F'angle = 27rules = { 'F': 'aFF[b-F++F][c+F--F]c++F--F', 'X': 'aFF+[b+F]+[c-F]'}for _ in range(4): path = apply_rules(path, rules)draw_path(path)gotopos(570, -330)done() |

[Python] Turtle库的运用, 创作精美绘画的更多相关文章
- Python turtle库详解
Python turtle库详解 Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在 ...
- Python turtle库的画笔控制说明
turtle.penup() 别名 turtle.pu() :抬起画笔海龟在飞行 turtle.pendown() 别名 turtle.pd():画笔落下,海龟在爬行 turtle.pensize(w ...
- Python Turtle库绘制蟒蛇
使用Python Turtle库来绘制蟒蛇 import turtle引入了海龟绘图体系 使用setup函数,设定了一个宽650像素和高350像素的窗体,其位置左上角坐标是200,200 说明位置在距 ...
- 一篇文教你使用python Turtle库画出“精美碎花小清新风格树”快来拿代码!
Turtle库手册可以查询查询 python图形绘制库turtle中文开发文档及示例大全,手册中现有示例,不需要自己动手就可以查看演示. 使用Turtle画树,看了一下网上的代码,基本上核心的方法是使 ...
- python turtle库的几个小demo
一.先上图 一个同切圆和五角星 上代码 import turtle #同切圆 turtle.pensize(2) turtle.circle(10) turtle.circle(40) turtle. ...
- python turtle库
turtle库初步 先看 https://www.cnblogs.com/chy8/p/9448606.html 一 turtle库介绍 turtle乌龟 import turtle from tur ...
- Python turtle库学习笔记
1.简介 Python的turtle库的易操作,对初学者十分友好.对于初学者来说,刚学编程没多久可以写出许多有趣的可视化东西,这是对学习编程极大的鼓舞,可以树立对编程学习的信心.当然turtle本身也 ...
- Python turtle库绘制简单图形
一.简介 Python中的turtle库是一个直观有趣的图形绘制函数库.turtle库绘制图形有一个基本框架:一个小海龟在坐标系中爬行,其爬行轨迹形成了绘制图形. 二.简单的图形列举 1.绘制4个不同 ...
- 第1课(续集),python turtle库的使用
原文再续,书接上一回 上回讲到了,python IDLE的草稿本和作业本,并顺便试了试python的输入输出,变量,运算的体验,大家应该能感受到python的简单了吧. 下面我们继续体验python的 ...
- Python turtle库的应用——蛇
turtle库介绍 1.Turtle中的turtle.setup()函数用于启动一个图形窗口,它有四个参数 turtle.setup(width, height, startx, starty) 分别 ...
随机推荐
- 初始elasticSearch
elasticSearch 大致印象 为什么用? mysql更擅长于crud等操作,当一张表达到百万级别时,检索速度过慢 es检索速度快 基本概念 Index索引(两层意思) 动词:类似mysql的i ...
- Java 中怎样将 bytes 转换为 long 类型?
将bytes 转换为long类型: 第一种方式: String 接收 bytes 的构造器转成 String,再 Long.parseLong: 但此种情况需要注意:字节数组中的每个字节都必须是有效的 ...
- AI回答打鸟问题
GPT-3.5 树上有9只鸟,猎枪打下来1只,还剩几只? 如果猎枪打下来1只鸟,那么树上剩下的鸟应该是8只.
- 解决pyinstaller生成的exe文件,在部分电脑无法运行的问题
下载vc_redist.x64.exe并在不能运行的电脑上运行即可. vc_redist.x64.exe的作用:一款Visual C++的运行库,里面包含了一些Visual C++的库函数.Visua ...
- 园子的商业化努力-阿里云开发者社区合作:RDS MySQL Serverless 免费试用活动
阿里云开发者社区这个月组织了一个针对"云数据库 RDS MySQL Serverless 版"免费试用用户的做任务活动,上周找园子合作,希望能在园子里推广一下这个活动. 由于时间太 ...
- Parallel 与 ConcurrentBag<T> 这对儿黄金搭档(C#)【并发编程系列】
〇.前言 日常开发中经常会遇到数据统计,特别是关于报表的项目.数据处理的效率和准确度当然是首要关注点. 本文主要介绍,如何通过 Parallel 来并行处理数据,并组合 ConcurrentBag&l ...
- 自治系统/自治域和自治系统编号(ASN)
定义: 自治系统或自治域(英文:Autonomous system, AS)是指在互联网中,一个或多个实体管辖下的所有IP网络和路由器的组合,它们对互联网执行共同的路由策略.参看RFC 1930中更新 ...
- Python实现输入三个整数x,y,z,请把这三个数由小到大输出;
num1=input('请输入第一个数,x:') num2=input('请输入第二个数,y:') num3=input('请输入第三个数,z:') if num1>num2: # if 语句判 ...
- java学习阶段一
扩展名默认没有打开 FIRST APP public class HelloWorld { public static void main (String[] args){ System.out.pr ...
- 使用.NET Jieba.NET 的 PosSegmenter 实现中文分词匹配
目录 引言 1. 什么是中文分词 2. Jieba.NET简介 3. PosSegmenter介绍 4. 实现中文分词匹配 4.1 安装Jieba.NET库 4.2 创建PosSegmenter实 ...