在Python中使用Turtle绘制椭圆 | 您所在的位置:网站首页 › 安全生产讲座讲稿 › 在Python中使用Turtle绘制椭圆 |
在Python中使用Turtle绘制椭圆
Turtle是Python中的一个内置模块。它提供了使用屏幕(纸板)和Turtle(笔)的绘画。为了在屏幕上画东西,我们需要移动Turtle(笔)。为了移动Turtle,有一些函数,如forward()、backward()等。 步骤: 使用的步骤如下 : Import turtle 将椭圆分成四条弧线 定义一种方法来形成这些成对的弧线 调用该函数。以下是实现: # import package import turtle # method to draw ellipse def draw(rad): # rad --> radius of arc for i in range(2): # two arcs turtle.circle(rad,90) turtle.circle(rad//2,90) # Main section # tilt the shape to negative 45 turtle.seth(-45) # calling draw method draw(100)输出 : 使用的步骤如下 : Import turtle Set Screen 将椭圆分成四条弧线 定义一种方法来形成这些成对的弧线 为不同的颜色多次调用该函数。以下是实现: # import package and making object import turtle screen = turtle.Screen() # method to draw ellipse def draw(rad): # rad --> radius for arc for i in range(2): turtle.circle(rad,90) turtle.circle(rad//2,90) # Main Section # Set screen size screen.setup(500,500) # Set screen color screen.bgcolor('black') # Colors col=['violet','blue','green','yellow', 'orange','red'] # some integers val=10 ind=0 # turtle speed turtle.speed(100) # loop for multiple ellipse for i in range(36): # oriented the ellipse at angle = -val turtle.seth(-val) # color of ellipse turtle.color(col[ind]) # to access different color if ind==5: ind=0 else: ind+=1 # calling method draw(80) # orientation change val+=10 # for hiding the turtle turtle.hideturtle()输出 : |
CopyRight 2018-2019 实验室设备网 版权所有 |