Colored Hexagons Turtle Design

Colored Hexagons Turtle Design

In this article, we will be learning about a very cool Colored Hexagons Turtle Design in Python. You can copy this code try it. There are many other articles on Turtle on this website. You can search turtle in the search box on the homepage. This article mainly targets beginners in Turtle. This is a basic turtle tutorial that will help you understand some basic concepts of turtle. After this tutorial, you will be able to draw some of your own concepts. The code for the turtle program is given below.

Requirements:

Little knowledge on random
Little knowledge on turtle

Code:


import turtle
from random import randint
#x for the expected circle value 1
x = 20
#variable for the expected gajurel circle 2
gajurel = 20
#the speed of the pen value 3 
turtle.speed(100)
# the turtle colormode set to 255
turtle.colormode(255)
#the move function
def move(l, a):
                turtle.right(a)
                turtle.forward(l)
#the hex function
def hex():
        turtle.pendown()
        turtle.color( randint(0,255),randint(0,255),randint(0,255) )
        turtle.begin_fill()
        for i in range(6):
                move(x,-60)
        turtle.end_fill()
        turtle.penup()

# start the drawing
turtle.penup()
#loop that draws the number of pentagons.
for ankur in range (gajurel):
        if ankur == 0:
                hex()
                move(x,-60)
                move(x,-60)
                move(x,-60)
                move(0,180)
        for i in range (6):
                move(0,60)
                for j in range (ankur+1):
                        hex()
                        move(x,-60)
                        move(x,60)
                move(-x,0)
        move(-x,60)
        move(x,-120)
        move(0,60)
#this function runs the code until the mouse is clicked
turtle.exitonclick()

Output:




Explanation:

First Part:

  • In the first part of this Colored Hexagons Turtle Design code, import the turtle module and the random module. The random module will be used to generate a random number and we will be drawing pentagons in the same pattern of color which we will be discussing below. Then, let the variable “x” be 20 and the variable “gajurel” also be 20. Now, set the speed to 100 and color mode to 255.

Second Part with Functions:

  • Create a function move() with the parameters l and a. In the the function we will be moving the turtle right at an angle of a and moving the turtle forward at a value of l with the code above. Similarly, We will also be creating a function hex(). In this function, first we will be call the pendown() function and setting the color from (radinint(0, 255), randint(0, 255), randint(0, 255)). Likewise, we will be calling the function begin_fill() and create a for loop with the range of 6. In this for loop, we will move from (x, -60). Lastly, we will call the end_fill() function and the penup() function.

Last Part with a large Loop:

  • Accordingly, we will be creating a for loop with the range of the value of the “gajurel” variable. Then, create an if statement which will run if “ankur” is equal to 0. Similarly, call the hex() function and move in the (x, -60) pattern three times and lastly move in (0, 180). Coming out of the if statement, create a for loop with the range of 6 and move (0, 60). moreover, we will be creating another for loop with the range of (ankur + 1). In this nested loop, call the hex() function and move (x, -60) and (x, 60) respectively. Coming out of two for loops, write the following code.
move(-x,60)
move(x,-120)
move(0,60)

Ending the code:

  • Lastly, call the exitonclick() function and end the code.

We will be posting many different turtle designs in the future and our latest selenium tutorial is also published. Check out the bubble sort algorithm too. Thank You for reading till end.

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


Share:

Author: Ayush Purawr