# 绘制分形树****************************************************************************** import turtle def draw_branch(branch_length): ''' 绘制分形树 ''' if branch_length > 5: # 绘制右侧树枝 turtle.forward(branch_length) print("向前:", branch_length) turtle.right(20) print("右转:20度") draw_branch(branch_length - 15) # 绘制左侧树枝 turtle.left(40) print("左转:40度") draw_branch(branch_length - 15) # 返回之前的树枝 turtle.right(20) print("右转:20度") turtle.backward(branch_length) print("向后:", branch_length) def main(): ''' 主函数 ''' turtle.speed(0.5) turtle.pensize(3) turtle.left(90) turtle.color('green') turtle.penup() turtle.backward(150) turtle.pendown() turtle.bgcolor("black") draw_branch(100) turtle.exitonclick() if __name__ == "__main__": main()
# 彩虹线绘制案例*************************************************************************** import turtle as t from random import randint as rint t.shape("turtle") t.pensize(5) t.colormode(255) t.bgcolor("black") t.tracer(False) for x in range(700): t.color(rint(0,255),rint(0,255),rint(0,255)) t.circle(2*(1+x/4),5) t.speed(0) t.tracer(True) t.exitonclick()
这些画图代码希望可以解决平时学习Python乏味和枯燥