Creating a beautiful kaleidoscope using Python's turtle
graphics can be a fun and mesmerizing experience. Below is a Python script to draw a kaleidoscope-like pattern using the turtle
module:
import turtle import random # Function to draw the kaleidoscope pattern def draw_kaleidoscope(size, sides, repetitions): colors = ["red", "orange", "yellow", "green", "blue", "purple"] turtle.speed(0) for _ in range(repetitions): turtle.color(random.choice(colors)) for _ in range(sides): turtle.forward(size) turtle.left(360 / sides) turtle.left(360 / repetitions) # Set up the turtle window turtle.bgcolor("black") turtle.title("Kaleidoscope") turtle.setup(width=800, height=800) # Hide the turtle and set initial position turtle.hideturtle() turtle.penup() turtle.goto(0, -300) turtle.pendown() # Draw the kaleidoscope pattern draw_kaleidoscope(100, 6, 36) # Close the window on click turtle.exitonclick()
Save the script in a file with a .py
extension, and run it. A window with a beautiful kaleidoscope pattern will appear. The draw_kaleidoscope
function takes three arguments: size
for the length of each side of the pattern, sides
for the number of sides the shape has, and repetitions
for the number of repetitions to create the kaleidoscope effect.
Feel free to adjust the size
, sides
, and repetitions
parameters to create different variations of the kaleidoscope pattern. Additionally, you can customize the colors and other aspects of the pattern to make it even more beautiful and mesmerizing!