python好之turtle例子集锦
这些程序大多取自或借鉴网上源码,整理在此,便于初学者借鉴,提高学习兴趣。包括:风车动画、小轿车、新年贺卡、动态贺卡、科赫曲线、谢尔宾斯基三角
一、风车动画
效果图如下:
源代码如下:
import turtle as t
t.pensize(2)
t.hideturtle()
windSpeed = 2
radius = 50
def windmill(c):
t.pencolor(c)
t.tracer(False) #隐藏绘制轨迹,之后用update()直接出图
for i in range(4):
t.forward(2*radius)
t.right(90)
t.circle(-radius,180)
while True:
windmill('red')
t.update() #刷新图画
windmill('white') #在此用背景色白色重绘风车,以此清除之前的黑色轨迹
t.right(windSpeed)
t.done()
二、小轿车
源码如下:
import turtle as t
#车身
t.penup()
t.goto(-130,0)
t.pencolor("red")
t.pendown()
t.goto(-75,0)
t.penup()
t.goto(-25,0)
t.pendown()
t.pencolor("red")
t.goto(50,0)
t.penup()
t.goto(100,0)
t.pendown()
t.pencolor("red")
t.goto(125,0)
t.color("red")
t.begin_fill()
t.goto(125,40)
t.goto(120,40)
t.goto(65,70)
t.goto(-40,70)
t.goto(-75,40)
t.goto(-105,40)
t.goto(-130,0)
t.end_fill()
#轮
t.penup()
t.goto(-50,-25)
t.color("black")
t.begin_fill()
t.pendown()
t.circle(25)
t.end_fill()
t.penup()
t.goto(75,-25)
t.color("black")
t.begin_fill()
t.pendown()
t.circle(25)
t.end_fill()
#门
t.penup()
t.pensize(4)
t.goto(-40,65)
t.pendown()
t.pencolor("black")
t.color("blue")
t.begin_fill()
t.goto(-65,40)
t.goto(65,40)
t.goto(65,65)
t.goto(-40,65)
t.end_fill()
t.penup()
t.goto(15,65)
t.pendown()
t.pencolor("black")
t.goto(15,5)
t.penup()
t.goto(-65,40)
t.pendown()
t.goto(-65,5)
t.goto(15,5)
t.goto(65,5)
t.goto(65,40)
t.penup()
t.goto(0,30)
t.pendown()
t.goto(30,30)
t.pensize(2)
#标志
t.penup()
t.goto(-105,40)
t.pencolor("black")
t.pendown()
t.goto(-105,5)
t.goto(-125,5)
t.goto(-110,5)
t.goto(-110,30)
t.penup()
t.goto(-115,5)
t.pendown()
t.goto(-115,23)
t.penup()
t.goto(126,20)
t.color("black")
t.begin_fill()
t.pendown()
t.goto(100,20)
t.goto(100,0)
t.goto(126,0)
t.goto(126,20)
t.end_fill()
#灯
t.penup()
t.goto(-100,16)
t.pendown()
t.color("orange")
t.begin_fill()
t.circle(12,-180)
t.goto(-100,16)
t.end_fill()
t.penup()
t.goto(110,43)
t.pendown()
t.color("orange")
t.begin_fill()
t.circle(11,-180)
t.goto(110,43)
t.end_fill()
t.hideturtle()
t.done()
三、新年贺卡
效果图如下:
源代码如下:
import turtle as t
t.color("red")
t.write("万\n事\n大\n吉", font=("华文行楷", 45, "normal"))
t.penup()
t.goto(80,0)
t.pendown()
t.write("新\n年\n快\n乐", font=("华文行楷", 45, "normal"))
t.penup()
t.goto(-120,110)
t.pendown()
t.write("李\n一\n民\n贺", font=("方正舒体", 20, "normal"))
t.penup()
t.goto(-130,0)
t.color("red")
t.pendown()
t.begin_fill()
t.forward(55)
t.left(90)
t.forward(55)
t.left(90)
t.forward(55)
t.left(90)
t.forward(55)
t.left(90)
t.end_fill()
t.color("white")
t.penup()
t.goto(-100,0)
t.pendown()
t.write("万\n事", font=("华文隶书", 20, "normal"))
t.penup()
t.goto(-130,0)
t.pendown()
t.write("如\n意", font=("华文隶书", 20, "normal"))
t.done()
四、动态贺卡
效果图
源码如下
import turtle as T
import random
import time
t = T.Turtle()
w = T.Screen()
t.hideturtle()
t.getscreen().tracer(5, 0)
w.screensize(bg='maroon')
t.left(90)
t.up()
t.forward(280)
t.down()
t.pensize(3)
n=100
t.color("orange","yellow")
t.begin_fill()
t.left(126)
for i in range(5):
t.forward(n/5)
t.right(144)
t.forward(n/5)
t.left(71)
t.end_fill()
t.left(60)
t.pensize(8)
t.forward(60)
t.right(20)
t.right(116)
t.pensize(6)
t.color('dark green')
n=130
for i in range(6):
time.sleep(0.5)
a=1+i/2
t.begin_fill()
t.left(90)
t.forward(n*a*0.707)
t.left(135)
t.forward(n*a)
t.left(135)
t.forward(n*a*0.707)
t.end_fill()
t.up()
t.left(90)
t.forward(n*a*0.707/3)
t.left(135)
t.forward(n*a/6)
t.left(135)
t.down()
t.up()
t.right(135)
t.forward(30)
t.right(90)
t.forward(157)
t.down()
t.color('saddlebrown')
t.begin_fill()
t.forward(80)
t.right(90)
t.forward(45)
t.right(90)
t.forward(80)
t.right(90)
t.forward(45)
t.end_fill()
t.up()
t.backward(45)
t.right(90)
t.backward(470)
t.down()
def light(l,t):
t.pensize(3)
colors = ["magenta","darkorange","red","blue"]
for i in range(l):
time.sleep(0.2)
b = 70+16*i
a = b/2*random.randint(-100,100)/100
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color("lightyellow",colors[i%4])
t.begin_fill()
t.circle(10)
t.end_fill()
t.up()
t.backward(a)
t.right(90)
t.backward(b)
t.down()
t.pensize(1)
def snow(m,t):
for i in range(m):
a = 400 - 800 * random.random()
b = 600 - 800 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('white')
t.begin_fill()
t.circle(1)
t.end_fill()
t.up()
t.backward(a)
t.right(90)
t.backward(b)
light(24,t)
snow(600, t)
t.goto(-200,200)
my_word = ("Merry Christmas")
t.write(my_word,font=("Edwardian Script ITC",40,"bold"))
time.sleep(0.3)
t.goto(-100,50)
my_word = ("and")
t.write(my_word,font=("Edwardian Script ITC",50,"bold"))
time.sleep(0.3)
t.goto(-150,-100)
my_word = ("Happy New Year")
t.write(my_word,font=("Edwardian Script ITC",40,"bold"))
五、科赫曲线
科赫曲线是一个典型的分形曲线,可以通过不断迭代生成。图形都具有自相似性和无限细节的特点。
效果图
源码如下:
import turtle
# 科赫曲线
def koch_snowflake(length, depth, t):
if depth == 0:
t.forward(length)
else:
length /= 3.0
koch_snowflake(length, depth-1, t)
t.left(60)
koch_snowflake(length, depth-1, t)
t.right(120)
koch_snowflake(length, depth-1, t)
t.left(60)
koch_snowflake(length, depth-1, t)
def draw_koch_snowflake():
screen = turtle.Screen()
screen.setup(600, 600)
screen.tracer(0)
snowflake = turtle.Turtle()
snowflake.hideturtle()
snowflake.speed(0)
snowflake.pu()
snowflake.goto(-150, 100)
snowflake.pd()
snowflake.color("blue")
for _ in range(3):
koch_snowflake(300, 4, snowflake)
snowflake.right(120)
screen.update()
turtle.done()
draw_koch_snowflake()
六、谢尔宾斯基三角
谢尔宾斯基三角也是一个著名的分形图形,由等边三角形不断分割而成。图形都具有自相似性和无限细节的特点。
效果图:
源码如下:
import turtle
# 谢尔宾斯基三角
def sierpinski_triangle(vertices, level):
x1, y1 = vertices[0]
x2, y2 = vertices[1]
x3, y3 = vertices[2]
if level == 0:
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
turtle.begin_fill()
turtle.goto(x2, y2)
turtle.goto(x3, y3)
turtle.goto(x1, y1)
turtle.end_fill()
else:
x12 = (x1 + x2) / 2
y12 = (y1 + y2) / 2
x23 = (x2 + x3) / 2
y23 = (y2 + y3) / 2
x31 = (x3 + x1) / 2
y31 = (y3 + y1) / 2
sierpinski_triangle([(x1, y1), (x12, y12), (x31, y31)], level - 1)
sierpinski_triangle([(x12, y12), (x2, y2), (x23, y23)], level - 1)
sierpinski_triangle([(x31, y31), (x23, y23), (x3, y3)], level - 1)
turtle.speed(0)
turtle.penup()
turtle.goto(-300, -250)
turtle.pendown()
sierpinski_triangle([(-300, 250), (300, 250), (0, -250)], 5)
turtle.hideturtle()
turtle.done()