绘图之前先要安装turtle模块
python 2:
pip install turtle
python 3:
pip3 install turtle
1.小猪佩奇:
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.08t.lt(3) # 向左转3度t.fd(a) # 向前走a的步长else:a = a - 0.08t.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.08t.lt(3) # 向左转3度t.fd(a) # 向前走a的步长else:a = a - 0.08t.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()
2.叮当猫
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.08t.lt(3) # 向左转3度t.fd(a) # 向前走a的步长else:a = a - 0.08t.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.08t.lt(3) # 向左转3度t.fd(a) # 向前走a的步长else:a = a - 0.08t.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()
3.小黄人
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()
4.樱花树
from turtle import *
from random import *
from math import *
class Tree:def __init__(self):setup(1000, 700)bgcolor(1, 1, 1) # 背景色# ht() # 隐藏turtlespeed(10) # 速度 1-10渐进,0 最快# tracer(1, 100) # 设置绘图屏幕刷新频率,参数1设置在正常刷新频次的第参数1次刷新,参数2设置每次刷新的时延tracer(0, 0)pu() # 抬笔backward(100)# 保证笔触箭头方向始终不向下,此处使其左转90度,而不是右转left(90) # 左转90度backward(300) # 后退300def tree(self, n, l):pd() # 下笔# 阴影效果t = cos(radians(heading() + 45)) / 8 + 0.25pencolor(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.5pencolor(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() * 40setheading(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.5pencolor(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)# passpu()backward(l) # 退回
def main():tree = Tree()tree.tree(12, 100) # 递归7层done()
if __name__ == '__main__':main()
#!/usr/bin/env python
# coding=utf-8import turtle
import random
from turtle import *
from time import sleep
# 画樱花的躯干(60,t)
def tree(branchLen, t):sleep(0.0005)if branchLen > 3:if 8 <= branchLen <= 12:if random.randint(0, 2) == 0:t.color('snow') # 白else:t.color('lightcoral') # 淡珊瑚色t.pensize(branchLen / 3)elif branchLen < 8:if random.randint(0, 1) == 0:t.color('snow')else:t.color('lightcoral') # 淡珊瑚色t.pensize(branchLen / 2)else:t.color('sienna') # 赭(zhě)色t.pensize(branchLen / 10) # 6t.forward(branchLen)a = 1.5 * random.random()t.right(20 * a)b = 1.5 * random.random()tree(branchLen - 10 * b, t)t.left(40 * a)tree(branchLen - 10 * b, t)t.right(20 * a)t.up()t.backward(branchLen)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)
def main():# 绘图区域t = turtle.Turtle()# 画布大小w = turtle.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()
main()
from turtle import *
from random import *
from math import *
def tree(n, l):pd() # 下笔# 阴影效果t = cos(radians(heading() + 45)) / 8 + 0.25pencolor(t, t, t)pensize(n / 4)forward(l) # 画树枝if n > 0:b = random() * 15 + 10 # 右分支偏转角度c = random() * 15 + 10 # 左分支偏转角度d = l * (random() * 0.35 + 0.6) # 下一个分支的长度# 右转一定角度,画右分支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.5pencolor(n, n, n)circle(2)left(90)pu()backward(l) # 退回
bgcolor(0.5, 0.5, 0.5) # 背景色
ht() # 隐藏turtle
speed(0) # 速度,1-10渐进,0最快
tracer(0, 0)
left(90) # 左转90度
pu() # 抬笔
backward(300) # 后退300
tree(13, 100) # 递归7层
done()
from random import *
from math import *
def tree(n, l):setup(1000, 800)pd() # 下笔# 阴影效果t = cos(radians(heading() + 45)) / 8 + 0.25pencolor(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.5pencolor(n, n * 0.8, n * 0.8)circle(3)left(90)# 添加0.3倍的飘落叶子if (random() > 0.7):pu()# 飘落t = heading()an = -40 + random() * 40setheading(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.5pencolor(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(0) # 速度,1-10渐进,0最快
tracer(0, 0)
pu() # 抬笔
backward(100)
left(90) # 左转90度
pu() # 抬笔
backward(300) # 后退300
tree(12, 100) # 递归7层
done()
5.皮卡丘
# 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增大stept.lt(ang) # 向左转ang度t.fd(dis) # 向前走dis的步长
def radian_right(ang, dis, step, n):for i in range(n):dis += stept.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()
6.钟表
# -*- coding:utf-8 –*-
# 用turtlr画时钟
# 以自定义shape的方式实现
import turtle as t
import datetime as d
def skip(step): # 抬笔,跳到一个地方t.penup()t.forward(step)t.pendown()
def drawClock(radius): # 画表盘t.speed(0)t.mode("logo") # 以Logo坐标、角度方式t.hideturtle()t.pensize(7)t.home() # 回到圆点for j in range(60):skip(radius)if (j % 5 == 0):t.forward(20)skip(-radius - 20)else:t.dot(5)skip(-radius)t.right(6)
def makePoint(pointName, len): # 钟的指针,时针、分针、秒针t.penup()t.home()t.begin_poly()t.back(0.1 * len)t.forward(len * 1.1)t.end_poly()poly = t.get_poly()t.register_shape(pointName, poly) # 注册为一个shape
def drawPoint(): # 画指针global hourPoint, minPoint, secPoint, fontWritermakePoint("hourPoint", 100)makePoint("minPoint", 120)makePoint("secPoint", 140)hourPoint = t.Pen() # 每个指针是一只新turtlehourPoint.shape("hourPoint")hourPoint.shapesize(1, 1, 6)minPoint = t.Pen()minPoint.shape("minPoint")minPoint.shapesize(1, 1, 4)secPoint = t.Pen()secPoint.shape("secPoint")secPoint.pencolor('red')fontWriter = t.Pen()fontWriter.pencolor('gray')fontWriter.hideturtle()
def getWeekName(weekday):weekName = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']return weekName[weekday]
def getDate(year, month, day):return "%s-%s-%s" % (year, month, day)
def realTime():curr = d.datetime.now()curr_year = curr.yearcurr_month = curr.monthcurr_day = curr.daycurr_hour = curr.hourcurr_minute = curr.minutecurr_second = curr.secondcurr_weekday = curr.weekday()t.tracer(False)secPoint.setheading(360 / 60 * curr_second)minPoint.setheading(360 / 60 * curr_minute)hourPoint.setheading(360 / 12 * curr_hour + 30 / 60 * curr_minute)fontWriter.clear()fontWriter.home()fontWriter.penup()fontWriter.forward(80)# 用turtle写文字fontWriter.write(getWeekName(curr_weekday), align="center", font=("Courier", 14, "bold"))fontWriter.forward(-160)fontWriter.write(getDate(curr_year, curr_month, curr_day), align="center", font=("Courier", 14, "bold"))t.tracer(True)print(curr_second)t.ontimer(realTime, 100) # 每隔100毫秒调用一次realTime()
def main():t.tracer(False)drawClock(160)drawPoint()realTime()t.tracer(True)t.mainloop()
if __name__ == '__main__':main()
7.汉诺塔
import turtle
class Stack:def __init__(self):self.items = []def isEmpty(self):return len(self.items) == 0def push(self, item):self.items.append(item)def pop(self):return self.items.pop()def peek(self):if not self.isEmpty():return self.items[len(self.items) - 1]def size(self):return len(self.items)
def drawpole_3(): # 画出汉诺塔的polest = turtle.Turtle()t.hideturtle()def drawpole_1(k):t.up()t.pensize(10)t.speed(100)t.goto(400 * (k - 1), 100)t.down()t.goto(400 * (k - 1), -100)t.goto(400 * (k - 1) - 20, -100)t.goto(400 * (k - 1) + 20, -100)drawpole_1(0) # 画出汉诺塔的poles[0]drawpole_1(1) # 画出汉诺塔的poles[1]drawpole_1(2) # 画出汉诺塔的poles[2]
def creat_plates(n): # 制造n个盘子plates = [turtle.Turtle() for i in range(n)]for i in range(n):plates[i].up()plates[i].hideturtle()plates[i].shape("square")plates[i].shapesize(1, 8 - i)plates[i].goto(-400, -90 + 20 * i)plates[i].showturtle()return plates
def pole_stack(): # 制造poles的栈poles = [Stack() for i in range(3)]return poles
def moveDisk(plates, poles, fp, tp): # 把poles[fp]顶端的盘子plates[mov]从poles[fp]移到poles[tp]mov = poles[fp].peek()plates[mov].goto((fp - 1) * 400, 150)plates[mov].goto((tp - 1) * 400, 150)l = poles[tp].size() # 确定移动到底部的高度(恰好放在原来最上面的盘子上面)plates[mov].goto((tp - 1) * 400, -90 + 20 * l)
def moveTower(plates, poles, height, fromPole, toPole, withPole): # 递归放盘子if height >= 1:moveTower(plates, poles, height - 1, fromPole, withPole, toPole)moveDisk(plates, poles, fromPole, toPole)poles[toPole].push(poles[fromPole].pop())moveTower(plates, poles, height - 1, withPole, toPole, fromPole)
myscreen = turtle.Screen()
drawpole_3()
n = int(input("请输入汉诺塔的层数并回车:\n"))
plates = creat_plates(n)
poles = pole_stack()
for i in range(n):poles[0].push(i)
moveTower(plates, poles, n, 0, 2, 1)
myscreen.exitonclick()
8.高达
import turtle
t=turtle.Turtle()
turtle.Turtle().screen.delay(0)
tleft=turtle.Turtle()
#第一部分
t.penup()
t.goto(0,0)
t.pendown()
t.left(20)
t.forward(110)
t.left(25)
t.forward(40)
t.left(100)
t.circle(180,20)
t.right(120)
t.forward(250)
t.left(165)
t.forward(250)
t.right(100)
t.forward(35)
t.left(70)
t.forward(45)
t.left(70)
t.forward(120)
t.left(70)
t.forward(80)
t.left(80)
t.forward(80)
t.left(68)
t.forward(120)
t.left(180)
t.forward(78)
t.right(68)
t.forward(60)
t.right(75)
t.forward(60)
t.right(110)
t.forward(15)
t.left(38)
t.forward(65)
t.right(73)#五边形的直边
t.forward(35)
t.right(70)
t.forward(65)
t.right(68)
t.forward(50)
t.right(80)
t.forward(50)
t.penup()
t.goto(-65,68)
t.pendown()
t.right(7)
t.forward(350)
t.right(165)
t.forward(330)
t.penup()
t.goto(64,65)
t.pendown()
t.left(75)
t.forward(350)
t.left(165)
t.forward(330)
t.penup()
t.goto(300,500)
#第二部分
tleft.left(180)
tleft.right(20)
tleft.forward(110)
tleft.right(25)
tleft.forward(40)
tleft.right(100)
tleft.circle(-180,20)
tleft.left(120)
tleft.forward(250)
tleft.right(165)
tleft.forward(250)
tleft.left(100)
tleft.forward(35)
tleft.penup()
tleft.goto(0,0)
tleft.pendown()
tleft.left(20)
tleft.penup()
tleft.forward(18)
tleft.pendown()
tleft.forward(50)#额头竖线
tleft.penup()
tleft.forward(110)#消除竖线
tleft.pendown()
tleft.left(90)
tleft.forward(30)
tleft.right(90)
tleft.forward(60)
tleft.right(90)
tleft.forward(60)
tleft.right(90)
tleft.forward(60)
tleft.right(90)
tleft.forward(40)
tleft.penup()
tleft.forward(30)
tleft.pendown()
tleft.left(90)
tleft.forward(30)
tleft.right(180)
tleft.forward(100)
tleft.right(90)
tleft.forward(80)
tleft.right(90)
tleft.forward(100)
tleft.penup()
tleft.goto(150,70)
tleft.pendown()
tleft.left(100)
tleft.forward(40)
tleft.right(80)
tleft.circle(-333,40)
tleft.right(160)
tleft.forward(230)
#右半部分
tleft.left(100)
tleft.forward(40)
tleft.left(80)
tleft.forward(20)
tleft.left(100)
tleft.forward(30)
tleft.right(100)
tleft.forward(20)
tleft.right(80)
tleft.forward(30)
tleft.left(80)
tleft.forward(20)
tleft.left(100)
tleft.forward(30)
tleft.right(100)
tleft.forward(20)
tleft.right(80)
tleft.forward(30)
tleft.left(80)
tleft.forward(20)
tleft.left(100)
tleft.forward(30)
tleft.right(100)
tleft.forward(20)
tleft.right(80)
tleft.forward(30)
tleft.left(80)
tleft.forward(20)
tleft.left(100)
tleft.forward(30)
tleft.right(100)
tleft.forward(20)
tleft.right(80)
tleft.forward(30)
tleft.left(80)
tleft.forward(20)
tleft.left(100)
tleft.forward(30)
tleft.right(100)
tleft.forward(20)
tleft.right(80)
tleft.forward(30)
tleft.left(80)
tleft.forward(20)
tleft.left(100)
tleft.forward(30)
tleft.right(100)
tleft.forward(20)
tleft.right(80)
tleft.forward(30)
#右下部分
tleft.left(70)
tleft.forward(30)
tleft.right(110)
tleft.forward(40)
tleft.right(60)
tleft.forward(100)
tleft.right(30)
tleft.circle(200,20)
tleft.left(10)
tleft.forward(80)
#右下部分goto
tleft.penup()
tleft.goto(145,-198)
tleft.pendown()
tleft.left(90)
tleft.forward(30)
tleft.right(30)
tleft.forward(40)
tleft.right(150)
tleft.forward(30)
tleft.backward(30)
tleft.left(90)
tleft.forward(100)
tleft.right(90)
tleft.forward(30)
tleft.backward(30)
tleft.left(90)
tleft.right(30)
tleft.circle(200,20)
tleft.left(10)
tleft.forward(50)
#第三部分脸
t2=turtle.Turtle()
t2.penup()
t2.goto(0,-80)
#尖角
t2.circle(150,extent=90)
t2.pendown()
t2.circle(150,extent=30)
t2.penup()
t2.circle(150,extent=18)
t2.pendown()
t2.circle(150,extent=27)
t2.penup()
t2.circle(150,extent=30)
t2.pendown()
t2.circle(150,extent=27)
t2.penup()
t2.circle(150,extent=18)
t2.pendown()
t2.circle(150,extent=30)
t2.right(100)
t2.forward(40)
#左脸夹
t2.left(80)
t2.circle(333,40)
t2.left(160)
t2.forward(230)
#左半部分
t2.right(100)
t2.forward(40)
t2.right(80)
t2.forward(20)
t2.right(100)
t2.forward(30)
t2.left(100)
t2.forward(20)
t2.left(80)
t2.forward(30)
t2.right(80)
t2.forward(20)
t2.right(100)
t2.forward(30)
t2.left(100)
t2.forward(20)
t2.left(80)
t2.forward(30)
t2.right(80)
t2.forward(20)
t2.right(100)
t2.forward(30)
t2.left(100)
t2.forward(20)
t2.left(80)
t2.forward(30)
t2.right(80)
t2.forward(20)
t2.right(100)
t2.forward(30)
t2.left(100)
t2.forward(20)
t2.left(80)
t2.forward(30)
t2.right(80)
t2.forward(20)
t2.right(100)
t2.forward(30)
t2.left(100)
t2.forward(20)
t2.left(80)
t2.forward(30)
t2.right(80)
t2.forward(20)
t2.right(100)
t2.forward(30)
t2.left(100)
t2.forward(20)
t2.left(80)
t2.forward(30)
t2.right(70)
t2.forward(30)
t2.left(110)
t2.forward(40)
t2.left(60)
t2.forward(100)
t2.left(30)
t2.circle(-200,20)
t2.right(10)
t2.forward(80)
t2.penup()
t2.goto(-145,-198)#左脸颊
t2.pendown()
t2.right(90)
t2.forward(30)
t2.left(30)
t2.forward(40)
t2.left(150)
t2.forward(30)
t2.right(180)
t2.forward(30)
t2.left(90)
t2.forward(100)
t2.left(90)
t2.forward(30)
t2.left(180)
t2.forward(30)
t2.left(120)
t2.circle(-200,20)
t2.right(10)
t2.forward(50)
#左眼
t2.right(135)
t2.forward(70)
t2.left(50)
t2.forward(40)
t2.left(20)
t2.forward(20)
t2.penup()
t2.goto(-100,28)
t2.pendown()
t2.right(70)
t2.forward(65)
t2.left(50)
t2.forward(40)
t2.left(40)
t2.forward(20)
#左眼带
t2.penup()
t2.goto(-105,-10)
t2.pendown()
t2.right(100)
t2.circle(120,extent=20)
t2.circle(60,extent=80)
t2.penup()
t2.goto(-105,-13)
t2.pendown()
t2.right(100)
t2.circle(120,extent=20)
t2.circle(60,extent=80)
t2.penup()
t2.goto(-70,-40)
t2.pendown()
t2.left(10)
t2.forward(30)
t2.penup()
t2.goto(-10,-40)
t2.pendown()
t2.left(35)
t2.forward(30)
t2.penup()
t2.goto(-80,30)
t2.pendown()
t2.right(130)
t2.forward(47)
t2.left(50)
t2.forward(35)
t2.penup()
t2.goto(-60,-45)
t2.pendown()
t2.right(98)
t2.forward(60)
t2.left(20)
t2.forward(80)
t2.left(70)
t2.forward(10)
t2.left(90)
t2.forward(50)
t2.right(60)
t2.forward(30)
t2.right(60)
t2.forward(30)
t2.right(60)
t2.forward(50)
t2.left(90)
t2.forward(10)
t2.left(75)
t2.forward(80)
t2.left(15)
t2.forward(60)
t2.penup()
t2.goto(-80,-140)
t2.pendown()
t2.right(150)
t2.circle(85,extent=45)
t2.left(15)
t2.forward(70)
t2.left(15)
t2.circle(55,extent=55)
t2.penup()
t2.goto(0,-175)
t2.pendown()
t2.left(18)
t2.forward(170)
#右眼
tleft.left(135)
tleft.forward(70)
tleft.right(50)
tleft.forward(40)
tleft.right(20)
tleft.forward(20)
tleft.penup()
tleft.goto(100,28)
tleft.pendown()
tleft.left(70)
tleft.forward(65)
tleft.right(50)
tleft.forward(40)
tleft.right(40)
tleft.forward(20)
#右眼带
tleft.penup()
tleft.goto(105,-10)
tleft.pendown()
tleft.left(100)
tleft.circle(-120,extent=20)
tleft.circle(-60,extent=80)
tleft.penup()
tleft.goto(105,-13)
tleft.pendown()
tleft.left(100)
tleft.circle(-120,extent=20)
tleft.circle(-60,extent=80)
#右眼睛
tleft.penup()
tleft.goto(70,-40)
tleft.pendown()
tleft.right(10)
tleft.forward(30)
tleft.penup()
tleft.goto(10,-40)
tleft.pendown()
tleft.right(35)
tleft.forward(30)
tleft.penup()
tleft.goto(80,30)
tleft.pendown()
tleft.left(130)
tleft.forward(47)
tleft.right(50)
tleft.forward(35)
#鼻子
tleft.penup()
tleft.goto(0,-70)
tleft.pendown()
tleft.left(30)
tleft.forward(20)
tleft.left(72)
tleft.forward(10)
tleft.left(108)
tleft.forward(20)
tleft.right(42)
tleft.forward(20)
tleft.left(108)
tleft.forward(10)
tleft.left(72)
tleft.forward(20)
tleft.penup()
tleft.goto(0,-90)
tleft.pendown()
tleft.left(42)
tleft.forward(20)
tleft.left(72)
tleft.forward(10)
tleft.left(108)
tleft.forward(20)
tleft.right(42)
tleft.forward(20)
tleft.left(108)
tleft.forward(10)
tleft.left(72)
tleft.forward(20)
tleft.penup()
tleft.goto(200,500)
turtle.done()
9.星空
from turtle import *
from random import random,randint
screen = Screen()
width ,height = 800,600
screen.setup(width,height)
screen.bgcolor("black")
screen.mode("logo")
screen.delay(0)#这里要设为0,否则很卡
t = Turtle(visible = False,shape='circle')
t.pencolor("white")
t.fillcolor("white")
t.penup()
t.setheading(-90)
t.goto(width/2,randint(-height/2,height/2))
stars = []
for i in range(200):star = t.clone()s =random() /3star.shapesize(s,s)star.speed(int(s*10))star.setx(width/2 + randint(1,width))star.sety( randint(-height/2,height/2))star.showturtle()stars.append(star)
while True:for star in stars:star.setx(star.xcor() - 3 * star.speed())if star.xcor()<-width/2:star.hideturtle()star.setx(width/2 + randint(1,width))star.sety( randint(-height/2,height/2))star.showturtle()
10.狮子头
import turtle as t
def hair(): # 画头发t.penup()t.goto(-50, 150)t.pendown()t.fillcolor('#a2774d')t.begin_fill()for j in range(10): # 重复执行10次t.setheading(60 - (j * 36)) # 每次调整初始角度t.circle(-50, 120) # 画120度的弧t.end_fill()
def face(): # 画脸t.penup()t.goto(0, 100)t.pendown()t.fillcolor('#f2ae20')t.begin_fill()t.setheading(180)t.circle(85)t.end_fill()# 下巴t.circle(85, 120)t.fillcolor('white')t.begin_fill()t.circle(85, 120)t.setheading(135)t.circle(100, 95)t.end_fill()
def ears(dir): # 画眼睛,dir用来设置方向,左右眼对称t.penup()t.goto((0 - dir) * 30, 90)t.setheading(90)t.pendown()t.fillcolor('#f2ae20')t.begin_fill()t.circle(dir * 30)t.end_fill()t.penup()t.goto((0 - dir) * 40, 85)t.setheading(90)t.pendown()t.fillcolor('white')t.begin_fill()t.circle(dir * 17)t.end_fill()
def nose(): # 画鼻子t.penup()t.goto(20, 0)t.setheading(90)t.pendown()t.fillcolor('#a2774d')t.begin_fill()t.circle(20)t.end_fill()
def eye(dir): # 画耳朵,dir用来设置方向,左右耳对称t.penup()t.goto((0 - dir) * 30, 20)t.setheading(0)t.pendown()t.fillcolor('black')t.begin_fill()t.circle(10)t.end_fill()
def mouth(): # 画嘴巴t.penup()t.goto(0, 0)t.setheading(-90)t.pendown()t.forward(50)t.setheading(0)t.circle(80, 30)t.penup()t.goto(0, -50)t.setheading(180)t.pendown()t.circle(-80, 30)
hair()
ears(1)
ears(-1)
face()
eye(1)
eye(-1)
mouth()
nose()
t.done()
11.捂脸
import turtle
# 画指定的任意圆弧
def arc(sa, ea, x, y, r): # start angle,end angle,circle center,radiusturtle.penup()turtle.goto(x, y)turtle.setheading(0)turtle.left(sa)turtle.fd(r)turtle.pendown()turtle.left(90)turtle.circle(r, (ea - sa))return turtle.position()
turtle.hideturtle()
# 画脸
turtle.speed(5)
turtle.setup(900, 600, 200, 200)
turtle.pensize(5)
turtle.right(90)
turtle.penup()
turtle.fd(100)
turtle.left(90)
turtle.pendown()
turtle.begin_fill()
turtle.pencolor("#B26A0F") # head side color
turtle.circle(150)
turtle.fillcolor("#F9E549") # face color
turtle.end_fill()
# 画嘴
turtle.penup()
turtle.goto(77, 20)
turtle.pencolor("#744702")
turtle.goto(0, 50)
turtle.right(30)
turtle.fd(110)
turtle.right(90)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor("#925902") # mouth color
turtle.circle(-97, 160)
turtle.goto(92, -3)
turtle.end_fill()
turtle.penup()
turtle.goto(77, -25)
# 画牙齿
turtle.pencolor("white")
turtle.begin_fill()
turtle.fillcolor("white")
turtle.goto(77, -24)
turtle.goto(-81, 29)
turtle.goto(-70, 43)
turtle.goto(77, -8)
turtle.end_fill()
turtle.penup()
turtle.goto(0, -100)
turtle.setheading(0)
turtle.pendown()
# 画左边眼泪
turtle.left(90)
turtle.penup()
turtle.fd(150)
turtle.right(60)
turtle.fd(-150)
turtle.pendown()
turtle.left(20)
turtle.pencolor("#155F84") # tear side color
turtle.fd(150)
turtle.right(180)
position1 = turtle.position()
turtle.begin_fill()
turtle.fillcolor("#7EB0C8") # tear color
turtle.fd(150)
turtle.right(20)
turtle.left(270)
turtle.circle(-150, 18)
turtle.right(52)
turtle.fd(110)
position2 = turtle.position()
turtle.goto(-33, 90)
turtle.end_fill()
# 画右边眼泪
turtle.penup()
turtle.goto(0, 0)
turtle.setheading(0)
turtle.left(90)
turtle.fd(50)
turtle.right(150)
turtle.fd(150)
turtle.left(150)
turtle.fd(100)
turtle.pendown()
turtle.begin_fill()
turtle.fd(-100)
turtle.fillcolor("#7EB0C8") # tear color
turtle.right(60)
turtle.circle(150, 15)
turtle.left(45)
turtle.fd(66)
turtle.goto(77, 20)
turtle.end_fill()
# 画眼睛
turtle.penup()
turtle.pencolor("#6C4E00") # eye color
turtle.goto(-65, 75)
turtle.setheading(0)
turtle.left(27)
turtle.fd(38)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor("#6C4E00") # eye color
turtle.left(90)
turtle.circle(38, 86)
turtle.goto(position2[0], position2[1])
turtle.goto(position1[0], position1[1])
turtle.end_fill()
# 画手
turtle.pencolor("#D57E18") # hand side color
turtle.begin_fill()
turtle.fillcolor("#EFBD3D") # hand color
# 第一个手指
arc(-110, 10, 110, -40, 30)
turtle.circle(300, 35)
turtle.circle(13, 120)
turtle.setheading(-50)
turtle.fd(20)
turtle.setheading(130)
# 第二个手指
turtle.circle(200, 15)
turtle.circle(12, 180)
turtle.fd(40)
turtle.setheading(137)
# 第三个手指
turtle.circle(200, 16)
turtle.circle(12, 160)
turtle.setheading(-35)
turtle.fd(45)
turtle.setheading(140)
# 第四个手指
turtle.circle(200, 13)
turtle.circle(11, 160)
turtle.setheading(-35)
turtle.fd(40)
turtle.setheading(145)
# 第五个手指
turtle.circle(200, 9)
turtle.circle(10, 180)
turtle.setheading(-31)
turtle.fd(50)
# 画最后手腕的部分
turtle.setheading(-45)
turtle.pensize(7)
turtle.right(5)
turtle.circle(180, 35)
turtle.end_fill()
turtle.begin_fill()
turtle.setheading(-77)
turtle.pensize(5)
turtle.fd(50)
turtle.left(-270)
turtle.fd(7)
turtle.pencolor("#EFBD3D")
turtle.circle(30, 180)
turtle.end_fill()
# 测试
# res=arc(70,220,90,50,300)
# print(res[0],res[1])
turtle.done()
12.绘制多边形(自定义边的数目(2-6))
#SquareSpiral1.py
import turtle as t
# t = turtle.Pen()
t.bgcolor("black")
sides=eval(input("输入要绘制的边的数目,请输入2-6的数字!"))
colors=["red","yellow","green","blue","orange","purple"]
for x in range(150):t.pencolor(colors[x%sides])t.forward(x*3/sides+x)t.left(360/sides+1)t.width(x*sides/200)
t.exitonclick()
print("####结束####")
13.绘制数码管日期
import turtle as t
import datetime
def drawGap(): #绘制数码管间隔t.penup()t.fd(5)
def drawLine(draw): #绘制单段数码管drawGap()t.pendown() if draw else t.penup()t.fd(40)drawGap()t.right(90)
def drawDigit(d): #根据数字绘制七段数码管drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)drawLine(True) if d in [0,2,6,8] else drawLine(False)t.left(90)drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)t.left(180)t.penup()t.fd(20)
def drawDate(date):t.pencolor("red")for i in date:if i == '-':t.write('年', font=("Arial", 18, "normal"))t.pencolor("green")t.fd(40)elif i == '=':t.write('月', font=("Arial", 18, "normal"))t.pencolor("blue")t.fd(40)elif i == '+': t.write('日', font=("Arial", 18, "normal"))else:drawDigit(eval(i))
def main():t.setup(800, 350, 200, 200)t.penup()t.fd(-350)t.pensize(5)drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))t.hideturtle()t.exitonclick()
main()
14.小狗
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()
15.小人发射爱心
import turtle as t
from time import sleep
def go_to(x, y):t.up()t.goto(x, y)t.down()
def head(x, y, r):go_to(x, y)t.speed(20)t.circle(r)leg(x, y)
def leg(x, y):t.right(90)t.forward(180)t.right(30)t.forward(100)t.left(120)go_to(x, y - 180)t.forward(100)t.right(120)t.forward(100)t.left(120)hand(x, y)
def hand(x, y):go_to(x, y - 60)t.forward(100)t.left(60)t.forward(100)go_to(x, y - 90)t.right(60)t.forward(100)t.right(60)t.forward(100)t.left(60)eye(x, y)
def eye(x, y):go_to(x - 50, y + 130)t.right(90)t.forward(50)go_to(x + 40, y + 130)t.forward(50)t.left(90)
def big_Circle(size):t.speed(20)for i in range(150):t.forward(size)t.right(0.3)
def line(size):t.speed(20)t.forward(51 * size)
def small_Circle(size):t.speed(20)for i in range(210):t.forward(size)t.right(0.786)
def heart(x, y, size):go_to(x, y)t.left(150)t.begin_fill()line(size)big_Circle(size)small_Circle(size)t.left(120)small_Circle(size)big_Circle(size)line(size)t.end_fill()
def main():t.pensize(2)t.color('red', 'pink')head(-120, 100, 100)heart(250, -80, 1)go_to(100, -300)t.write("To: 智慧与美貌并存的", move=True, align="left", font=("楷体", 20, "normal"))t.done()
main()