Skip to content

Turtle Graphics in Ellex

Ellex includes a fun and interactive feature called Turtle Graphics, which allows you to create drawings and visual patterns by controlling a virtual “turtle” that moves around the screen. This guide will introduce you to turtle graphics in Ellex, showing you how to draw shapes, change colors, and build creative projects with simple commands.

Turtle Graphics is a method of programming where you give instructions to a virtual turtle to move, turn, and draw lines as it travels. Think of the turtle as a little robot with a pen attached to it. You can tell it to move forward or backward, turn left or right, and choose whether the pen is down (drawing) or up (not drawing). This makes it easy to create pictures and designs with code.

In Ellex, turtle graphics are enabled by default, so you can start drawing right away in the REPL or web playground. It’s a great way for young learners to see the immediate results of their code and experiment with geometry and creativity.

Let’s learn the basic commands for controlling the turtle in Ellex. These commands are designed to sound natural, just like the rest of the language.

  • move forward N: Moves the turtle forward by N units (a number like 50 or 100), drawing a line if the pen is down.
  • move backward N: Moves the turtle backward by N units.

Example:

move forward 100

This moves the turtle forward 100 units, drawing a straight line if the pen is down.

  • turn right N: Turns the turtle right by N degrees.
  • turn left N: Turns the turtle left by N degrees.

Example:

move forward 50
turn right 90
move forward 50

This draws an L-shape by moving forward, turning right 90 degrees, and moving forward again.

  • pen down: Puts the pen down so the turtle draws a line as it moves (this is the default state).
  • pen up: Lifts the pen up so the turtle moves without drawing.

Example:

move forward 50
pen up
move forward 50
pen down
move forward 50

This draws a line, moves without drawing (creating a gap), and then draws another line.

  • use color NAME: Sets the drawing color to NAME, which can be a color like red, blue, green, or others supported by Ellex.

Example:

use color blue
move forward 100

This draws a blue line 100 units long.

Ellex provides shortcuts for common shapes, making it easier to create complex drawings:

  • draw circle with radius N: Draws a circle with a radius of N units.

Example:

draw circle with radius 30

This draws a circle with a 30-unit radius at the turtle’s current position.

Let’s put these commands together to draw a simple square. A square has four equal sides and four 90-degree angles, so we’ll repeat a pattern of moving and turning:

repeat 4 times:
move forward 50
turn right 90

When you run this in the Ellex REPL or web playground, you’ll see the turtle draw a square with sides 50 units long. Each move forward 50 draws a side, and turn right 90 positions the turtle for the next side.

You can make your drawings more exciting by using different colors. Let’s draw a colorful square where each side is a different color:

use color red
move forward 50
turn right 90
use color blue
move forward 50
turn right 90
use color green
move forward 50
turn right 90
use color yellow
move forward 50
turn right 90

This creates a square with red, blue, green, and yellow sides. Experiment with different color names to see what Ellex supports!

Loops (repeat) are powerful for creating repeating patterns with turtle graphics. Here’s how to draw a spiral by gradually increasing the distance the turtle moves while turning:

set distance to 10
repeat 20 times:
move forward {distance}
turn right 30
set distance to {distance} + 5

This starts with a small move of 10 units, turns right 30 degrees, and increases the move distance by 5 each time. The result is a spiral that grows outward.

Sometimes, you need to move the turtle to a new starting point without drawing. Use pen up to lift the pen, move, and then pen down to start drawing again. You can also directly set the turtle’s position if Ellex supports coordinate commands:

pen up
move forward 100
pen down
move forward 50

This moves the turtle 100 units without drawing, then draws a 50-unit line from the new position.

Here are a few ideas to practice turtle graphics in Ellex. Type these into the REPL (make dev) or web playground and see what you create!

Create a simple house outline with a square base and a triangular roof:

# Square base
repeat 4 times:
move forward 60
turn right 90
# Move to start of roof
pen up
turn left 90
move forward 60
pen down
turn right 45
# Roof
move forward 42
turn right 90
move forward 42

This draws a house shape. Adjust the numbers to make it bigger or smaller!

Draw a star shape with alternating colors:

repeat 5 times:
use color red
move forward 100
turn right 144
use color blue
move forward 100
turn right 144

This creates a five-pointed star with red and blue lines. The angle 144 degrees ensures the points connect correctly.

  • Start Small: Begin with simple shapes like lines or squares before trying complex patterns.
  • Experiment with Angles: Different turn angles (like 45, 60, or 120 degrees) can create unique shapes.
  • Use Loops for Patterns: Repeating small movements with slight changes can make spirals, stars, or waves.
  • Check the Display: Make sure you’re using the Ellex REPL or web playground to see the turtle’s output visually.

Turtle Graphics in Ellex is more than just fun—it’s a powerful learning tool. It teaches concepts like sequencing (order of commands), loops (repeating actions), and geometry (angles and distances) in a visual, hands-on way. Seeing your code come to life as a drawing helps you understand how programming works and encourages creativity.

As you become more comfortable, you can combine turtle graphics with other Ellex features, like user input (ask) to let users choose colors or shapes, or functions (make) to reuse drawing patterns.

If your turtle drawing doesn’t look right, Ellex offers friendly error messages with suggestions. For example, if you type a command incorrectly, it might say, “Did you mean ‘move forward 50’? Let’s try that! 🐢”. You can also check the Ellex community forums (coming soon) or revisit this guide for ideas.

Ready to draw? Open the Ellex REPL with make dev or use the web playground, and start creating with turtle graphics. What cool design will you make first?