1. 三角形
import turtle as t # 导入模块
t.speed(1)
# 红色三角形
t.fillcolor('red') # 填充颜色 红色
t.begin_fill() # 开始填充
t.fd(180) # 向当前方向前进距离180像素, forward - fd
t.left(90) # 左转90度 left - lt
t.forward(180) # 前走180像素
t.goto(0,0) # 起点坐标
t.end_fill() # 结束填充
# 黄色三角形
t.fillcolor('yellow') # 填充颜色 红色
t.begin_fill() # 开始填充
t.forward(180) # 前走180像素
t.right(90) # 左转90度 right - rt
t.forward(180) # 前走180像素
t.goto(0,0) # 起点坐标
t.end_fill() # 结束填充
t.hideturtle() # 隐藏画笔 hideturtle - ht
2. 扇形
import turtle as t
t.speed(0)
list = [0,90,180,270]
colors = ['red','yellow','blue','green']
for i in range(4):
t.seth(list[i]) # 0,90,180,270
# t.seth(i * 90) # 0,90,180,270
t.fillcolor(colors[i]) # 填充颜色
t.begin_fill() # 开始填充
t.forward(100)
t.right(90)
t.circle(-100,45) # 100代表半径(-100代表顺时针),45代表角度
t.right(90)
t.forward(100)
t.end_fill() # 结束填充
3. 正方形内切填充圆
import turtle as t
# ① 准备
t.hideturtle()
t.penup()
t.goto(0,-50)
t.pendown()
# ② 画圆
t.color('red','yellow')
t.begin_fill()
t.circle(50)
t.end_fill()
# ③ 画正方形
t.back(50)
for i in range(4):
t.forward(100)
t.left(90)
t.done()
4. 绘制六边形
绘制图形要求:
- 背景为白色,正六边形和三角形的填充颜色分别为红色、绿色;
- 图形中间为边长为150的正六边形,周围为六个等边三角形;
- 正六边形的上下两条边要求与X轴方向平行;
- 绘图过程中隐藏画笔,并能清楚地看到图形绘制过程。
import turtle as t
t.fillcolor('red')
t.hideturtle()
t.begin_fill()
for i in range(6):
t.forward(150)
# 绘制正三角形
t.fillcolor('green')
t.begin_fill()
for j in range(3):
t.right(120)
t.forward(150)
t.left(60)
t.end_fill()
t.end_fill()
t.fillcolor('red')
t.begin_fill()
for i in range(6):
t.forward(150)
t.left(60)
t.end_fill()
5. 奥运五环
import turtle as t
t.speed(0)
# 黑色环
t.pensize(6) # 画笔的粗细
t.color('black') # 画笔颜色
t.circle(80)
# 蓝色环
t.penup() # 抬笔
t.goto(-180,0) # 移到指定的坐标
t.pendown() # 落笔
t.color('blue')
t.circle(80)
#红色环
t.penup() # 抬笔
t.goto(180,0) # 移到指定的坐标
t.pendown() # 落笔
t.color('red')
t.circle(80)
#绿色环
t.penup() # 抬笔
t.goto(92,-95) # 移到指定的坐标
t.pendown() # 落笔
t.color('green')
t.circle(80)
#黄色环
t.penup() # 抬笔
t.goto(-92,-95) # 移到指定的坐标
t.pendown() # 落笔
t.color('yellow')
t.circle(80)
# 奥运五环四字
t.color('black')
t.penup() # 抬笔
t.goto(-200,200) # 移到指定的坐标
t.pendown() # 落笔
t.write('奥运五环',font=('楷体',50))
6. 五角星
import random
import turtle as t
t.speed(0)
t.hideturtle() # 隐藏
def a(x,y):
z = random.randint(50,100)
t.penup()
t.goto(x,y)
t.pendown()
t.color('red')
t.begin_fill() # 填充
for i in range(5):
t.forward(z) # forward() 画直线
t.right(144)
t.end_fill() # 结束填充
count = random.randint(1,10)
for i in range(count):
x = random.randint(-300,300)
y = random.randint(-300,300)
a(x,y)
2. 第二种
import turtle as t
t.speed(0)
t.hideturtle() # 隐藏
t.pensize(6)
t.color('yellow','red')
t.begin_fill()
for i in range(5):
t.forward(150)
t.right(144)
t.end_fill()
7. 斐波那契螺旋线
前两项都是1,从第3项开始,每一项都等于前两项之和。
a = [1,1] # [1, 1, 2, 3, 5, 8, 13, 21, 34]
for i in range(2,9):
a.append(a[-2] + a[-1]) # 前面第一项 和 前面第二项的和
for i in a:
t.circle(i*5,90) # t.circle(顺时针(负为逆时针),角度)
def fun(n):
if n <= 2:
return 1
else:
return fun(n - 1)+ fun(n - 2)
for i in range(1,9):
t.circle(fun(i)* 6,90) # t.circle(顺时针(负为逆时针),角度)
# ① 利用递归函数求出所需项
def fun(n):
if n <= 2:
return 1
else:
return fun(n - 1)+ fun(n - 2)
a = []
for i in range(1,9):
a.append(fun(i))
# ② 绘制正方形和圆弧
for x in a:
t.color('red')
for i in range(4):
t.forward(x * 6)
t.right(90)
# t.left(90) # t.circle(x * 6,90) circle第一个参数为正数时需要左转
t.color('black')
t.circle(-x * 6,90)