python怎么绘制正方形螺旋线?

1、 安装turtle

Python2安装命令:

pip install turtule

Python3安装命令:

pip3 install turtle

因为turtle库主要是在Python2中使用的,所以安装的时候可能会提示错误:

Command "python setup.py egg_info" failed with error code 1

2 、利用turtle库绘制正方形螺旋线

(1)效果图

p7.jpg

(2)相关代码

          import turtle as t
          t.pen(speed=0) #加快绘图速度
          t.penup()
          t.goto(-200, -200) #以左下角某处为起点
          t.pendown()
          t.seth(0)
          length = 400
          while (length !=0): #利用正方形螺旋线的性质来绘图
          t.fd(length)
          t.left(90)
          length -= 2.5
          t.hideturtle() #绘图结束后把海龟头(笔触头)隐藏起来
          t.done() #绘图结束后使窗口停留

3 、绘图命令介绍

(1)turtle.speed(speed)            画笔绘制的速度范围[0,10]整数。

(2)turtle.goto(x,y)                    将画笔移动到坐标为x,y的位置。

(3)turtle.pendown()                 动时绘制图形,缺省时也为绘制。

来源:PY学习网:原文地址:https://www.py.cn/article.html

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » python怎么绘制正方形螺旋线?