Colorful Spiral Pattern
Colorful Spiral Pattern in Ellex
Section titled “Colorful Spiral Pattern in Ellex”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.
Code Example
Section titled “Code Example”Below is the Ellex code to draw a spiral that increases in size and alternates between colors:
set distance to 5set angle to 30set count to 0repeat 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} + 1Explanation
Section titled “Explanation”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 thecountvariable. After yellow, it resetscountto 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} + 2increases the line length each iteration, making the spiral grow outward.set count to {count} + 1advances the color counter.
- Conditional statements (
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.
Try It Yourself
Section titled “Try It Yourself”- Start the Ellex REPL by running
make devor use the web playground. - Type or paste the code above and run it.
- Watch as Ellex draws a spiral that grows larger and cycles through red, blue, green, and yellow colors.
Experiment Further
Section titled “Experiment Further”- Change the
anglevalue (e.g., to 45 or 15) to see how it affects the spiral’s tightness. - Adjust the
distanceincrement (e.g., from+ 2to+ 5) to make the spiral grow faster or slower. - Add more colors by extending the
whenconditions with newcountvalues and resettingcountat 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!