Turtle Graphics in Ellex
Turtle Graphics in Ellex
Section titled “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.
What is Turtle Graphics?
Section titled “What is Turtle Graphics?”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.
Getting Started with Turtle Commands
Section titled “Getting Started with Turtle Commands”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.
Moving the Turtle
Section titled “Moving the Turtle”move forward N
: Moves the turtle forward byN
units (a number like 50 or 100), drawing a line if the pen is down.move backward N
: Moves the turtle backward byN
units.
Example:
move forward 100
This moves the turtle forward 100 units, drawing a straight line if the pen is down.
Turning the Turtle
Section titled “Turning the Turtle”turn right N
: Turns the turtle right byN
degrees.turn left N
: Turns the turtle left byN
degrees.
Example:
move forward 50turn right 90move forward 50
This draws an L-shape by moving forward, turning right 90 degrees, and moving forward again.
Controlling the Pen
Section titled “Controlling the Pen”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 50pen upmove forward 50pen downmove forward 50
This draws a line, moves without drawing (creating a gap), and then draws another line.
Changing Colors
Section titled “Changing Colors”use color NAME
: Sets the drawing color toNAME
, which can be a color likered
,blue
,green
, or others supported by Ellex.
Example:
use color bluemove forward 100
This draws a blue line 100 units long.
Drawing Shapes
Section titled “Drawing Shapes”Ellex provides shortcuts for common shapes, making it easier to create complex drawings:
draw circle with radius N
: Draws a circle with a radius ofN
units.
Example:
draw circle with radius 30
This draws a circle with a 30-unit radius at the turtle’s current position.
Drawing Your First Shape: A Square
Section titled “Drawing Your First Shape: A Square”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.
Adding Colors to Your Drawings
Section titled “Adding Colors to Your Drawings”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 redmove forward 50turn right 90use color bluemove forward 50turn right 90use color greenmove forward 50turn right 90use color yellowmove forward 50turn right 90
This creates a square with red, blue, green, and yellow sides. Experiment with different color names to see what Ellex supports!
Creating Patterns with Loops
Section titled “Creating Patterns with Loops”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 10repeat 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.
Repositioning the Turtle
Section titled “Repositioning the Turtle”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 upmove forward 100pen downmove forward 50
This moves the turtle 100 units without drawing, then draws a 50-unit line from the new position.
Fun Projects to Try
Section titled “Fun Projects to Try”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!
Draw a House
Section titled “Draw a House”Create a simple house outline with a square base and a triangular roof:
# Square baserepeat 4 times: move forward 60 turn right 90# Move to start of roofpen upturn left 90move forward 60pen downturn right 45# Roofmove forward 42turn right 90move forward 42
This draws a house shape. Adjust the numbers to make it bigger or smaller!
Colorful Star
Section titled “Colorful Star”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.
Tips for Turtle Graphics
Section titled “Tips for Turtle Graphics”- 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.
Why Turtle Graphics Helps Learning
Section titled “Why Turtle Graphics Helps Learning”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.
Getting Help with Turtle Graphics
Section titled “Getting Help with Turtle Graphics”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?