Drawing Colored and Uncolored Spider Web using Python Turtle Module

Introduction

The Logo programming language is frequently linked to turtle graphics. In the late 1960s, Seymour Papert added turtle graphics support to Logo to support his version of the turtle robot, which is a simple robot controlled from the user’s workstation and designed to carry out the drawing functions assigned to it using a small retractable pen set into or attached to the robot’s body.

The standard library of the Python programming language now contains a Turtle graphics module. Turtle in Python, like its Logo ancestor, allows programmers to manipulate one or more turtles in a two-dimensional space.

Overview of the syntax

A location, an orientation (or direction), and a pen are the three qualities of the turtle. Color, width, and on/off state are all properties of the pen (also called down and up).

“Move ahead 10 spaces” and “turn left 90 degrees” are orders that the turtle responds to based on its current location. The turtle’s pen can also be managed by enabling it, changing its color, and adjusting its breadth. By visualizing what they would do if they were the turtle, a pupil may comprehend (and forecast and reason about) the turtle’s motion. This is referred to as “body syntonic” reasoning by Seymour Papert.

Basic syntax:

import turtle // start of the program
     //body
    //of the main
   //code
turtle.done() //end of the program

Inorder to understand the codes to draw various shapes given below, Getting Started with Powerful yet Easy Python Graphics Module, Turtle.

Uncolored Spider Web

The following is the code for drawing uncolored spider web using python turtle module.

import turtle
 
tom = turtle.Turtle()

#code for defining turtle speed
tom.speed(2)
 
#Code for radical thread
for i in range(6):
  tom.forward(150)
  tom.backward(150)
  tom.right(60)
 
#Code for spiral thread length
side = 150



for i in range(15):
  tom.penup()
  tom.goto(0,0)
  tom.pendown()
  tom.setheading(0)
  tom.forward(side)
  tom.right(120)
  for j in range(6):
    tom.forward(side)
    tom.right(60)
  side = side - 10


turtle.done()

From above code we can observe the following:

The turtle is swung back and forth to begin constructing the radical strands. Each radical thread is drawn by rotating the turtle by 60 degrees. The spiral thread’s length is set to 150 and then lowered by 15 per cycle. The inner loop is in charge of creating single spiral threads and stacking the web, while the outer loop is in charge of determining the number of spirals to be created.

Output

Colored Spider Web

The following is the code for drawing uncolored spider web using python turtle module.

import turtle
 
tom = turtle.Turtle()

#code for defining turtle speed
tom.speed(2)
 
#Code for radical thread
for i in range(6):
  tom.forward(150)
  tom.backward(150)
  tom.right(60)
 
#Code for spiral thread length
side = 150

tom.color("Black", "Green")

tom.begin_fill()

for i in range(15):
  tom.penup()
  tom.goto(0,0)
  tom.pendown()
  tom.setheading(0)
  tom.forward(side)
  tom.right(120)
  for j in range(6):
    tom.forward(side)
    tom.right(60)
  side = side - 10

tom.end_fill()

turtle.done()

From above code we basically added 3 lines of code:

  • tom.begin_fill() / tom.end_fill(): The .begin_fill() and .end_fill() do as the name suggests.
  • tom.color(“black”, “green”): Here, .color() function takes two colors (black and green) as .color(“border_color”, “fill_color”). Where from the output given below we can observe black border and the green fill.

Output

SHARE Drawing Colored and Uncolored Spider Web using Python Turtle Module

You may also like...

Leave a Reply

Your email address will not be published.

Share