Draw Spider web using Python Turtle

Draw Spider web using Python Turtle

Introduction

Hello friends, today in this tutorial we are going to learn how to draw a Spider web using the Python Turtle module. The code is very simple, interesting, and easy to understand. We are providing the comments and detailed explanations for easy understanding of the code.

For the complete code go to the bottom of the page, along with the output provided for reference.

So let’s begin drawing

Import Turtle

import turtle as t

Here we have imported the turtle module as t, so that we can access its functions as t instead of using the turtle as an object.

Draw the radical thread of Spider web

# define turtle speed
t.speed(2)

# radical thread
for i in range(6):
   t.forward(150)
   t.backward(150)
   t.right(60)

# spiral thread length
side = 50
t.fillcolor("Orange")

Here we have initialized the speed of the turtle as 2. For radical thread creation, we have made use of for loop. The loop will run 6 times. The forward and backward function to move 150 steps to and fro with the angle of right(60).

The length of the spiral thread is 50. The spider web color is initialized to Orange.

Create a web for Spiral web using Python Turtle

# building web
t.begin_fill()

for i in range(15):
   t.penup()
   t.goto(0, 0)
   t.pendown()
   t.setheading(0)
   t.forward(side)
   t.right(120)

   for j in range(6):
      t.forward(side-2)
      t.right(60)
   side = side - 10

Here we started creating web threads using for loop. We have initialized the position of the web to goto(0,0). The setheading(0) will point in the east direction. The thread generates 15 times as given in the for loop. It is getting forward by 50 steps keeping an angle of 120 degrees.

For each thread iteration in the range of 15, there is another for loop of range 6. Its side decreases by 2 providing an angle of 60 degrees.

After the inner for loop iteration completes, the side decreases by 10 every time.

Complete Code to Draw Spider web using Python Turtle

import turtle as t

# define turtle speed
t.speed(2)

# radical thread
for i in range(6):
   t.forward(150)
   t.backward(150)
   t.right(60)

# spiral thread length
side = 50

# Spider web color
t.fillcolor("Orange")

# building web
t.begin_fill()

for i in range(15):
   t.penup()
   t.goto(0, 0)
   t.pendown()
   t.setheading(0)
   t.forward(side)
   t.right(120)
   # Inner for loop of range 6
   for j in range(6):
      t.forward(side-2)#for each iteration side decreases by 2
      t.right(60)
   side = side - 10 #Side decreases by 10
#Fill color completes
t.end_fill()
turtle.done()

Output

Output of spider web using Python Turtle
Spider web using Python Turtle

So here we have created this beautiful spider web using Python turtle module.

Thank you for reading this article on our website.


Also Read:

Share:
Avatar of pranjal dev

Author: pranjal dev