banner



How To Draw A Spiral In Python Turtle

Colorful Spiral Web Using Turtle Python

Pravallika Devireddy

"Turtle" is a python feature like a drawing board, which lets you command a turtle to draw all over it!

You can use functions like turtle.forward(...) and turtle.left(...) which can move the turtle around.

Before you can use turtle, you have to import it. We recommend playing around with it in the interactive interpreter first, as there is an extra bit of work required to make it work from files. Just go to your terminal and type:

                      import            turtle
turtle.forward(25)

                      turtle.left(30)                  

The turtle.forward(...) function tells the turtle to move forward by the given distance. turtle.left(...) takes a number of degrees which you want to rotate to the left. There is also turtle.backward(...) and turtle.right(...), too.

The standard turtle is just a triangle. That's no fun! Let's make it a turtle instead with the turtle.shape() command:

                      turtle.shape("turtle")                  

So much cuter!

If you put the commands into a file, you might have recognized that the turtle window vanishes after the turtle finished its movement. (That is because Python exits when your turtle has finished moving. Since the turtle window belongs to Python, it goes away as well.) To prevent that, just put turtle.exitonclick() at the bottom of your file. Now the window stays open until you click on it:

                      import            turtle
turtle.shape("turtle")
turtle.forward(25)
turtle.exitonclick()

Drawing a square

To draw a square as in the following picture:

For a square you will probably need a right angle, which is 90 degrees.

                      turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)

Now let us see how to draw a color spiral using turtle,

                      import turtle                                #Defining colors
colors = ['red','yellow','green','purple','blue','orange']
#Set up the turtle Pen
t = turtle.Pen()
#Changes the speed of the turtle
t.speed(10)
#Changes the background color of turtle
turtle.bgcolor("Black")
#Make Spiral Web
for valin range(200):
t.pencolor(colors[val%6])
t.width(val/100 + 1)
t.forward(val)
t.left(59)
turtle.done()

Output:

How To Draw A Spiral In Python Turtle

Source: https://medium.com/learning-python-programming-language/colorful-spiral-web-using-turtle-python-1f515ca88f25

Posted by: sainanderser.blogspot.com

0 Response to "How To Draw A Spiral In Python Turtle"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel