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

七、生日快乐

画生日蛋糕,庆祝生日快乐。先看效果图:

源码如下:

# 画生日蛋糕,庆祝生日快乐
import turtle as t
import math as m
import random as r

# 计算对应于角度的 x 坐标
def drawX(a, i):
    angle = m.radians(i)
    return a * m.cos(angle)

# 计算对应于角度的 y 坐标
def drawY(b, i):
    angle = m.radians(i)
    return b * m.sin(angle)

# 初始化画布
t.bgcolor("#d3dae8")  # 设定背景颜色
t.setup(width=900, height=600, startx=0, starty=0)  # 设置窗口的大小和初始位置
t.title("祝你生日快乐!")  # 设置窗口标题

t.speed(10)
t.penup()
t.goto(150, 0)
t.pendown()

# 1 绘制蛋糕底层
t.pencolor("white")
t.begin_fill()
for i in range(360):
    x = drawX(150, i)
    y = drawY(60, i)
    t.goto(x, y)
t.fillcolor("#fef5f7")
t.end_fill()
# 2
t.begin_fill()
for i in range(180):
    x = drawX(150, -i)
    y = drawY(70, -i)
    t.goto(x, y)
for i in range(180, 360):
    x = drawX(150, i)
    y = drawY(60, i)
    t.goto(x, y)
t.fillcolor("#f2d7dd")
t.end_fill()
# 3
t.pu()
t.goto(120, 0)
t.pd()
t.begin_fill()
for i in range(360):
    x = drawX(120, i)
    y = drawY(48, i)
    t.goto(x, y)
t.fillcolor("#cbd9f9")
t.end_fill()
# 4
t.begin_fill()
t.pencolor("#fee48c")
for i in range(540):
    x = drawX(120, i)
    y = drawY(48, i) + 70
    t.goto(x, y)
t.goto(-120, 0)
t.fillcolor("#cbd9f9")
t.end_fill()
# 5
t.pu()
t.goto(120, 70)
t.pd()
t.pencolor("#fff0f3")
t.begin_fill()
for i in range(360):
    x = drawX(120, i)
    y = drawY(48, i) + 70
    t.goto(x, y)
t.fillcolor("#fff0f3")
t.end_fill()
# 6
t.pu()
t.goto(110, 70)
t.pd()
t.pencolor("#fff9fb")
t.begin_fill()
for i in range(360):
    x = drawX(110, i)
    y = drawY(44, i) + 70
    t.goto(x, y)
t.fillcolor("#fff9fb")
t.end_fill()
# 7
t.pu()
t.goto(120, 0)
t.pd()
t.begin_fill()
t.pencolor("#ffa79d")
for i in range(180):
    x = drawX(120, -i)
    y = drawY(48, -i) + 10
    t.goto(x, y)
t.goto(-120, 0)
for i in range(180, 360):
    x = drawX(120, i)
    y = drawY(48, i)
    t.goto(x, y)
t.fillcolor("#ffa79d")
t.end_fill()
# 8
t.pu()
t.goto(120, 70)
t.pd()
t.begin_fill()
t.pensize(4)
t.pencolor("#fff0f3")
for i in range(1800):
    x = drawX(120, 0.1 * i)
    y = drawY(-18, i) + 10
    t.goto(x, y)
t.goto(-120, 70)
t.pensize(1)
for i in range(180, 360):
    x = drawX(120, i)
    y = drawY(48, i) + 70
    t.goto(x, y)
t.fillcolor("#fff0f3")
t.end_fill()
# 9
t.pu()
t.goto(80, 70)
t.pd()
t.begin_fill()
t.pencolor("#6f3732")
t.goto(80, 120)
for i in range(180):
    x = drawX(80, i)
    y = drawY(32, i) + 120
    t.goto(x, y)
t.goto(-80, 70)
for i in range(180, 360):
    x = drawX(80, i)
    y = drawY(32, i) + 70
    t.goto(x, y)
t.fillcolor("#6f3732")
t.end_fill()
# 10
t.pu()
t.goto(80, 120)
t.pd()
t.pencolor("#ffaaa0")
t.begin_fill()
for i in range(360):
    x = drawX(80, i)
    y = drawY(32, i) + 120
    t.goto(x, y)
t.fillcolor("#ffaaa0")
t.end_fill()
# 11
t.pu()
t.goto(70, 120)
t.pd()
t.pencolor("#ffc3be")
t.begin_fill()
for i in range(360):
    x = drawX(70, i)
    y = drawY(28, i) + 120
    t.goto(x, y)
t.fillcolor("#ffc3be")
t.end_fill()
# 12
t.pu()
t.goto(80, 120)
t.pd()
t.begin_fill()
t.pensize(3)
t.pencolor("#ffaaa0")
for i in range(1800):
    x = drawX(80, 0.1 * i)
    y = drawY(-12, i) + 80
    t.goto(x, y)
t.goto(-80, 120)
t.pensize(1)
for i in range(180, 360):
    x = drawX(80, i)
    y = drawY(32, i) + 120
    t.goto(x, y)
t.fillcolor("#ffaaa0")
t.end_fill()
# 13
t.pu()
t.goto(64, 120)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
    x = drawX(4, i) + 60
    y = drawY(1, i) + 120
    t.goto(x, y)
t.goto(64, 170)
for i in range(540):
    x = drawX(4, i) + 60
    y = drawY(1, i) + 170
    t.goto(x, y)
t.goto(56, 120)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
    t.goto(64, 120 + 10 * i)
    t.pu()
    t.goto(56, 120 + 10 * i)
    t.pd()
t.pu()
t.goto(60, 170)
t.pd()
t.goto(60, 180)
t.pensize(1)
#
t.pu()
t.goto(64, 190)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
    x = drawX(4, i) + 60
    y = drawY(10, i) + 190
    t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()

# 14
t.pu()
t.goto(-56, 120)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
    x = drawX(4, i) - 60
    y = drawY(1, i) + 120
    t.goto(x, y)
t.goto(-56, 170)
for i in range(540):
    x = drawX(4, i) - 60
    y = drawY(1, i) + 170
    t.goto(x, y)
t.goto(-64, 120)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
    t.goto(-56, 120 + 10 * i)
    t.pu()
    t.goto(-64, 120 + 10 * i)
    t.pd()
t.pu()
t.goto(-60, 170)
t.pd()
t.goto(-60, 180)
t.pensize(1)
#
t.pu()
t.goto(-56, 190)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
    x = drawX(4, i) - 60
    y = drawY(10, i) + 190
    t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
# 15
t.pu()
t.goto(0, 130)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
    x = drawX(4, i)
    y = drawY(1, i) + 130
    t.goto(x, y)
t.goto(4, 180)
for i in range(540):
    x = drawX(4, i)
    y = drawY(1, i) + 180
    t.goto(x, y)
t.goto(-4, 130)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
    t.goto(4, 130 + 10 * i)
    t.pu()
    t.goto(-4, 130 + 10 * i)
    t.pd()
t.pu()
t.goto(0, 180)
t.pd()
t.goto(0, 190)
t.pensize(1)
#
t.pu()
t.goto(4, 200)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
    x = drawX(4, i)
    y = drawY(10, i) + 200
    t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
# 16
t.pu()
t.goto(30, 110)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
    x = drawX(4, i) + 30
    y = drawY(1, i) + 110
    t.goto(x, y)
t.goto(34, 160)
for i in range(540):
    x = drawX(4, i) + 30
    y = drawY(1, i) + 160
    t.goto(x, y)
t.goto(26, 110)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
    t.goto(34, 110 + 10 * i)
    t.pu()
    t.goto(26, 110 + 10 * i)
    t.pd()
t.pu()
t.goto(30, 160)
t.pd()
t.goto(30, 170)
t.pensize(1)
#
t.pu()
t.goto(34, 180)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
    x = drawX(4, i) + 30
    y = drawY(10, i) + 180
    t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
# 17
t.pu()
t.goto(-30, 110)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
    x = drawX(4, i) - 30
    y = drawY(1, i) + 110
    t.goto(x, y)
t.goto(-26, 160)
for i in range(540):
    x = drawX(4, i) - 30
    y = drawY(1, i) + 160
    t.goto(x, y)
t.goto(-34, 110)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
    t.goto(-26, 110 + 10 * i)
    t.pu()
    t.goto(-34, 110 + 10 * i)
    t.pd()
t.pu()
t.goto(-30, 160)
t.pd()
t.goto(-30, 170)
t.pensize(1)
#
t.pu()
t.goto(-26, 180)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
    x = drawX(4, i) - 30
    y = drawY(10, i) + 180
    t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()

# 随机绘制彩点
color = ["#e28cb9", "#805a8c", "#eaa989", "#6e90b7", "#b8b68f", "#e174b5", "#cf737c", "#7c8782"]
for i in range(80):
    t.pu()
    x = r.randint(-120, 120)
    y = r.randint(-25, 30)
    t.goto(x, y)
    t.pd()
    t.dot(r.randint(2, 5), color[r.randint(0, 7)])
for i in range(40):
    t.pu()
    x = r.randint(-90, 90)
    y = r.randint(-35, 10)
    t.goto(x, y)
    t.pd()
    t.dot(r.randint(2, 5), color[r.randint(0, 7)])

for i in range(40):
    t.pu()
    x = r.randint(-80, 80)
    y = r.randint(60, 90)
    t.goto(x, y)
    t.pd()
    t.dot(r.randint(2, 5), color[r.randint(0, 7)])
for i in range(30):
    t.pu()
    x = r.randint(-50, 50)
    y = r.randint(45, 70)
    t.goto(x, y)
    t.pd()
    t.dot(r.randint(2, 5), color[r.randint(0, 7)])
for i in range(50):
    t.pu()
    x = r.randint(-500, 500)
    y = r.randint(120, 300)
    t.goto(x, y)
    t.pd()
    t.dot(r.randint(3, 5), color[r.randint(0, 7)])

# 绘制贺卡文字
t.seth(90)
t.pu()
t.goto(0, 0)
t.fd(210)
t.left(90)
t.fd(170)
t.pd()
t.write("Happy Birthday", font=("Curlz MT", 50))

t.color('blue')
t.penup()
t.goto(-400, 210)
t.pendown()
t.write('致:某某  ', font=('楷体', 32, 'bold'))
t.color('red')
t.penup()
t.goto(-300, 50)
t.pendown()
t.write('祝 你 生 日 快 乐!前 程 似 锦!', font=('楷体', 30, 'bold'))
t.color('blue')
t.penup()
t.goto(100, -220)
t.pendown()
t.done()

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐