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()
效果圖:

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()
效果圖:

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()
效果圖:

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()
效果圖:

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()
效果圖:

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()
效果圖:

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()
效果圖:

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()
效果圖:

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)
效果圖:
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)
效果圖:
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()
效果圖:
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 :2952277346@qq.com',font=("微軟雅黑", 18))
goto(-600, 200)
write('Code :https://github.com/Amd794/Python123', font=("微軟雅黑", 18))
goto(-600,-350)
down()
ht()
部分效果圖:
未完待續
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/258433.html
標籤:python
