top of page

Example: Drawing a Colorful Spiral with turtle

How to Run:

  1. Install Python (if you haven’t already):

  2. Run the script:

    • Save the code to a file called spiral.py

    • Open a terminal or command prompt.

    • Run: python spiral.py

  3. Watch the magic!

    • A window will open, and you’ll see a colorful spiral being drawn.

import turtle
import colorsys

​

# Set up screen
screen = turtle.Screen()
screen.bgcolor("black")

​

# Create turtle
pen = turtle.Turtle()
pen.speed(0)
pen.width(2)

​

# Number of colors and steps
h = 0
n = 36  # number of loops

​

for i in range(360):
    c = colorsys.hsv_to_rgb(h, 1, 1)  # Generate color
    pen.color(c)
    pen.forward(i * 3 / n + i)
    pen.left(59)
    pen.forward(i * 3 / n + i)
    pen.left(60)
    h += 0.005

​

# Hide turtle and finish
pen.hideturtle()
turtle.done()

 

bottom of page