Skip to content

Colorful Spiral Pattern

This intermediate example builds on basic turtle graphics and loops to create a visually striking spiral pattern that changes colors as it grows. It’s a great way to practice combining multiple Ellex concepts like variables, loops, and color changes.

Below is the Ellex code to draw a spiral that increases in size and alternates between colors:

set distance to 5
set angle to 30
set count to 0
repeat 50 times:
when {count} = 0:
use color red
when {count} = 1:
use color blue
when {count} = 2:
use color green
when {count} = 3:
use color yellow
set count to 0
else:
set count to 0
move forward {distance}
turn right {angle}
set distance to {distance} + 2
set count to {count} + 1
  • set distance to 5: Starts with a small movement distance of 5 units.
  • set angle to 30: Sets the turning angle to 30 degrees, which helps create the spiral effect.
  • set count to 0: Initializes a counter to manage color changes.
  • repeat 50 times: Loops 50 times to build a large spiral.
  • Inside the loop:
    • Conditional statements (when) cycle through four colors (red, blue, green, yellow) by checking the count variable. After yellow, it resets count to 0 to restart the color cycle.
    • move forward {distance} draws a line of the current length.
    • turn right {angle} rotates the turtle to create the spiral shape.
    • set distance to {distance} + 2 increases the line length each iteration, making the spiral grow outward.
    • set count to {count} + 1 advances the color counter.

This code demonstrates how to use variables to control both the geometry of a drawing and aesthetic elements like color. It also shows nested control structures (when inside repeat) for more complex logic.

  1. Start the Ellex REPL by running make dev or use the web playground.
  2. Type or paste the code above and run it.
  3. Watch as Ellex draws a spiral that grows larger and cycles through red, blue, green, and yellow colors.
  • Change the angle value (e.g., to 45 or 15) to see how it affects the spiral’s tightness.
  • Adjust the distance increment (e.g., from + 2 to + 5) to make the spiral grow faster or slower.
  • Add more colors by extending the when conditions with new count values and resetting count at a higher number.

This example is a stepping stone to more advanced turtle graphics projects where you can combine user input or functions to create even more dynamic patterns!