首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python-turtle库绘图

Python-turtle库绘图

作者头像
wangmcn
发布2022-07-25 16:29:27
发布2022-07-25 16:29:27
2.2K0
举报
文章被收录于专栏:AllTests软件测试AllTests软件测试

turtle库绘图

目录

  • 一箭穿心
  • 发射爱心
  • 哆啦A梦
  • 小鸭子
  • 爱情树
  • 玫瑰花
  • 画星星
  • 表白
  • 小猪佩奇
  • 时钟

turtle(海龟)库是turtle绘图体系的Python实现,turtle库是一种标准库,是Python自带的。

turtle(海龟)是一种真实的存在,有一个海龟在窗口的正中心,在画布上游走,走过的轨迹形成了绘制的图形,海龟由程序控制,可改变颜色,宽度等。

基础知识:

1、画布(canvas)

代码语言:javascript
复制
# 参数分别为画布的宽(单位像素), 高, 背景颜色。
turtle.screensize(canvwidth=None, canvheight=None, bg=None)

2、画笔

2.1、画笔的状态:在画布上,默认有一个坐标原点为画布中心的坐标轴,坐标原点上有一只面朝x轴正方向小乌龟。这里我们描述小乌龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向),turtle绘图中,就是使用位置方向描述小乌龟(画笔)的状态。

2.2、画笔的属性:画笔(画笔的属性,颜色、画线的宽度等)

代码语言:javascript
复制
# 设置画笔的宽度。
turtle.pensize()
# 没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", "red",也可以是RGB 3元组。
turtle.pencolor()
# 设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。
turtle.speed(speed)

2.3、绘图命令:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令。

(1)画笔运动命令

(2)画笔控制命令

(3)全局控制命令

(4)其他命令

一箭穿心

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from turtle import *

def go_to(x, y):
   up()
   goto(x, y)
   down()

def big_Circle(size):  #函数用于绘制心的大圆
   speed(5)
   for i in range(150):
       forward(size)
       right(0.3)

def small_Circle(size):  #函数用于绘制心的小圆
   speed(10)
   for i in range(210):
       forward(size)
       right(0.786)
 
def line(size):
   speed(5)
   forward(51*size)
 
def heart( x, y, size):
   go_to(x, y)
   left(150)
   begin_fill()
   line(size)
   big_Circle(size)
   small_Circle(size)
   left(120)
   small_Circle(size)
   big_Circle(size)
   line(size)
   end_fill()
 
def arrow():
   pensize(10)
   setheading(0)
   go_to(-400, 0)
   left(15)
   forward(150)
   go_to(339, 178)
   forward(150)
 
def arrowHead():
   pensize(1)
   speed(5)
   color('red', 'red')
   begin_fill()
   left(120)
   forward(20)
   right(150)
   forward(35)
   right(120)
   forward(35)
   right(150)
   forward(20)
   end_fill()
 
if __name__ == "__main__":
   pensize(4)
   color('red', 'pink')
   getscreen().tracer(30, 0) # 取消注释后,快速显示图案
   heart(200, 0, 1) # 画出第一颗心,前面两个参数控制心的位置,函数最后一个参数可控制心的大小
   setheading(0) # 使画笔的方向朝向x轴正方向
   heart(-80, -100, 1.5) # 画出第二颗心
   arrow() # 画出穿过两颗心的直线
   arrowHead() # 画出箭的箭头
   go_to(400, -300)
   done()

运行效果:

发射爱心

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from turtle import *

def go_to(x, y):
    up()
    goto(x, y)
    down()

def head(x,y,r):
    go_to(x,y)
    speed(1)
    circle(r)
    leg(x,y)
 
def leg(x,y):
    right(90)
    forward(180)
    right(30)
    forward(100)
    left(120)
    go_to(x,y-180)
    forward(100)
    right(120)
    forward(100)
    left(120)
    hand(x,y)

def hand(x,y):
    go_to(x,y-60)
    forward(100)
    left(60)
    forward(100)
    go_to(x, y - 90)
    right(60)
    forward(100)
    right(60)
    forward(100)
    left(60)
    eye(x,y)
 
def eye(x,y):
    go_to(x-50,y+130)
    right(90)
    forward(50)
    go_to(x+40,y+130)
    forward(50)
    left(90)

def big_Circle(size):
    speed(20)
    for i in range(150):
        forward(size)
        right(0.3)

def line(size):
    speed(1)
    forward(51*size)
 
def small_Circle(size):
    speed(10)
    for i in range(210):
        forward(size)
        right(0.786)

def heart(x, y, size):
    go_to(x, y)
    left(150)
    begin_fill()
    line(size)
    big_Circle(size)
    small_Circle(size)
    left(120)
    small_Circle(size)
    big_Circle(size)
    line(size)
    end_fill()
 
if __name__ == "__main__":
    pensize(2)
    color('red', 'pink')
    head(-120, 100, 100)
    heart(250, -80, 1)
    go_to(200, -300)
    write("To: 智慧与美貌并存的you", move=True, align="left", font=("楷体", 20, "normal"))
    done()

运行效果:

哆啦A梦

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import turtle as t

if __name__ == "__main__":
    t.speed(10)
    t.pensize(8)
    t.hideturtle()
    t.screensize(500, 500, bg='white')

    t.fillcolor('#00A1E8')
    t.begin_fill()
    t.circle(120)
    t.end_fill()

    t.pensize(3)
    t.fillcolor('white')
    t.begin_fill()
    t.circle(100)
    t.end_fill()

    t.pu()
    t.home()
    t.goto(0, 134)
    t.pd()
    t.pensize(4)
    t.fillcolor("#EA0014")
    t.begin_fill()
    t.circle(18)
    t.end_fill()

    t.pu()
    t.goto(7, 155)
    t.pensize(2)
    t.color('white', 'white')
    t.pd()
    t.begin_fill()
    t.circle(4)
    t.end_fill()

    t.pu()
    t.goto(-30, 160)
    t.pensize(4)
    t.pd()
    t.color('black', 'white')
    t.begin_fill()
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            t.lt(3)
            t.fd(a)
        else:
            a = a - 0.08
            t.lt(3)
            t.fd(a)
    t.end_fill()

    t.pu()
    t.goto(30, 160)
    t.pensize(4)
    t.pd()
    t.color('black', 'white')
    t.begin_fill()
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            t.lt(3)
            t.fd(a)
        else:
            a = a - 0.08
            t.lt(3)
            t.fd(a)
    t.end_fill()

    t.pu()
    t.goto(-38, 190)
    t.pensize(8)
    t.pd()
    t.right(-30)
    t.forward(15)
    t.right(70)
    t.forward(15)

    t.pu()
    t.goto(15, 185)
    t.pensize(4)
    t.pd()
    t.color('black', 'black')
    t.begin_fill()
    t.circle(13)
    t.end_fill()

    t.pu()
    t.goto(13, 190)
    t.pensize(2)
    t.pd()
    t.color('white', 'white')
    t.begin_fill()
    t.circle(5)
    t.end_fill()

    t.pu()
    t.home()
    t.goto(0, 134)
    t.pensize(4)
    t.pencolor('black')
    t.pd()
    t.right(90)
    t.forward(40)

    t.pu()
    t.home()
    t.goto(0, 124)
    t.pensize(3)
    t.pencolor('black')
    t.pd()
    t.left(10)
    t.forward(80)

    t.pu()
    t.home()
    t.goto(0, 114)
    t.pensize(3)
    t.pencolor('black')
    t.pd()
    t.left(6)
    t.forward(80)

    t.pu()
    t.home()
    t.goto(0, 104)
    t.pensize(3)
    t.pencolor('black')
    t.pd()
    t.left(0)
    t.forward(80)

    t.pu()
    t.home()
    t.goto(0, 124)
    t.pensize(3)
    t.pencolor('black')
    t.pd()
    t.left(170)
    t.forward(80)

    t.pu()
    t.home()
    t.goto(0, 114)
    t.pensize(3)
    t.pencolor('black')
    t.pd()
    t.left(174)
    t.forward(80)

    t.pu()
    t.home()
    t.goto(0, 104)
    t.pensize(3)
    t.pencolor('black')
    t.pd()
    t.left(180)
    t.forward(80)

    t.pu()
    t.goto(-70, 70)
    t.pd()
    t.color('black', 'red')
    t.pensize(6)
    t.seth(-60)
    t.begin_fill()
    t.circle(80, 40)
    t.circle(80, 80)
    t.end_fill()

    t.pu()
    t.home()
    t.goto(-80, 70)
    t.pd()
    t.forward(160)

    t.pu()
    t.home()
    t.goto(-50, 50)
    t.pd()
    t.pensize(1)
    t.fillcolor("#eb6e1a")
    t.seth(40)
    t.begin_fill()
    t.circle(-40, 40)
    t.circle(-40, 40)
    t.seth(40)
    t.circle(-40, 40)
    t.circle(-40, 40)
    t.seth(220)
    t.circle(-80, 40)
    t.circle(-80, 40)
    t.end_fill()

    t.pu()
    t.goto(-70, 12)
    t.pensize(14)
    t.pencolor('red')
    t.pd()
    t.seth(-20)
    t.circle(200, 30)
    t.circle(200, 10)

    t.pu()
    t.goto(0, -46)
    t.pd()
    t.pensize(3)
    t.color("black", '#f8d102')
    t.begin_fill()
    t.circle(25)
    t.end_fill()

    t.pu()
    t.goto(-5, -40)
    t.pd()
    t.pensize(2)
    t.color("black", '#79675d')
    t.begin_fill()
    t.circle(5)
    t.end_fill()

    t.pensize(3)
    t.right(115)
    t.forward(7)

    t.mainloop()

运行效果:

小鸭子

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from turtle import *

# 腿
def leg(pos0):
    pensize(8)
    color('#ECC578')
    pu()
    goto(pos0)
    seth(0)
    fd(7)
    seth(-90)
    fd(8.5)
    pd()
    fd(20) # 腿长

# 小红靴
def boot(pos0):
    pensize(2)
    color('#B4070B','#FBA06B')
    pu()
    goto(pos0) # 靴子右上顶点
    pd()
    begin_fill()
    seth(140)
    circle(25,80)
    seth(-80)
    fd(35) # fd(30)左侧线条

    circle(-2,60) # 靴低
    fd(20)
    circle(4,180)
    seth(5)
    fd(30)
    circle(2,60)

    goto(pos0) # 右侧线条
    end_fill()

if __name__ == "__main__":
    # 扁嘴
    pensize(2)

    pu()
    goto(-100,100) # 上嘴最高顶点
    seth(-50)
    pd()
    color('#6C3100','#FADD77')
    begin_fill()
    fd(16)
    vertex_right = pos() # 嘴最右顶点
    rt(50)
    fd(12)
    vertex_down = pos() # 下嘴最低顶点
    rt(80)
    fd(30)
    circle(-3,200)
    vertex_left = pos() # 嘴最左顶点
    goto(-100,100)
    end_fill()
    goto(vertex_left) # 回到最左顶点
    circle(-3,-200) # 扁嘴
    goto(vertex_right)

    # 身体
    # 头颈背尾曲线
    color('#B6A88E')
    pu()
    goto(-100,100)
    pd()

    seth(80)
    circle(-36,160)
    fd(25)
    circle(115,20)
    circle(60,55)
    circle(-200,20)
    circle(110,20)
    color('#7D6A4C')
    circle(40,40)
    color('#B6A88E')
    seth(-100)
    circle(-180,30)
    circle(-20,50)

    # 右鸭腿
    circle(20,70)
    color('#736856')
    circle(-12,120)
    leg_pos1 = pos() # 定位左腿位置
    fd(25)

    # 前胸肚曲线
    pu()
    goto(vertex_down)
    pd()
    seth(-10)
    color('#B9AD9D')
    circle(-40,50)
    circle(-80,48)
    color('#736856')
    circle(250,5)
    circle(50,75)
    color('#B9AD9D')
    circle(220,28)

    # 左鸭腿
    pu()
    seth(175)
    fd(40)
    pd()
    seth(-120)
    fd(8)
    circle(-10,120)
    leg_pos2 = pos() # 定位右腿位置
    fd(15)

    # 眼睛
    color('black')
    # 左眼
    pu()
    goto(vertex_down - (1,-29))
    pd()
    dot(4,'black') # 相比circle(),不需要再额外填充颜色
    # 右眼
    pu()
    goto(vertex_down + (23,20))
    pd()
    dot(4,'black')

    # 翅膀
    color('#BCB2A6')
    pu()
    goto(vertex_down - (-75,130))
    seth(130)
    pd()
    circle(-25,130)
    circle(-100,30)
    fd(85)
    point = pos()
    rt(137)
    fd(52)
    circle(-100,58)

    pu()
    goto(point)
    lt(30)
    pd()
    fd(60)

    pu()
    goto(point)
    pd()
    lt(10)
    fd(70)

    # 腿部
    leg(leg_pos1)
    leg(leg_pos2)
    boot(leg_pos1-(-20,30))
    boot(leg_pos2-(-20,30))

    # 小雨滴
    color('#77DDFF','#D8E8E5')
    fd_ls = [200,140,250,240,230,220,180,250]
    lt_ls = [30,60,60,100,125,170,200,330]
    for i in range(8):
        pu()
        home()
        lt(lt_ls[i])
        fd(fd_ls[i])
        
        pd()
        seth(-78)
        fd(15)
        begin_fill()
        circle(-3,200)
        end_fill()
        fd(15)

    # 文字
    pu()
    goto(vertex_left)
    seth(180)
    fd(150)
    seth(-90)
    fd(300)
    color('black')
    write('turtle',font=("Arial",15,"normal"))

    hideturtle()
    done()

运行效果:

爱情树

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import turtle
import random

# 在(x,y)处画爱心
def love(x,y):
    lv = turtle.Turtle()
    lv.hideturtle()
    lv.up()
    lv.goto(x,y) # 定位到(x,y)
    def curvemove(): # 画圆弧
        for i in range(20):
            lv.right(10)
            lv.forward(2)
    lv.color('red','pink')
    lv.speed(10000000)
    lv.pensize(1)
    # 开始画爱心
    lv.down()
    lv.begin_fill()
    lv.left(140)
    lv.forward(22)
    curvemove()
    lv.left(120)
    curvemove()
    lv.forward(22)
    lv.write("她(他)的文字",font=("Arial",12,"normal"),align="center") # 写上表白的人的名字
    lv.left(140) # 画完复位
    lv.end_fill()

def tree(branchLen,t):
    if branchLen > 5: # 剩余树枝太少要结束递归
        if branchLen<20:
            t.color("green")
            t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
            t.down()
            t.forward(branchLen)
            love(t.xcor(),t.ycor()) # 传输现在turtle的坐标
            t.up()
            t.backward(branchLen)
            t.color("brown")
            return
        t.pensize(random.uniform((branchLen+5)/4-2,(branchLen+6)/4+5))
        t.down()
        t.forward(branchLen)
        # 以下递归
        ang = random.uniform(15,45)
        t.right(ang)
        tree(branchLen-random.uniform(12,16),t) # 随机决定减小长度
        t.left(2*ang)
        tree(branchLen-random.uniform(12,16),t) # 随机决定减小长度
        t.right(ang)
        t.up()
        t.backward(branchLen)

if __name__ == "__main__":
    myWin = turtle.Screen()
    t = turtle.Turtle()
    t.hideturtle()
    t.speed(1000)
    t.left(90)
    t.up()
    t.backward(200)
    t.down()
    t.color("brown")
    t.pensize(32)
    t.forward(60)
    tree(100,t)
    myWin.exitonclick()

运行效果:

玫瑰花

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from turtle import *
import time

if __name__ == "__main__":
    speed(0)
    penup()
    seth(90)
    fd(340)
    seth(0)
    pendown()
    speed(5)
    begin_fill()
    fillcolor('red')
    circle(50, 30)

    for i in range(10):
        fd(1)
        left(10)

    circle(40, 40)

    for i in range(6):
        fd(1)
        left(3)

    circle(80, 40)

    for i in range(20):
        fd(0.5)
        left(5)

    circle(80, 45)

    for i in range(10):
        fd(2)
        left(1)

    circle(80, 25)

    for i in range(20):
        fd(1)
        left(4)

    circle(50, 50)
    time.sleep(0.1)
    circle(120, 55)
    speed(0)
    seth(-90)
    fd(70)
    right(150)
    fd(20)
    left(140)
    circle(140, 90)
    left(30)
    circle(160, 100)
    left(130)
    fd(25)
    penup()
    right(150)
    circle(40, 80)
    pendown()
    left(115)
    fd(60)
    penup()
    left(180)
    fd(60)
    pendown()
    end_fill()
    right(120)
    circle(-50, 50)
    circle(-20, 90)
    speed(1)
    fd(75)
    speed(0)
    circle(90, 110)
    penup()
    left(162)
    fd(185)
    left(170)
    pendown()
    circle(200, 10)
    circle(100, 40)
    circle(-52, 115)
    left(20)
    circle(100, 20)
    circle(300, 20)
    speed(1)
    fd(250)
    penup()
    speed(0)
    left(180)
    fd(250)
    circle(-300, 7)
    right(80)
    circle(200, 5)
    pendown()
    left(60)
    begin_fill()
    fillcolor('green')
    circle(-80, 100)
    right(90)
    fd(10)
    left(20)
    circle(-63, 127)
    end_fill()
    penup()
    left(50)
    fd(20)
    left(180)
    pendown()
    circle(200, 25)
    penup()
    right(150)
    fd(180)
    right(40)
    pendown()
    begin_fill()
    fillcolor('green')
    circle(-100, 80)
    right(150)
    fd(10)
    left(60)
    circle(-80, 98)
    end_fill()
    penup()
    left(60)
    fd(13)
    left(180)
    pendown()
    speed(1)
    circle(-200, 23)

运行效果:

画星星

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import turtle as t

if __name__ == "__main__":
  t.goto(100,0)
  
  for i in range(50):
    t.left(80)
    t.fd(100)
    t.left(135)
    t.fd(105)

运行效果:

表白

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import turtle
import time

# 清屏函数
def clear_all():
    turtle.penup()
    turtle.goto(0, 0)
    turtle.color('white')
    turtle.pensize(800)
    turtle.pendown()
    turtle.setheading(0)
    turtle.fd(300)
    turtle.bk(600)

# 重定位海龟的位置
def go_to(x, y, state):
    turtle.pendown() if state else turtle.penup()
    turtle.goto(x, y)

# 画线
# state为真时海龟回到原点,为假时不回到原来的出发点
def draw_line(length, angle, state):
    turtle.pensize(1)
    turtle.pendown()
    turtle.setheading(angle)
    turtle.fd(length)
    turtle.bk(length) if state else turtle.penup()
    turtle.penup()

# 画箭羽
def draw_feather(size):
    angle = 30                          # 箭的倾角
    feather_num = size//6               # 羽毛的数量
    feather_length = size // 3          # 羽毛的长度
    feather_gap = size//10              # 羽毛的间隔
    for i in range(feather_num):
        draw_line(feather_gap, angle+180, False) # 箭柄,不折返
        draw_line(feather_length, angle + 145, True) # 羽翼,要折返
    draw_line(feather_length, angle + 145, False)
    draw_line(feather_num*feather_gap, angle, False)
    draw_line(feather_length, angle + 145 + 180, False)
    for i in range(feather_num):
        draw_line(feather_gap, angle+180, False) # 箭柄,不折返
        draw_line(feather_length, angle - 145, True) # 羽翼,要折返
    draw_line(feather_length, angle - 145, False)
    draw_line(feather_num*feather_gap, angle, False)
    draw_line(feather_length, angle - 145 + 180, False)

# 画爱心
def draw_heart(size):
    turtle.color('red', 'pink')
    turtle.pensize(2)
    turtle.pendown()
    turtle.setheading(150)
    turtle.begin_fill()
    turtle.fd(size)
    turtle.circle(size * -3.745, 45)
    turtle.circle(size * -1.431, 165)
    turtle.left(120)
    turtle.circle(size * -1.431, 165)
    turtle.circle(size * -3.745, 45)
    turtle.fd(size)
    turtle.end_fill()

# 画箭
def draw_arrow(size):
    angle = 30
    turtle.color('black')
    draw_feather(size)
    turtle.pensize(4)
    turtle.setheading(angle)
    turtle.pendown()
    turtle.fd(size*2)

# 一箭穿心
# 箭的头没有画出来,而是用海龟来代替
def arrow_heart(x, y, size):
    go_to(x, y, False)
    draw_heart(size*1.15)
    turtle.setheading(-150)
    turtle.penup()
    turtle.fd(size*2.2)
    draw_heart(size)
    turtle.penup()
    turtle.setheading(150)
    turtle.fd(size * 2.2)
    draw_arrow(size)

# 画出发射爱心的小人
def draw_people(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.pensize(2)
    turtle.color('black')
    turtle.setheading(0)
    turtle.circle(60, 360)
    turtle.penup()
    turtle.setheading(90)
    turtle.fd(75)
    turtle.setheading(180)
    turtle.fd(20)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(2, 360)
    turtle.setheading(0)
    turtle.penup()
    turtle.fd(40)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(-2, 360)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(20)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(60)
    turtle.fd(10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(40)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(-60)
    turtle.fd(10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(60)
    turtle.setheading(-135)
    turtle.fd(60)
    turtle.bk(60)
    turtle.setheading(-45)
    turtle.fd(30)
    turtle.setheading(-135)
    turtle.fd(35)
    turtle.penup()

# 第一个画面,显示文字
def page0():
    turtle.penup()
    turtle.goto(-350, 0)
    turtle.color('black')
    turtle.write('专属于我们的节日', font=('宋体', 60, 'normal'))
    time.sleep(3)

# 第二个画面,显示发射爱心的小人
def page1():
    turtle.speed(10)
    draw_people(-250, 20)
    turtle.penup()
    turtle.goto(-150, -30)
    draw_heart(14)
    turtle.penup()
    turtle.goto(-20, -60)
    draw_heart(25)
    turtle.penup()
    turtle.goto(250, -100)
    draw_heart(45)
    turtle.hideturtle()
    time.sleep(3)

# 最后一个画面,一箭穿心
def page2():
    turtle.speed(1)
    turtle.penup()
    turtle.goto(-200, -200)
    turtle.color('blue')
    turtle.pendown()
    turtle.write('你的名字 她(他)的文字', font=('wisdom', 40, 'normal'))
    turtle.penup()
    turtle.goto(0, -180)
    draw_heart(10)
    arrow_heart(20, -60, 51)
    turtle.showturtle()

if __name__ == "__main__":
    page0()
    clear_all()
    page1()
    clear_all()
    page2()
    turtle.done()

运行效果:

小猪佩奇

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from turtle import *

def nose(x, y):
    """画鼻子"""
    pensize(5)
    pencolor((255, 155, 192))
    penup()
    # 将海龟移动到指定的坐标
    goto(x, y)
    pendown()
    # 设置海龟的方向(0-东、90-北、180-西、270-南)
    setheading(-30)
    begin_fill()
    fillcolor(255, 192, 203)
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            # 向左转3度
            left(3)
            # 向前走
            forward(a)
        else:
            a = a - 0.08
            left(3)
            forward(a)
    end_fill()
    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    # 设置画笔的颜色(红, 绿, 蓝)
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()
    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()

def head(x, y):
    """画头"""
    color((255, 155, 192), "pink")
    penup()
    goto(x, y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300, -30)
    circle(100, -60)
    circle(80, -100)
    circle(150, -20)
    circle(60, -95)
    setheading(161)
    circle(-300, 15)
    penup()
    goto(-100, 100)
    pendown()
    setheading(-30)
    a = 0.4
    for i in range(60):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            lt(3) # 向左转3度
            fd(a) # 向前走a的步长
        else:
            a = a - 0.08
            lt(3)
            fd(a)
    end_fill()

def ears(x, y):
    """画耳朵"""
    color((255, 155, 192), "pink")
    penup()
    goto(x, y)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 54)
    end_fill()
    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(90)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 56)
    end_fill()

def eyes(x, y):
    """画眼睛"""
    color((255, 155, 192), "white")
    penup()
    setheading(90)
    forward(-20)
    setheading(0)
    forward(-95)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
    color((255, 155, 192), "white")
    penup()
    seth(90)
    forward(-25)
    seth(0)
    forward(40)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()

def cheek(x, y):
    """画脸颊"""
    color((255, 155, 192))
    penup()
    goto(x, y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()

def mouth(x, y):
    """画嘴巴"""
    color(239, 69, 19)
    penup()
    goto(x, y)
    pendown()
    setheading(-80)
    circle(30, 40)
    circle(40, 80)

def body(x, y):
    '''画身体'''
    penup()
    goto(x, y)
    pencolor('red')
    fillcolor(250, 106, 106)
    pendown()
    begin_fill()
    setheading(-66)
    circle(-450, 17)
    setheading(180)
    forward(185)
    setheading(85)
    circle(-450, 17)
    end_fill()
    '''右手'''
    penup()
    goto(110, -45)
    pendown()
    pensize(8)
    pencolor(255, 192, 203)
    setheading(30)
    circle(-400, 10)
    penup()
    goto(167, -5)
    pendown()
    setheading(-120)
    forward(20)
    left(100)
    forward(20)
    '''左手'''
    penup()
    goto(-25, -45)
    pendown()
    pencolor(255, 192, 203)
    setheading(150)
    circle(400, 10)
    penup()
    goto(-78, -6)
    pendown()
    setheading(-60)
    forward(20)
    right(100)
    forward(20)

def feet1(x, y):
    pensize(7)
    pencolor(255, 192, 203)
    penup()
    goto(x, y)
    setheading(-90)
    pendown()
    forward(10)
    penup()
    goto(x - 12, y - 10)
    pendown()
    pencolor(238, 201, 0)
    fillcolor(238, 230, 132)
    begin_fill()
    setheading(0)
    forward(24)
    right(90)
    forward(36)
    right(90)
    forward(40)
    circle(-10, 180)
    forward(16)
    left(90)
    forward(12)
    end_fill()

def feet2(x, y):
    pensize(7)
    pencolor(255, 192, 203)
    penup()
    goto(x, y)
    setheading(-90)
    pendown()
    forward(10)
    penup()
    goto(x - 12, y - 10)
    pendown()
    pencolor(238, 201, 0)
    fillcolor(238, 230, 132)
    begin_fill()
    setheading(0)
    forward(24)
    right(90)
    forward(36)
    right(90)
    forward(40)
    circle(-10, 180)
    forward(16)
    left(90)
    forward(12)
    end_fill()

def tail(x, y):
    pensize(8)
    penup()
    goto(x, y)
    pendown()
    pencolor(255, 192, 203)
    setheading(-5)
    circle(30, 100)
    circle(10, 180)
    circle(20, 150)

def backg(x):
    penup()
    goto(-420, x)
    setheading(0)
    fillcolor(50, 205, 50)
    begin_fill()
    forward(840)
    right(90)
    forward(300)
    right(90)
    forward(840)
    right(90)
    forward(300)
    end_fill()
    setheading(0)
    fillcolor(0, 191, 255)
    begin_fill()
    forward(840)
    left(90)
    forward(600)
    left(90)
    forward(840)
    left(90)
    forward(600)
    end_fill()

def cloude1(x, y):
    """画云"""
    penup()
    goto(x, y)
    setheading(90)
    fillcolor(255, 255, 255)
    begin_fill()
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.14
            # 向左转3度
            left(3)
            # 向前走
            forward(a)
        else:
            a = a - 0.15
            left(3)
            forward(a)
    end_fill()

def cloude2(x, y):
    """画云"""
    penup()
    goto(x, y)
    setheading(90)
    fillcolor(255, 255, 255)
    begin_fill()
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.15
            # 向左转3度
            left(3)
            # 向前走
            forward(a)
        else:
            a = a - 0.13
            left(3)
            forward(a)
    end_fill()

def setting():
    """设置参数"""
    pensize(5)
    # 隐藏海龟
    hideturtle()
    colormode(255)
    color((255, 155, 192), "pink")
    speed(10)

def main():
    """主函数"""
    setting()
    backg(0)
    body(105, -20)
    nose(-100, 100)
    head(-69, 167)
    ears(0, 160)
    eyes(0, 140)
    cheek(80, 10)
    mouth(-20, 30)
    feet1(10, -150)
    feet2(90, -150)
    tail(130, -110)
    cloude1(-200, 200)
    cloude2(300, 300)
    done()

if __name__ == '__main__':
    main()

运行效果:

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from turtle import *
from random import *
from math import *

def tree(n, l):
    pd() # 下笔
    # 阴影效果
    t = cos(radians(heading() + 45)) / 8 + 0.25
    pencolor(t, t, t)
    pensize(n / 3)
    forward(l) # 画树枝

    if n > 0:
        b = random() * 15 + 10 # 右分支偏转角度
        c = random() * 15 + 10 # 左分支偏转角度
        d = l * (random() * 0.25 + 0.7) # 下一个分支的长度
        # 右转一定角度,画右分支
        right(b)
        tree(n - 1, d)
        # 左转一定角度,画左分支
        left(b + c)
        tree(n - 1, d)

        # 转回来
        right(c)
    else:
        # 画叶子
        right(90)
        n = cos(radians(heading() - 45)) / 4 + 0.5
        pencolor(n, n*0.8, n*0.8)
        circle(3)
        left(90)

        # 添加0.3倍的飘落叶子
        if(random() > 0.7):
            pu()
            # 飘落
            t = heading()
            an = -40 + random()*40
            setheading(an)
            dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
            forward(dis)
            setheading(t)

            # 画叶子
            pd()
            right(90)
            n = cos(radians(heading() - 45)) / 4 + 0.5
            pencolor(n*0.5+0.5, 0.4+n*0.4, 0.4+n*0.4)
            circle(2)
            left(90)
            pu()

            #返回
            t = heading()
            setheading(an)
            backward(dis)
            setheading(t)

    pu()
    backward(l) # 退回

if __name__ == "__main__":
  bgcolor(0.5, 0.5, 0.5) # 背景色
  ht() # 隐藏turtle
  speed(0) # 速度,1-10渐进,0最快
  tracer(0, 0)
  pu() # 抬笔
  backward(100)
  left(90) # 左转90度
  pu() # 抬笔
  backward(300) # 后退300
  tree(12, 100) # 递归7层
  done()

运行效果:

时钟

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import turtle
from datetime import *

# 抬起画笔,向前运动一段距离放下
def Skip(step):
  turtle.penup()
  turtle.forward(step)
  turtle.pendown()

def mkHand(name, length):
  # 注册Turtle形状,建立表针Turtle
  turtle.reset()
  Skip(-length * 0.1)
  # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
  turtle.begin_poly()
  turtle.forward(length * 1.1)
  # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
  turtle.end_poly()
  # 返回最后记录的多边形。
  handForm = turtle.get_poly()
  turtle.register_shape(name, handForm)

def Init():
  global secHand, minHand, hurHand, printer
  # 重置Turtle指向北
  turtle.mode("logo")
  # 建立三个表针Turtle并初始化
  mkHand("secHand", 135)
  mkHand("minHand", 125)
  mkHand("hurHand", 90)
  secHand = turtle.Turtle()
  secHand.shape("secHand")
  minHand = turtle.Turtle()
  minHand.shape("minHand")
  hurHand = turtle.Turtle()
  hurHand.shape("hurHand")

  for hand in secHand, minHand, hurHand:
    hand.shapesize(1, 1, 3)
    hand.speed(0)

  # 建立输出文字Turtle
  printer = turtle.Turtle()
  # 隐藏画笔的turtle形状
  printer.hideturtle()
  printer.penup()

def SetupClock(radius):
  # 建立表的外框
  turtle.reset()
  turtle.pensize(7)
  for i in range(60):
    Skip(radius)
    if i % 5 == 0:
      turtle.forward(20)
      Skip(-radius - 20)

      Skip(radius + 20)
      if i == 0:
        turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
      elif i == 30:
        Skip(25)
        turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
        Skip(-25)
      elif (i == 25 or i == 35):
        Skip(20)
        turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
        Skip(-20)
      else:
        turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
      Skip(-radius - 20)
    else:
      turtle.dot(5)
      Skip(-radius)
    turtle.right(6)

def Week(t):
  week = ["星期一", "星期二", "星期三",
      "星期四", "星期五", "星期六", "星期日"]
  return week[t.weekday()]

def Date(t):
  y = t.year
  m = t.month
  d = t.day
  return "%s-%d-%d" % (y, m, d)

def Tick():
  # 绘制表针的动态显示
  t = datetime.today()
  second = t.second + t.microsecond * 0.000001
  minute = t.minute + second / 60.0
  hour = t.hour + minute / 60.0
  secHand.setheading(6 * second)
  minHand.setheading(6 * minute)
  hurHand.setheading(30 * hour)

  turtle.tracer(False)
  printer.forward(65)
  printer.write(Week(t), align="center", font=("Courier", 14, "bold"))
  printer.back(130)
  printer.write(Date(t), align="center", font=("Courier", 14, "bold"))
  printer.home()
  turtle.tracer(True)

  # 100ms后继续调用tick
  turtle.ontimer(Tick, 100)

def main():
  # 打开/关闭龟动画,并为更新图纸设置延迟。
  turtle.tracer(False)
  Init()
  SetupClock(160)
  turtle.tracer(True)
  Tick()
  turtle.mainloop()

if __name__ == "__main__":
  main()

运行效果:动态运行

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-03-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AllTests软件测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档