Python之turtle库画各种有趣的图及源码(更新中)

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

1、安卓小人

#!/usr/bin/env python
import turtle
aj=turtle.Pen()
y=0
aj.speed(5)
#turtle.screensize(200,800)
turtle.bgcolor("black")
#aj.shape("turtle")
def head():
    aj.color("green")
    aj.fd(160)
    x=aj.xcor()
    aj.seth(90)
    aj.begin_fill()
    #aj.color("green")
    aj.circle(x/2,180)
    aj.end_fill()
    aj.penup()
    aj.goto(33,37)
    aj.pendown()
    aj.dot(13,"black")
    aj.penup()
    aj.goto(126,37)
    aj.pendown()
    aj.dot(13,"black")
    aj.penup()
    aj.home()
    aj.pendown()
    aj.hideturtle()
    aj.fd(160)
    aj.seth(90)
    aj.circle(x/2,60)
    aj.right(90)
    aj.pensize(5)
    aj.fd(30)

    aj.penup()
    aj.home()
    #aj.pendown()
    aj.hideturtle()
    aj.fd(160)
    aj.seth(90)
    aj.circle(x/2,120)
    aj.right(90)
    aj.pensize(5)
    aj.pendown()
    aj.fd(30)
    aj.penup()
    aj.home()
    aj.penup()

def body():
    aj.pensize(0)

    aj.home()
    aj.showturtle()
    aj.goto(0,-7)
    aj.pendown()
    aj.begin_fill()
    aj.fd(160)
    aj.right(90)
    aj.fd(120)
    aj.right(90)
    aj.fd(160)
    y=aj.ycor()
    aj.right(90)
    aj.fd(120)
    aj.end_fill()

def legs():
    aj.penup()
    #turtle.color("red")
    aj.goto(33,-169)
    aj.pendown()
    aj.pensize(32)
    aj.fd(43)
    aj.penup()
    aj.goto(130,-169)
    aj.pendown()
    aj.fd(43)
    aj.penup()

def hands():
    aj.home()
    aj.pensize(30)
    aj.goto(-18,-77)
    aj.pendown()
    aj.left(90)
    aj.fd(65)
    aj.penup()
    aj.goto(179,-77)
    aj.pendown()
    aj.fd(65)
    aj.penup()
    aj.hideturtle
    aj.fd(100)
    aj.hideturtle()
    aj.circle(100)
    aj.circle(100,360,59)
    aj.reset()
    turtle.bgcolor("black")
    turtle.pencolor("green")
    turtle.hideturtle()
    turtle.goto(-300,0)
    aj.hideturtle
    turtle.write("Thank you for watching....", font = ("Bodoni MT Black", 28, "bold"))
    turtle.penup()
    turtle.goto(-40,-170)
    turtle.pendown()
    turtle.pencolor("yellow")
    turtle.write("Developed by 一个超会写Bug的安太狼", font = ("Palatino Linotype", 22, "bold"))

head()
body()
legs()
hands()
turtle.done()

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第1张图片

2、龙形曲线(Dragon Curve)

又叫分形龙,是一种自相似碎形曲线的统称,因形似龙的蜿蜒盘曲而得名。

# -*- coding: utf-8 -*-

from turtle import *
length = 5
angle  = 90
setup(1280,720)
up()
goto(300,-100)

down()
def draw_path(path):
    for symbol in path:
        if symbol == 'f':
            import random
            colormode(255)
            color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
            fd(length)
        elif symbol == '-':
            lt(angle)
        elif symbol == '+':
            rt(angle)

def apply_path(rules,path):
    lit = [x for x in path]
    for i in range(len(lit)):
        symbol = lit[i]
        if symbol == 'x':
            lit[i] = rules[symbol]
        elif symbol == 'y':
            lit[i] = rules[symbol]
    path = ''.join(lit)
    return path

rules = {
     
    'x':'x+yf+',
    'y':'-fx-y'
}
path = 'fx'
speed(0)
for i in range(13):
    path = apply_path(rules,path)
draw_path(path)
done()

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第2张图片

3、樱桃树

# -*- coding: utf-8 -*-

import turtle

toplevel = 8  # 一共递归6层
angle = 30
rangle = 15


def drawTree(length, level):
    turtle.left(angle)  # 绘制左枝
    turtle.color("black")
    turtle.forward(length)

    if level == toplevel:  # 叶子
        turtle.color("pink")
        turtle.circle(2, 360)

    if level < toplevel:  # 在左枝退回去之前递归
        drawTree(length - 10, level + 1)
    turtle.back(length)

    turtle.right(angle + rangle)  # 绘制右枝
    turtle.color("black")
    turtle.forward(length)

    if level == toplevel:  # 叶子
        turtle.color("pink")
        turtle.circle(2, 360)

    if level < toplevel:  # 在右枝退回去之前递归
        drawTree(length - 10, level + 1)
        turtle.color("black")
    turtle.back(length)
    turtle.left(rangle)


turtle.left(90)
turtle.penup()
turtle.back(300)
turtle.pendown()
turtle.forward(100)
turtle.speed('fastest')
drawTree(80, 1)

turtle.done()

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第3张图片

4、科赫雪花

import turtle as t
from turtle import *
import random

def draw_path(path):
    t.colormode(255)
    t.color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
    for symbol in path:
        if symbol == 'F':
            forward(length)
        elif symbol == '-':
            right(angle)
        elif symbol == '+':
            left(angle)

def apply_rule(path):
    rule = 'F+F--F+F'
    return path.replace('F',rule)

length = .5
angle  = 60
setup(1280,720)
bgcolor('black')
up()
color("#0fe6ca")
goto(0,0)
down()
path = 'F--F--F'
speed(0)
up()
goto(-440,-250)
down()
for i in range(5):
    path = apply_rule(path)
draw_path(path)
draw_path(path)
draw_path(path)
a,b = pos()
for i in range(3):
    up()
    a += 250
    goto(a,b)
    down()
    draw_path(path)
    draw_path(path)
    draw_path(path)
b += 220
for i in range(2):
    up()
    a -= 250
    goto(a,b)
    down()
    draw_path(path)
    draw_path(path)
    draw_path(path)
b += 220
for i in range(2):

    draw_path(path)
    draw_path(path)
    draw_path(path)
    up()
    a += 130
    goto(a,b)
    down()

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第4张图片

5、视觉冲击1

import turtle as t
from turtle import *


angle = 60 #通过改变角度,绘制出各种多边形
t.setup(1280,720)
t.bgcolor('black')
t.pensize(2)
randomColor = ['red','blue','green','purple','gold','pink']
t.speed(0)
for i in range(600):
      t.color(randomColor[i%6])
      t.fd(i)
      t.rt(angle+1)
up()
color("#0fe6ca")
goto(0,0)
down()
t.done()

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第5张图片

6、视觉冲击2

# -*- coding: utf-8 -*-

import turtle as t
from turtle import *

angle = 60 #通过改变角度,绘制出各种多边形
t.bgcolor('black')
t.pensize(2)
randomColor = ['red','blue','green','purple','gold','pink']
t.speed(0)
for i in range(200):
      t.color(randomColor[i%6])
      t.circle(i)
      t.rt(angle+1)
up()
color("#0fe6ca")
goto(0,0)
down()

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第6张图片

7、视觉冲击3

from turtle import *
import time

speed(0)
colormode(255)
clrs = ["MidnightBlue", "Navy", "DarkBlue", "MediumBlue", "RoyalBlue", "MediumSlateBlue", "CornflowerBlue",
        "DodgerBlue", "DeepskyBlue", "LightSkyBlue", "SkyBlue", "LightBlue"]

time.sleep(2)

for j in range(120):

    cn = 0
    c = 30
    f = 70

    for i in range(12):
        pencolor(clrs[cn])
        circle(c)
        left(90)
        penup()
        forward(f)
        right(90)
        pendown()
        c = c * 0.8
        f = f * 0.8
        circle(c)
        cn = cn + 1

    penup()
    goto(0, 0)
    forward(5)
    right(3)
    pendown()

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第7张图片

8、希尔伯特曲线:

# -*- coding: utf-8 -*-

from turtle import *
import random
length = 10
angle  = 90
setup(1280,720)
up()

goto(-640,-360)
down()
def draw_path(path):
    for symbol in path:
        if symbol == 'f':
            colormode(255)
            color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
            fd(length)
        elif symbol == '+':
            lt(angle)
        elif symbol == '-':
            rt(angle)

def apply_path(rules,path):
    lit = [x for x in path]
    for i in range(len(lit)):
        symbol = lit[i]
        if symbol == 'x':
            lit[i] = rules[symbol]
        elif symbol == 'y':
            lit[i] = rules[symbol]
    path = ''.join(lit)
    return path

rules = {
     
    'x':'+yf-xfx-fy+',
    'y':'-xf+yfy+fx-'
}
path = 'x'
speed(0)
for i in range(7):
    path = apply_path(rules,path)
draw_path(path)
done()

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第8张图片

9、Sierpiński箭头曲线

# -*- coding: utf-8 -*-


from turtle import *
length = 5
angle = -60
setup(1280,720)
up()

goto(-640,-350)
down()
def draw_path(path):
    for symbol in path:
        if symbol == 'A' or symbol == 'B':
            import random
            colormode(255)
            color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
            forward(length)
        elif symbol == '-':
            right(angle)
        elif symbol == '+':
            left(angle)
    ht()

def apply_rules(path,rules):
    lit = [_ for _ in path]
    for i in range(len(lit)):
        symbol = lit[i]
        if symbol in rules:
            lit[i] = rules[symbol]
    path = ''.join(lit)
    return path


rules = {
     
    'A':'B-A-B',
    'B':'A+B+A'
}
path = 'A'
speed(0)
for i in range(7):
    path = apply_rules(path,rules)
draw_path(path)
up()
goto(0,-340)
angle = 60
down()
draw_path(path)
up()
goto(0,-340)
angle = -60
down()
draw_path(path)


效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第9张图片

10、Koch曲线

# -*- coding: utf-8 -*-

from turtle import *
import random
length = 2
angle  = 90
setup(1280,720)
up()
goto(-600,-350)
down()
def draw_path(path):
    for symbol in path:
        if symbol == 'F':
            colormode(255)
            color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
            forward(length)
        elif symbol == '-':
            right(angle)
        elif symbol == '+':
            left(angle)
    ht()

def apply_rule(path):
    rule = 'F+F-F-F+F'
    return path.replace('F',rule)

path = 'F'
speed(0)
for i in range(5):
    path = apply_rule(path)
for i in range(5):
    draw_path(path)
up()
goto(-478,-228)
down()
for i in range(4):
    draw_path(path)
up()
goto(-356,-106)
down()
for i in range(3):
    draw_path(path)
up()
goto(-235,16)
down()
for i in range(2):
    draw_path(path)
up()
goto(-115,137)
down()
draw_path(path)

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第10张图片

11、月亮代表我的心

# -*- coding: utf-8 -*-

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)
 

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()

效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第11张图片

12、生则同寝 死则同穴

# -*- coding: utf-8 -*-


from turtle import *
import random
import time

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)
up()
color("#262626;")
goto(-600,300)
write('Author:Mifen',font=("微软雅黑", 18))
goto(-600,250)
write('E-mail :[email protected]',font=("微软雅黑", 18))
goto(-600, 200)
write('Code :https://github.com/Amd794/Python123', font=("微软雅黑", 18))
goto(-600,-350)
down()
ht()

部分效果图:
Python之turtle库画各种有趣的图及源码(更新中)_第12张图片

Python之turtle库画各种有趣的图及源码(更新中)_第13张图片
Python之turtle库画各种有趣的图及源码(更新中)_第14张图片
Python之turtle库画各种有趣的图及源码(更新中)_第15张图片

未完待续

你可能感兴趣的:(python)