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

yipeiwu_com7年前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引用模块和查找模块路径

模块间相互独立相互引用是任何一种编程语言的基础能力。对于“模块”这个词在各种编程语言中或许是不同的,但我们可以简单认为一个程序文件是一个模块,文件里包含了类或者方法的定义。对于编译型的语...

Python3.4学习笔记之常用操作符,条件分支和循环用法示例

本文实例讲述了Python3.4常用操作符,条件分支和循环用法。分享给大家供大家参考,具体如下: #Pyhon常用操作符 c = d = 10 d /= 8 #3.x真正的除法 pr...

Python高级特性之闭包与装饰器实例详解

Python高级特性之闭包与装饰器实例详解

本文实例讲述了Python高级特性之闭包与装饰器。分享给大家供大家参考,具体如下: 闭包 1.函数参数: (1)函数名存放的是函数的地址 (2)函数名()存放的是函数内的代码 (3)...

python从入门到精通(DAY 1)

python从入门到精通(DAY 1)

1、要点    (1) 在C语言中没有字符串,只有字符,    在python中的字符串hello,在C语言中是以字符数组在内存存放['h','e...

python3的UnicodeDecodeError解决方法

python3的UnicodeDecodeError解决方法

爬虫部分解码异常 response.content.decode() # 默认使用 utf-8 出现解码异常 以下是设计的通用解码 通过 text 获取编码 # 通过...