Python Turtle Program To Design Colored Hexagons

Python Code

import turtle
from random import randint

# Variable for the expected circle value
x = 20

# Variable for the expected y circle
y = 20

# Set the speed of the pen
turtle.speed(100)

# Set the turtle colormode to 255
turtle.colormode(255)

# Function to move the turtle
def move(l, a):
    turtle.right(a)
    turtle.forward(l)

# Function to draw a hexagon
def hexagon():
    turtle.pendown()
    turtle.color(randint(0, 255), randint(0, 255), randint(0, 255))
    turtle.begin_fill()
    for _ in range(6):
        move(x, -60)
    turtle.end_fill()
    turtle.penup()

# Start the drawing
turtle.penup()

for i in range(y):
    if i == 0:
        hexagon()
        move(x, -60)
        move(x, -60)
        move(x, -60)
        move(0, 180)
    for _ in range(6):
        move(0, 60)
        for _ in range(i + 1):
            hexagon()
            move(x, -60)
            move(x, 60)
        move(-x, 0)
    move(-x, 60)
    move(x, -120)
    move(0, 60)

# This function keeps the turtle graphics window open until clicked
turtle.exitonclick()

Output

python-turtle-program-to-design-colored-hexagons

Leave a comment