[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 random
 
str_ = """
守一段情 念一个人。
时光不老 我们不散。
厮守终生 不离不弃。
天暗下来 你就是光。
亡魂溺海 止于终老。
生死挈阔 与子成说。
柔情似水 佳期如梦。
我中有你 你中有我。
青山不老 为雪白头。
心若向阳 无畏悲伤。
一人一心 白首不离。
心如荒岛 囚我终老。
我的世界 只有你懂。
你若安好 便是晴天。
心有灵犀 一点就通。
厮守海角 非你不娶。
执子的手 漫漫的走。
执子之手 与子偕老。
山河拱手 为君一笑。
红尘初妆 山河无疆。
千秋功名 一世葬你。
既不回头 何必不忘。
既然无缘 何须誓言。
今日种种 似水无痕。
明夕何夕 君已陌路。
才会相思 便害相思。
人来人往 繁华似锦。
回首万年 情衷伊人。
生能尽欢 死亦无憾。
执手若无 泪溅花上。
花开花落 人世无常。
入我心者 待以君王。
为醉而醉 似醉非醉。
伤心鸿影 爱已惘然。
只要你要 只要我有。
日久生情 日久情疏。
忧佳相随 风雨无悔。
有生之年 誓死娇宠
引喻山河 指日可诚。
水上鸳鸯 云中翡翠。
天荒地老 海誓山盟。
生则同襟 死则同穴。
生有此女 夫复何求""".split("。")
setup(1280,720# 设置窗口大小
colormode(255# 使用的颜色模式, 整数还是小数
up()
a, b = -500, 280
goto(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)



 

简单的图形例子

主要是通过改变角度和位置偏移, 做出各种效果

[Python] 纯文本查看 复制代码
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 = 10
 
for 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/Amd794
 
 
 
import time
import turtle as t
from turtle import *
 
setup(1280,720)
t.speed(0)
t.pensize(1)
length = 5
path = 'FX'
angle = 25
up()
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 path
 
 
for _ in range(5):
    path = apply_rules(path, rules)
draw_path(path, expalnation)



综合以上, 来一个整合:

[Python] 纯文本查看 复制代码
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/Amd794
 
 
from turtle import *
import time
import turtle as t
 
 
def 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 path
 
 
def 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 = 6
path = 'F'
angle = 27
rules = {
    '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()


posted @ 2019-04-15 22:52  Amd794  阅读(6012)  评论(3编辑  收藏  举报