adplus-dvertising
Coders Packet

Python Tutorial! Hexagonal Spiral using Turtle (Source Code with Output)

By Anita Suresh Lawate

Draw the spiral with hexagon shape using Turtle module of Python. In each loop, increases the forward length.

In this tutorial, we will learn that how to draw hexagon spirals using Turtle Graphics in Python.

Turtle Graphics:

Turtle is a python feature like a drawing board. We can use many turtle functions which can move the turtle around. Turtle comes in the turtle library. The Turtle module can be used in both object-oriented and procedure-oriented.

forward(length): moves the pen in the forward direction by specific unit.

backward(length): moves the pen in the backward direction by specific unit.

right(angle): rotate the pen in the clockwise direction by a specified angle.

left(angle): rotate the pen in the anticlockwise direction by a specified angle.

penup(): stop drawing of the turtle.

pendown(): start drawing the turtle pen.

 

let's start the code:

First, you have to import the turtle module.

from turtle import *

Then you decide which colors you want in that spiral. So, assign that six specific colors to the variable color.

colors = ['pink', 'green', 'orange', 'yellow', 'blue', 'brown', 'white']

After that you have set the speed.

speed(0)

Then assign the background color using bgcolor().

bgcolor('black')

Now, we are defining one for loop here.

for x in range(200):
    pencolor(colors[x % 6])
    width(2)
    forward(x*1.2)
    left(59)

for loop for hexagon executes 200 times.

After every move of the turtle, it changes the color.

In every iteration, the turtle moves 90 units forward and 59 degrees in left.

 

Let's see the whole source code.

from turtle import *

colors = ['pink', 'green', 'orange', 'yellow', 'blue', 'brown', 'white']
speed(0)
bgcolor('black')
for x in range(200):
    pencolor(colors[x % 7])
    width(2)
    forward(x*1.2)
    left(59)

 

Output:

 

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Anita Suresh Lawate (Anitacodespeedy)

Download packets of source code on Coders Packet