python 叠加等边三角形的绘制的实现

yipeiwu_com6年前Python基础

python语言程序设计基础 习题2.5

 import turtle
def drawTriangle(num,len,flag):#flag用来调整画三角形的方向
  flag*=-1
  len/=2
  if(num==1):
    if(flag==1):
      turtle.left(60)
      turtle.fd(len)
      turtle.right(120)
      turtle.fd(len)
      turtle.right(120)
      turtle.fd(len)
      turtle.left(120)
    else:
      turtle.right(60)
      turtle.fd(len)
      turtle.left(120)
      turtle.fd(len)
      turtle.left(120)
      turtle.fd(len)
      turtle.right(120)
  else:
    if(flag==1):
      turtle.left(60)
      turtle.fd(len/2)
      num=num-1
      turtle.right(60)
      drawTriangle(num,len,flag)
      turtle.fd(len/2)
      turtle.right(120)
      turtle.fd(len)
      turtle.right(120)
      turtle.fd(len)
      turtle.left(120)#注意递归过程中画笔返回的角度
    else:
      turtle.right(60)
      turtle.fd(len/2)
      num=num-1
      turtle.left(60)
      drawTriangle(num,len,flag)
      turtle.fd(len/2)
      turtle.left(120)
      turtle.fd(len)
      turtle.left(120)
      turtle.fd(len)
      turtle.right(120)#注意递归过程中画笔返回的角度
    

def main():
  turtle.setup(600,600, 200, 200)
  turtle.pendown()
  turtle.speed(2)
  turtle.pensize(5)
  drawTriangle(5,512,-1)
  turtle.hideturtle()#隐藏画笔图标
main()
turtle.done()#which returns after the main loop exits不知道啥意思,反正可以让窗口停住(欢迎评论告诉我啊)

​以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用pandas处理CSV文件的实例讲解

Python使用pandas处理CSV文件的实例讲解

Python中有许多方便的库可以用来进行数据处理,尤其是Numpy和Pandas,再搭配matplot画图专用模块,功能十分强大。 CSV(Comma-Separated Values)...

Python Numpy 自然数填充数组的实现

今天学习Numpy时,想到了一个小问题。在Numpy中,随机生成array是比较容易的,用np.random.rand即可。如下 a = np.random.rand(3,4) 可...

Python2.x利用commands模块执行Linux shell命令

用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands...

python使用xlrd模块读写Excel文件的方法

本文实例讲述了python使用xlrd模块读写Excel文件的方法。分享给大家供大家参考。具体如下: 一、安装xlrd模块 到python官网下载http://pypi.python....

使用Python神器对付12306变态验证码

使用Python神器对付12306变态验证码

临近春节,【听图阁-专注于Python设计】小编带领大家用Python抢火车票! 首先我们需要splinter 安装: pip install splinter -i http://py...