【Python】turtle

写在前面:因为想要玩耍就开始学这个库了


参考博客

参考简书

官方开发文档

小学生python文档(


常用Turtle和Screen方法

    turtle方法

      海龟动作

          移动和绘制

  •                   forward()  fd() 前进
  •                   backward()  bk()  back() 后退
  •                   right() rt() 右转
  •                   left() lt() 左转
  •                   goto() setpos() setposition() 前往/定位
  •                   setx() 设置x坐标
  •                   sety() 设置y坐标
  •                   setheading() seth() 设置朝向
  •                   home() 返回原点
  •                   circle() 画圆
  •                   dot() 画点
  •                   stamp() 印章
  •                   clearstamp() 清楚印章
  •                   undo() 撤销
  •                   speed() 速度

          获取海龟的状态

  •                   position() pos()位置
  •                   towards() 目标方向
  •                   xcor() x坐标
  •                   ycor() y坐标
  •                   heading() 朝向
  •                   distance() 距离

          设置与度量单位

  •                  degrees() 角度
  •                  radians() 弧度

      画笔控制

          绘图状态

  •                   pendown() pd() down() 画笔落下
  •                   penup() pu() up() 画笔抬起
  •                   pensize() width() 画笔粗细
  •                   pen() 画笔
  •                   isdown() 画笔是否落下

          颜色控制

  •                   color() 颜色
  •                   pencolor() 画笔颜色
  •                   fillcolor() 填充颜色

          填充

  •                   filling() 是否填充
  •                   begin_fill() 开始填充
  •                   end_fill() 结束填充

      更多绘图控制

  •                    reset() 重置
  •                    clear() 清空
  •                    write() 书写

       海龟状态

          可见性

  •                     showturtle() st() 显示海龟
  •                     hideturtle() ht() 隐藏海龟
  •                     isvisible() 是否可见

          外观

  •                      shape()  形状
  •                      resizemode() 大小调整模式
  •                      shapesize() turtlesize() 形状大小
  •                      shearfactor() 剪切因子
  •                      settiltangle() 设置倾角
  •                      tiltangle() 倾角
  •                      tilt() 倾斜
  •                      shapetransform() 变形
  •                      get_shapepoly() 获取形状多边形

       使用事件

  •                      onclick() 当鼠标点击
  •                      onrelease() 当鼠标释放
  •                      ondrag() 当鼠标拖动

       特殊海龟方法

  •                      begin_poly() 开始记录多边形
  •                      end_poly() 结束记录多边形
  •                      get_poly() 获取多边形
  •                      clone() 克隆
  •                      getturtle() getpen() 获取海龟画笔
  •                      getscreen() 获取屏幕
  •                      setundobuffer() 设置撤销缓冲区
  •                      undobufferentries() 撤销缓冲区条目数

    TurtleScreen/Screen 方法

       窗口控制

  •                         bgcolor() 背景颜色
  •                         bgpic() 背景图片
  •                         clear() clearscreen() 清屏
  •                         reset() resetscreen() 重置
  •                         screensize() 屏幕大小
  •                         setworldcoordinates() 设置世界坐标系

        动画控制

  •                         delay() 延迟
  •                         tracer() 追踪
  •                         update() 更新

        使用屏幕事件

  •                         listen() 监听
  •                         onkey() | onkeyrelease() 当键盘按下并释放
  •                         onkeypress() 当键盘按下
  •                         onclick() | onscreenclick() 当点击屏幕
  •                         ontimer() 当达到定时
  •                         mainloop() | done() 主循环

        设置与特殊方法

  •                         mode() 模式
  •                         colormode() 颜色模式
  •                         getcanvas() 获取画布
  •                         getshapes() 获取形状
  •                         register_shape() | addshape() 添加形状
  •                         turtles() 所有海龟
  •                         window_height() 窗口高度
  •                         window_width() 窗口宽度

        输入方法

  •                         textinput() 文本输入
  •                         numinput() 数字输入

        Screen 专有方法

  •                         bye() 退出
  •                         exitonclick() 当点击时退出
  •                         setup() 设置
  •                         title() 标题


代码展示

大丽花

import turtle

turtle=turtle.Turtle()
screen=turtle.getscreen()#获取屏幕

turtle.color('lightcoral', 'lavenderblush')#颜色
turtle.begin_fill()#开始填充
for i in range(50):
    turtle.forward(200)#前进
    turtle.left(170)#左转
turtle.end_fill()#结束填充
screen.mainloop()#主循环 允许循环执行

玫瑰

改了自己喜欢的颜色

import turtle

import time

turtle.speed(5)

# 设置初始位置  

turtle.penup()  

turtle.left(90)  

turtle.fd(200)  

turtle.pendown()  

turtle.right(90)
# 花蕊  

turtle.fillcolor("mediumpurple")  

turtle.begin_fill()  

turtle.circle(10,180)  

turtle.circle(25,110)  

turtle.left(50)  

turtle.circle(60,45)  

turtle.circle(20,170)  

turtle.right(24)  

turtle.fd(30)  

turtle.left(10)  

turtle.circle(30,110)  

turtle.fd(20)  

turtle.left(40)  

turtle.circle(90,70)  

turtle.circle(30,150)  

turtle.right(30)  

turtle.fd(15)  

turtle.circle(80,90)  

turtle.left(15)  

turtle.fd(45)  

turtle.right(165)  

turtle.fd(20)  

turtle.left(155)  

turtle.circle(150,80)  

turtle.left(50)  

turtle.circle(150,90)  

turtle.end_fill()  


# 花瓣1  

turtle.left(150)  

turtle.circle(-90,70)  

turtle.left(20)  

turtle.circle(75,105)  

turtle.setheading(60)  

turtle.circle(80,98)  

turtle.circle(-90,40)  

  

# 花瓣2  

turtle.left(180)  

turtle.circle(90,40)  

turtle.circle(-80,98)  

turtle.setheading(-83)  

  

# 叶子1  

turtle.fd(30)  

turtle.left(90)  

turtle.fd(25)  

turtle.left(45)  

turtle.fillcolor("green")  

turtle.begin_fill()  

turtle.circle(-80,90)  

turtle.right(90)  

turtle.circle(-80,90)  

turtle.end_fill()  

   

turtle.right(135)  

turtle.fd(60)  

turtle.left(180)  

turtle.fd(85)  

turtle.left(90)  

turtle.fd(80)  



# 叶子2  

turtle.right(90)  

turtle.right(45)  

turtle.fillcolor("green")  

turtle.begin_fill()  

turtle.circle(80,90)  

turtle.left(90)  

turtle.circle(80,90)  

turtle.end_fill()  

   

turtle.left(135)  

turtle.fd(60)  

turtle.left(180)  

turtle.fd(60)  

turtle.right(90)  

turtle.circle(200,60) 

 樱花树

这个是我运行的时候觉得最震撼的一个 太好看了

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)#退回

bgcolor(0.5,0.5,0.5)#背景色
ht()#隐藏turtle
speed(10)#速度 1-10渐进,0 最快
tracer(0,0)#非飘落效果是(0,0) 动画效果是(1,0)就是加载的太慢
pu()#抬笔
backward(100)
left(90)#左转90度
pu()#抬笔
backward(300)#后退300
tree(12,100)#递归7层
done()

梅花树

不要问我为什么是梅花树 可能是因为避免重复吧

import turtle as T
import random
import time

# 画躯干(60,t)
def Tree(branch, t):
    time.sleep(0.0005)
    if branch > 3:
        if 8 <= branch <= 12:
            if random.randint(0, 2) == 0:
                t.color('snow')  # 白
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 3)
        elif branch < 8:
            if random.randint(0, 1) == 0:
                t.color('snow')
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 2)
        else:
            t.color('sienna')  # 赭(zhě)色
            t.pensize(branch / 10)  # 6
        t.forward(branch)
        a = 1.5 * random.random()
        t.right(20 * a)
        b = 1.5 * random.random()
        Tree(branch - 10 * b, t)
        t.left(40 * a)
        Tree(branch - 10 * b, t)
        t.right(20 * a)
        t.up()
        t.backward(branch)
        t.down()

# 掉落的花瓣
def Petal(m, t):
    for i in range(m):
        a = 200 - 400 * random.random()
        b = 10 - 20 * random.random()
        t.up()
        t.forward(b)
        t.left(90)
        t.forward(a)
        t.down()
        t.color('lightcoral')  # 淡珊瑚色
        t.circle(1)
        t.up()
        t.backward(a)
        t.right(90)
        t.backward(b)

# 绘图区域
t = T.Turtle()
# 画布大小
w = T.Screen()
t.hideturtle()  # 隐藏画笔
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat')  # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')

# 画樱花的躯干
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()

 

修狗

from turtle import *
import turtle as t
t.screensize(500, 500)
# 【头部轮廓】
t.pensize(5)
t.home()
t.seth(0)
t.pd()
t.color('black')
t.circle(20, 80)  # 0
t.circle(200, 30)  # 1
t.circle(30, 60)  # 2
t.circle(200, 29.5)  # 3
t.color('black')
t.circle(20, 60)  # 4
t.circle(-150, 22)  # 5
t.circle(-50, 10)  # 6
t.circle(50, 70)  # 7
# 确定鼻头大概位置
x_nose = t.xcor()
y_nose = t.ycor()
t.circle(30, 62)  # 8
t.circle(200, 15)  # 9
# 【鼻子】
t.pu()
t.goto(x_nose, y_nose + 25)
t.seth(90)
t.pd()
t.begin_fill()
t.circle(8)
t.end_fill()
# 【眼睛】
t.pu()
t.goto(x_nose + 48, y_nose + 55)
t.seth(90)
t.pd()
t.begin_fill()
t.circle(8)
t.end_fill()
# 【耳朵】
t.pu()
t.color('#444444')
t.goto(x_nose + 100, y_nose + 110)
t.seth(182)
t.pd()
t.circle(15, 45)  # 1
t.color('black')
t.circle(10, 15)  # 2
t.circle(90, 70)  # 3
t.circle(25, 110)  # 4
t.rt(4)
t.circle(90, 70)  # 5
t.circle(10, 15)  # 6
t.color('#444444')
t.circle(15, 45)  # 7
# 【身体】
t.pu()
t.color('black')
t.goto(x_nose + 90, y_nose - 30)
t.seth(-130)
t.pd()
t.circle(250, 28)  # 1
t.circle(10, 140)  # 2
t.circle(-250, 25)  # 3
t.circle(-200, 25)  # 4
t.circle(-50, 85)  # 5
t.circle(8, 145)  # 6
t.circle(90, 45)  # 7
t.circle(550, 5)  # 8
# 【尾巴】
t.seth(0)
t.circle(60, 85)  # 1
t.circle(40, 65)  # 2
t.circle(40, 60)  # 3
t.lt(150)
t.circle(-40, 90)  # 4
t.circle(-25, 100)  # 5
t.lt(5)
t.fd(20)
t.circle(10, 60)  # 6
# 【背部】
t.rt(80)
t.circle(200, 35)
# 【项圈】
t.pensize(20)
t.color('#F03C3F')
t.lt(10)
t.circle(-200, 25)  # 5
# 【爱心铃铛】
t.pu()
t.fd(18)
t.lt(90)
t.fd(18)
t.pensize(6)
t.seth(35)
t.color('#FDAF17')
t.begin_fill()
t.lt(135)
t.fd(6)
t.right(180)  # 画笔掉头
t.circle(6, -180)
t.backward(8)
t.right(90)
t.forward(6)
t.circle(-6, 180)
t.fd(15)
t.end_fill()
# 【前小腿】
t.pensize(5)
t.pu()
t.color('black')
t.goto(x_nose + 100, y_nose - 125)
t.pd()
t.seth(-50)
t.fd(25)
t.circle(10, 150)
t.fd(25)
# 【后小腿】
t.pensize(4)
t.pu()
t.goto(x_nose + 314, y_nose - 125)
t.pd()
t.seth(-95)
t.fd(25)
t.circle(-5, 150)
t.fd(2)
t.hideturtle()
t.done()

小猪佩奇

import turtle as t
t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255, 155, 192), "pink")
t.setup(840, 500)
t.speed(20)
# 鼻子
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
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)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
t.pu()
t.seth(90)
t.fd(25)
t.seth(0)
t.fd(10)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()
# 头
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30)
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
# 耳朵
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()
# 眼睛
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
# 腮
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
# 嘴
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)
# 身体
t.color("red", (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100, 10)
t.circle(300, 30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300, 30)
t.circle(100, 3)
t.color((255, 155, 192), (255, 100, 100))
t.seth(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.end_fill()
# 手
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300, 15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20, 90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300, 15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20, 90)
# 脚
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
# 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.exitonclick()

 哆啦A梦

import turtle as t
# t.speed(5)
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)  # 向左转3度
        t.fd(a)  # 向前走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)  # 向左转3度
        t.fd(a)  # 向前走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()

 小黄人

import turtle as t
# t = turtle.Turtle()
wn = t.Screen()
t.colormode(255)
t.hideturtle()
t.speed(0)
t.penup()
t.pensize(4)
t.goto(100,0)
t.pendown()
t.left(90)
t.color((0,0,0),(255,255,0))
#身体绘制上色
t.begin_fill()
t.forward(200)
t.circle(100,180)
t.forward(200)
t.circle(100,180)
t.end_fill()
#右眼睛绘制上色
t.pensize(12)
t.penup()
t.goto(-100,200)
t.pendown()
t.right(100)
t.circle(500,23)
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()
t.penup()
t.goto(15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()
t.penup()
t.goto(35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#左眼睛绘制上色
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(90)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()
t.penup()
t.goto(-15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()
t.penup()
t.goto(-35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#嘴绘制上色
t.penup()
t.goto(-20,100)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(20,180)
t.left(90)
t.forward(40)
t.end_fill()
#裤子绘制上色
t.penup()
t.goto(-100,0)
t.pendown()
t.seth(0)
t.color("black","blue")
t.begin_fill()
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(160)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.seth(270)
t.penup()
t.goto(-100,0)
t.circle(100,180)
t.end_fill()
#左裤子腰带
t.penup()
t.goto(-70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(45)
t.forward(15)
t.left(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.left(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(-70,30)
t.dot()
#右裤腰带
t.penup()
t.goto(70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(135)
t.forward(15)
t.right(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.right(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(70,30)
t.dot()
#脚
t.penup()
t.goto(4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.left(90)
t.forward(40)
t.seth(20)
t.circle(10,180)
t.circle(400,2)
t.seth(90)
t.forward(20)
t.goto(4,-100)
t.end_fill()
t.penup()
t.goto(-4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(40)
t.seth(20)
t.circle(10,-225)
t.circle(400,-3)
t.seth(90)
t.forward(21)
t.goto(-4,-100)
t.end_fill()
#左手
t.penup()
t.goto(-100,50)
t.pendown()
t.seth(225)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.left(90)
t.forward(35)
t.seth(90)
t.forward(50)
t.end_fill()
#右手
t.penup()
t.goto(100,50)
t.pendown()
t.seth(315)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.right(90)
t.forward(36)
t.seth(90)
t.forward(50)
t.end_fill()
#
t.penup()
t.goto(0,-100)
t.pendown()
t.forward(30)
#
t.penup()
t.goto(0,-20)
t.pendown()
t.color("yellow")
t.begin_fill()
t.seth(45)
t.forward(20)
t.circle(10,180)
t.right(90)
t.circle(10,180)
t.forward(20)
t.end_fill()
#
t.penup()
t.color("black")
t.goto(-100,-20)
t.pendown()
t.circle(30,90)
t.penup()
t.goto(100,-20)
t.pendown()
t.circle(30,-90)
#头顶
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(135)
t.circle(100,40)
t.end_fill()
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(45)
t.circle(100,40)
t.exitonclick()

樱花树2.0

from turtle import *
from random import *
from math import *
class Tree:
    def __init__(self):
        setup(1000, 700)
        bgcolor(1, 1, 1)  # 背景色
        # ht()  # 隐藏turtle
        speed(10)  # 速度 1-10渐进,0 最快
        # tracer(1, 100)    # 设置绘图屏幕刷新频率,参数1设置在正常刷新频次的第参数1次刷新,参数2设置每次刷新的时延
        tracer(0, 0)
        pu()  # 抬笔
        backward(100)
        # 保证笔触箭头方向始终不向下,此处使其左转90度,而不是右转
        left(90)  # 左转90度
        backward(300)  # 后退300
    def tree(self, n, l):
        pd()  # 下笔
        # 阴影效果
        t = cos(radians(heading() + 45)) / 8 + 0.25
        pencolor(t, t, t)
        pensize(n / 1.2)
        forward(l)  # 画树枝
        if n > 0:
            b = random() * 15 + 10  # 右分支偏转角度
            c = random() * 15 + 10  # 左分支偏转角度
            d = l * (random() * 0.25 + 0.7)  # 下一个分支的长度
            # 右转一定角度,画右分支
            right(b)
            self.tree(n - 1, d)
            # 左转一定角度,画左分支
            left(b + c)
            self.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)
            fillcolor(n, n * 0.8, n * 0.8)
            begin_fill()
            circle(3)
            left(90)
            end_fill()
            # 添加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)
                fillcolor(n, n * 0.8, n * 0.8)
                begin_fill()
                circle(2)
                left(90)
                end_fill()
                pu()
                # 返回
                t = heading()
                setheading(an)
                backward(dis)
                setheading(t)
                # pass
        pu()
        backward(l)  # 退回
def main():
    tree = Tree()
    tree.tree(12, 100)  # 递归7层
    done()
if __name__ == '__main__':
    main()

 皮~卡丘!

# coding:utf-8
import turtle as t
import time
# 皮卡丘
# 基础设置
t.screensize(800, 600)
t.pensize(2)  # 设置画笔的大小
t.speed(10)  # 设置画笔速度为10
# 画左偏曲线函数
def radian_left(ang, dis, step, n):
    for i in range(n):
        dis += step  # dis增大step
        t.lt(ang)  # 向左转ang度
        t.fd(dis)  # 向前走dis的步长
def radian_right(ang, dis, step, n):
    for i in range(n):
        dis += step
        t.rt(ang)  # 向左转ang度
        t.fd(dis)  # 向前走dis的步长
# 画耳朵
def InitEars():
    t.color("black", "yellow")
    # 左耳朵曲线
    t.pu()  # 提笔
    t.goto(-50, 100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(110)  # 画笔角度
    t.begin_fill()
    radian_left(1.2, 0.4, 0.1, 40)
    t.setheading(270)  # 画笔角度
    radian_left(1.2, 0.4, 0.1, 40)
    t.setheading(44)  # 画笔角度
    t.forward(32)
    t.end_fill()
    # 右耳朵曲线
    t.pu()  # 提笔
    t.goto(50, 100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(70)  # 画笔角度
    t.begin_fill()
    radian_right(1.2, 0.4, 0.1, 40)
    t.setheading(270)  # 画笔角度
    radian_right(1.2, 0.4, 0.1, 40)
    t.setheading(136)  # 画笔角度
    t.forward(32)
    t.end_fill()
    # 耳朵黑
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(88, 141)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(35)  # 画笔角度
    radian_right(1.2, 1.6, 0.1, 16)
    t.setheading(270)  # 画笔角度
    radian_right(1.2, 0.4, 0.1, 25)
    t.setheading(132)  # 画笔角度
    t.forward(31)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(-88, 141)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(145)  # 画笔角度
    radian_left(1.2, 1.6, 0.1, 16)
    t.setheading(270)  # 画笔角度
    radian_left(1.2, 0.4, 0.1, 25)
    t.setheading(48)  # 画笔角度
    t.forward(31)
    t.end_fill()
# 画尾巴
def InitTail():
    # 尾巴
    t.begin_fill()
    t.fillcolor("yellow")
    t.pu()  # 提笔
    t.goto(64, -140)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(10)  # 画笔角度
    t.forward(20)
    t.setheading(90)  # 画笔角度
    t.forward(20)
    t.setheading(10)  # 画笔角度
    t.forward(10)
    t.setheading(80)  # 画笔角度
    t.forward(100)
    t.setheading(35)  # 画笔角度
    t.forward(80)
    t.setheading(260)  # 画笔角度
    t.forward(100)
    t.setheading(205)  # 画笔角度
    t.forward(40)
    t.setheading(260)  # 画笔角度
    t.forward(37)
    t.setheading(205)  # 画笔角度
    t.forward(20)
    t.setheading(260)  # 画笔角度
    t.forward(25)
    t.setheading(175)  # 画笔角度
    t.forward(30)
    t.setheading(100)  # 画笔角度
    t.forward(13)
    t.end_fill()
# 画脚
def InitFoots():
    # 脚
    t.begin_fill()
    t.fillcolor("yellow")
    t.pensize(2)
    t.pu()  # 提笔
    t.goto(-70, -200)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(225)  # 画笔角度
    radian_left(0.5, 1.2, 0, 12)
    radian_left(35, 0.6, 0, 4)
    radian_left(1, 1.2, 0, 18)
    t.setheading(160)  # 画笔角度
    t.forward(13)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("yellow")
    t.pensize(2)
    t.pu()  # 提笔
    t.goto(70, -200)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(315)  # 画笔角度
    radian_right(0.5, 1.2, 0, 12)
    radian_right(35, 0.6, 0, 4)
    radian_right(1, 1.2, 0, 18)
    t.setheading(20)  # 画笔角度
    t.forward(13)
    t.end_fill()
# 画身体
def InitBody():
    # 外形轮廓
    t.begin_fill()
    t.pu()  # 提笔
    t.goto(112, 0)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(90)  # 画笔角度
    t.circle(112, 180)
    t.setheading(250)  # 画笔角度
    radian_left(1.6, 1.3, 0, 50)
    radian_left(0.8, 1.5, 0, 25)
    t.setheading(255)  # 画笔角度
    radian_left(0.4, 1.6, 0.2, 27)
    radian_left(2.8, 1, 0, 45)
    radian_right(0.9, 1.4, 0, 31)
    t.setheading(355)  # 画笔角度
    radian_right(0.9, 1.4, 0, 31)
    radian_left(2.8, 1, 0, 45)
    radian_left(0.4, 7.2, -0.2, 27)
    t.setheading(10)  # 画笔角度
    radian_left(0.8, 1.5, 0, 25)
    radian_left(1.6, 1.3, 0, 50)
    t.end_fill()
def InitEyes():
    # 左眼睛
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(-46, 10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(90)  # 画笔角度
    t.circle(5, 360)
    t.end_fill()
    # 右眼睛
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(46, 10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(-90)  # 画笔角度
    t.circle(5, 360)
    t.end_fill()
# 画脸
def InitFace():
    # 脸蛋
    t.begin_fill()
    t.fillcolor("red")
    t.pu()  # 提笔
    t.goto(-63, -10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(90)  # 画笔角度
    t.circle(10, 360)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("red")
    t.pu()  # 提笔
    t.goto(63, -10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(-90)  # 画笔角度
    t.circle(10, 360)
    t.end_fill()
    # 嘴巴
    t.pensize(2.2)
    t.pu()  # 提笔
    t.goto(0, 0)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(235)  # 画笔角度
    radian_right(5, 0.8, 0, 30)
    t.pu()  # 提笔
    t.goto(0, 0)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(305)  # 画笔角度
    radian_left(5, 0.8, 0, 30)
# 画手
def InitHands():
    # 左手
    t.pensize(2)
    t.pu()  # 提笔
    t.goto(-46, -100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(285)  # 画笔角度
    radian_right(0.4, 1.2, 0, 26)
    radian_right(5, 0.35, 0, 26)
    radian_right(0.3, 1.2, 0, 15)
    # 右手
    t.pu()  # 提笔
    t.goto(46, -100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(255)  # 画笔角度
    radian_left(0.4, 1.2, 0, 26)
    radian_left(5, 0.35, 0, 26)
    radian_left(0.3, 1.2, 0, 15)
def CloseEyes():
    # 左眼睛
    t.pu()  # 提笔
    t.goto(-46, 12)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(180)  # 画笔角度
    t.forward(10)
    # 右眼睛
    t.pu()  # 提笔
    t.goto(46, 12)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(0)  # 画笔角度
    t.forward(10)
# 初始化
def Init():
    InitEars()
    InitTail()
    InitFoots()
    InitBody()
    InitFace()
    InitHands()
    InitEyes()
# 眨眼睛
def Upgarde():
    InitEars()
    InitTail()
    InitFoots()
    InitBody()
    InitFace()
    InitHands()
    CloseEyes()
def Upgarde_Init():
    InitEars()
    InitTail()
    InitFoots()
    InitBody()
    InitFace()
    InitHands()
    InitEyes()
def main():
    Init()
    t.tracer(False)
    # 眨眼睛动画
    for i in range(30):
        if i % 2 == 0:
            t.reset()
            t.hideturtle()
            Upgarde()
            t.update()
            time.sleep(0.3)
        else:
            t.reset()
            t.hideturtle()
            Upgarde_Init()
            t.update()
            time.sleep(1)
main()
# 结束画笔
t.done()

 


 

其他

turtle库基本颜色

因为经常想换颜色整活所以干脆把它加到了博客里方便自己查阅

设置颜色几种输入格式(pencolor/color/fillcolor)

pencolor()

返回以颜色描述字符串或元组 (见示例) 表示的当前画笔颜色。可用作其他 color/pencolor/fillcolor 调用的输入。

pencolor(colorstring)

设置画笔颜色为 colorstring 指定的 Tk 颜色描述字符串,例如 "red""yellow" 或 "#33cc8c"

pencolor((r, g, b))

设置画笔颜色为以 rgb 元组表示的 RGB 颜色。rgb 的取值范围应为 0..colormode,colormode 的值为 1.0 或 255 (参见 colormode())。

pencolor(r, g, b)

设置画笔颜色为以 rgb 表示的 RGB 颜色。rgb 的取值范围应为 0..colormode。

我用到的一共有如下几种

 

你甚至可以拿它画二次元plmm(感慨)

结束后画布不自动关闭

在程序的最后面加上:turtle.exitonclick()
这种方法当用户点击界面之后就会退出

在程序的最后面加上:turtle.mainloop()
这种方法会使得后面的代码无法运行

在程序的最后面加上:turtle.done()
和第二种方法一样

在程序最后加上:input()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值