练习1:二叉树
效果如下:
import turtle as t
t.setup(700,700)
t.pu()
t.goto(0,0)
t.pd()
t.left(90)
t.pensize(2)
t.speed(10)
def drawTree(length,angle):#长度和角度
if length>=42:
#先画左子树
t.left(angle)
t.fd(length)
#递归调用(假设dw函数可用,调用自身完成左树)
drawTree(length-6,angle)
#返回节点,上一次绘制的距离的位置
t.backward(length)
#调整角度绘制右子树
t.right(angle*2)
t.fd(length)
#递归调用(假设dw函数可用,调用自身完成右树)
drawTree(length-6,angle)
#返回节点数
t.backward(length)
t.left(angle)
drawTree(60,25)
t.mainloop()
练习二:
完成如下功能
利用random库和turtle库,在屏幕 上绘制4个小雪花,雪花的中心点坐标 由列表points给出,雪花的半径长度由 randint ()函数产生。
points= [[0, 0], [50, 40], [70, 80],[- 40, 30]]
雪花的颜色是红色,单一雪花效果如下图所示
from turtle import *
from random import *
points = [[0, 0],[50, 40],[70, 80],[-40, 30]]
pencolor("red")
screensize(1200,600)
speed(1)
pensize(5)
def xuehua1():
penup()
goto(points[0][0],points[0][1])
pendown()
a = randint(1,200)#半径长度
b = a // 4
for i in range(1,7,1):
fd(a)
backward(b)
c = pos()
left(30)
fd(b)
penup()
goto(c[0],c[1])
pendown()
right(60)
fd(b)
penup()
goto(points[0][0],points[0][1])
pendown()
right(30)
def xuehua2():
penup()
goto(points[1][0],points[1][1])
pendown()
a = randint(1,200)#半径长度
b = a // 4
for i in range(1,7,1):