Python Turtle Design of Spiral Square and Hexagon

Python Turtle Design of Spiral Square and Hexagon

Spiral Square

This part of this article will learn about a very cool Python Turtle Design of the spiral square. The code for the turtle program is given below. Accordingly, this article will create a clear basic concept about the turtle. We will first see the code, and then, we will understand it line-by-line. Now let’s see the code for the design.


Code:


import turtle as t

pen = t.Turtle()
pen.color("cyan")
pen.speed(0)

def draw_square():
        for side in range(4):
                pen.forward(100)
                pen.right(90)
                for side in range(4):
                        pen.forward(50)
                        pen.right(90)

pen.penup()
pen.back(20)
pen.pendown()

for square in range(80):
        draw_square()
        pen.forward(5)
        pen.left(5)
        
pen.hideturtle()

Output:


python turtle Spiral Square
Spiral Square


Explanation:


First Part:

  • First, import the “turtle” module and set the “pen” variable as the turtle.
  • Similarly, set the color of the turtle to “cyan” and set the speed to 0.

Function:

  • Accordingly, create a function name draw_square(). Inside the function, create a for loop with the range of 4. Then, move forward at the value of 100 and right at an angle of 90 degrees.
  • Likewise, create a for loop with a range of 4 inside of the for loop from before. Then, move forward at the value of 50 and right at an angle of 90 degrees.

Out of the function:

  • Coming out of the function draw_square(), call the penup() function. Then, go back with the value of 20 and call the pendown() function.

Last Part with Loop:

  • Similarly, create a for loop with the range of 80 and call the draw_square() function from before. After calling the draw_square() function, take the pen forward at the value of 5 and left at an angle of 5 degrees.
  • Lastly, end the code with the function hideturtle().

Spiral Hexagon:

In this part of this article, we will learn about a very cool Python Turtle Design of the spiral hexagon.


Code:


import turtle as t

def spiral(sides, trun, color, width):
    pen = t.Turtle()
    pen.color(color)
    pen.width(width)
    pen.speed(0)
    for n in range(sides):
        pen.forward(n)
        pen.right(trun)


spiral(150, 45, "cyan", 5)

Output:


python turtle Spiral Hexagon
Spiral Hexagon

Explanation:


First Part:

  • First, import the module “turtle” as t with the code below.
import turtle as t

Function:

  • Accordingly, create a function named spiral() with the parameters: sides, turn, color, width.
  • After, create a “p” variable and set it to the turtle.
  • Then, set the turtle’s color to color, which will be given while calling the function, set the width as “width” and speed to 0.
  • Likewise, create a for loop with the range of the value of “sides.” Inside this loop, move the pen forward at the “n” value and right at an angle of “trun.” You can change the variable and play with the code too.
  • Lastly, call the spiral() function with the arguments of your own wish.

Thank You for reading till the end. Comment your queries or anything you want to know or tell us based on this post or website. We have many other turtle tutorials on this website. You can see other tutorials too.

Keep Learning


Also read:


Python Turtle Side Look Emoji

Python Turtle Beautiful Design

Guess the Number Python

Snake Game in Python using Pygame

Rock Paper Scissor Game Using Python Tkinter

Password Generator Application In Python

Colored Hexagons Turtle Design


Share:

Author: Ankur Gajurel

I am Ankur from Nepal trying to learn different aspects of Information Technology and Physics. I like building websites and minor projects with Python.