小猪佩奇:
import turtle as t
# 准备设置
t.screensize(400, 300) # 设置画布大小
t.pensize(4) # 设置画笔的大小
t.colormode(255) # 设置GBK颜色范围为0-255
t.color((255, 155, 192), "pink") # 设置画笔颜色和填充颜色(pink)
t.setup(840, 500) # 设置主窗口的大小为840*500
t.speed(50) # 设置画笔速度为10
# 画鼻子
t.penup() # 提笔
t.goto(-100, 100) # 画笔前往坐标(-100,100)
t.pendown() # 下笔
t.seth(-30) # 笔的角度为-30°(鼻子向右倾斜30度),但未行进
t.begin_fill() # 外形填充的开始标志
a = 0.4 # 设置初始步长
for i in range(120): # 循环一个从0开始到119的列表
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) # 向左转3度
t.fd(a) # 向前走a的步长
t.end_fill() # 依据轮廓填充颜色
t.penup() # 提笔
t.seth(90) # 笔的角度为90度
t.fd(25) # 向前移动25
t.seth(0) # 转换画笔的角度为0
t.fd(10) # 向前移动10
t.pendown() # 下笔
t.pencolor(255, 155, 192) # 设置画笔颜色
t.seth(10) # 转换画笔的角度为10
t.begin_fill() # 外形填充开始标志
t.circle(5) # 画一个半径为5的圆
t.color(160, 82, 45) # 设置填充颜色
t.end_fill() # 依据轮廓填充颜色
t.penup() # 提笔
t.seth(0) # 转换画笔的角度为0
t.fd(20) # 向前移动25
t.pendown() # 下笔
t.pencolor(255, 155, 192) # 设置画笔和填充颜色
t.seth(10) # 转换画笔的角度为10
t.begin_fill() # 外形填充开始标志
t.circle(5) # 画一个半径为5的圆
t.color(160, 82, 45) # 设置填充颜色
t.end_fill() # 依据轮廓填充颜色
# 以此类推,画头
t.color((255, 155, 192), "pink")
t.penup()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pendown()
t.begin_fill()
t.seth(180)
t.circle(300, -30) # 顺时针画一个半径为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.penup()
t.goto(-100, 100)
t.pendown()
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.penup()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pendown()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()
t.penup()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pendown()
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.penup()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pendown()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.penup()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pendown()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255, 155, 192), "white")
t.penup()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pendown()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.penup()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pendown()
t.begin_fill()
t.circle(3)
t.end_fill()
# 画腮
t.color((255, 155, 192))
t.penup()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pendown()
t.begin_fill()
t.circle(30)
t.end_fill()
# 画嘴
t.color(239, 69, 19)
t.penup()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pendown()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)
# 画身体
t.color("red", (255, 99, 71))
t.penup()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pendown()
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.penup()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pendown()
t.seth(-160)
t.circle(300, 15)
t.penup()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pendown()
t.seth(-10)
t.circle(-20, 90)
t.penup()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pendown()
t.seth(-20)
t.circle(-300, 15)
t.penup()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pendown()
t.seth(-170)
t.circle(20, 90)
# 画脚
t.pensize(10)
t.color((240, 128, 128))
t.penup()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pendown()
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.penup()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pendown()
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.penup()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pendown()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.done() # 画布窗口停留
谢尔宾斯基三角形:
import turtle as t
# 准备设置
t.screensize(400, 300) # 设置画布大小
t.setup(840, 500) # 设置主窗口的大小为840*500
t.speed(100) # 设置画笔速度为10
def draw_triangle(size): # 传入列表中三个点的坐标,绘制三角形
t.penup()
t.goto(size[0])
t.pendown()
t.goto(size[1])
t.goto(size[2])
t.goto(size[0])
def get_mid(a, b): # 计算返回任意2个点的中间点坐标
x = (a[0] + b[0]) / 2
y = (a[1] + b[1]) / 2
return [x, y]
def draw_s(size, times): # 递归times次,调用绘制函数draw_triangle(size)
draw_triangle(size) # 绘制三角形
if times > 1:
# 绘制左边小三角形
size2 = [size[0], get_mid(size[0], size[1]), get_mid(size[0], size[2])]
draw_s(size2, times - 1)
# 绘制上边的小三角形
size3 = [get_mid(size[0], size[2]), get_mid(size[1], size[2]), size[2]]
draw_s(size3, times - 1)
# 绘制右边的小三角形
size4 = [get_mid(size[0], size[1]), size[1], get_mid(size[1], size[2])]
draw_s(size4, times - 1)
t.left(90) # 向左转90度
points = [[-200, -150], [200, -150], [0, 150]] # 初始三角形坐标
count = 5 # 递归5次
draw_s(points, count) # 调用绘制谢尔宾斯基三角形函数
t.done() # 保持窗口停留
樱花树:
from turtle import *
from random import *
from math import *
class Tree:
def __init__(self):
setup(1000, 700)
bgcolor(1, 1, 1) # 背景色
# ht() # 隐藏turtle
speed(0) # 速度 1-10渐进,0 最快
tracer(1, 1) # 设置绘图屏幕刷新频率,参数1设置在正常刷新频次的第参数1次刷新,参数2设置每次刷新的时延
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()