阅读量:3
绘制樱花的代码:
import turtle # 设置画布大小 turtle.setup(800, 600) # 画樱花的枝干 def draw_branch(branch_len): if branch_len > 3: # 设置画笔粗细和颜色 turtle.pensize(branch_len / 3) turtle.pencolor('brown') # 画右边的枝干 turtle.forward(branch_len) turtle.right(20) draw_branch(branch_len - 15) # 画左边的枝干 turtle.left(40) draw_branch(branch_len - 15) # 返回树干处 turtle.right(20) turtle.backward(branch_len) # 画樱花的花瓣 def draw_petal(): turtle.pensize(2) turtle.pencolor('pink') turtle.fillcolor('pink') # 开始填充 turtle.begin_fill() turtle.left(60) turtle.forward(30) turtle.right(60) turtle.circle(60, 120) turtle.right(60) turtle.forward(30) turtle.end_fill() # 移动画笔到指定位置 def move_pen(x, y): turtle.penup() turtle.goto(x, y) turtle.pendown() # 主函数 def main(): # 隐藏画笔 turtle.hideturtle() # 设置速度和角度 turtle.speed(5) turtle.left(90) # 画樱花的枝干 move_pen(0, -200) draw_branch(100) # 画樱花的花瓣 move_pen(0, 0) for _ in range(6): draw_petal() turtle.left(60) # 关闭画布 turtle.done() if __name__ == '__main__': main()
绘制玫瑰的代码:
import turtle # 设置画布大小 turtle.setup(800, 600) # 画玫瑰 def draw_rose(): turtle.speed(10) turtle.pensize(2) turtle.pencolor('red') # 画玫瑰的花瓣 for _ in range(18): turtle.forward(100) turtle.left(100) turtle.forward(100) turtle.right(160) # 画玫瑰的花蕊 turtle.fillcolor('yellow') turtle.begin_fill() turtle.circle(10) turtle.end_fill() # 移动画笔到指定位置 def move_pen(x, y): turtle.penup() turtle.goto(x, y) turtle.pendown() # 主函数 def main(): # 隐藏画笔 turtle.hideturtle() # 画玫瑰 move_pen(0, 0) draw_rose() # 关闭画布 turtle.done() if __name__ == '__main__': main()
绘制圣诞树的代码:
import turtle # 设置画布大小 turtle.setup(800, 600) # 画圣诞树 def draw_tree(): turtle.speed(10) turtle.pensize(3) turtle.pencolor('green') # 画树干 turtle.fillcolor('brown') turtle.begin_fill() turtle.left(90) turtle.forward(100) turtle.right(90) turtle.backward(10) turtle.right(90) turtle.forward(10) turtle.right(90) turtle.backward(20) turtle.right(90) turtle.forward(10) turtle.right(90) turtle.backward(10) turtle.right(90) turtle.forward(100) turtle.end_fill() # 画树叶 turtle.fillcolor('green') turtle.begin_fill() turtle.left(180) turtle.circle(100, -180) turtle.right(180) turtle.forward(100) turtle.right(60) turtle.circle(80, -180) turtle.right(180) turtle.forward(80) turtle.right(120) turtle.circle(80, -180) turtle.right(180) turtle.forward(80) turtle.right(60) turtle.circle(100, -180) turtle.end_fill() # 移动画