Skip to content

Interactive Drawing Tool

This advanced example shows how to build an interactive drawing tool in Ellex that lets users control the turtle through a series of prompted commands. It combines user input, functions, loops, and conditional logic to create a dynamic experience, ideal for learners ready to tackle more complex programming concepts.

Below is the Ellex code for an interactive drawing tool that lets users choose actions like moving, turning, changing colors, and more:

make show_instructions:
tell "Welcome to the Interactive Drawing Tool!"
tell "Choose an action by typing a number:"
tell "1. Move Forward"
tell "2. Move Backward"
tell "3. Turn Right"
tell "4. Turn Left"
tell "5. Change Color"
tell "6. Pen Up (stop drawing)"
tell "7. Pen Down (start drawing)"
tell "8. Exit"
make get_user_choice:
ask "Enter a number (1-8):" → choice
set choice to {choice}
make move_forward_action:
ask "How many units forward?" → distance
move forward {distance}
make move_backward_action:
ask "How many units backward?" → distance
move backward {distance}
make turn_right_action:
ask "How many degrees to turn right?" → degrees
turn right {degrees}
make turn_left_action:
ask "How many degrees to turn left?" → degrees
turn left {degrees}
make change_color_action:
ask "Which color? (red, blue, green, yellow)" → color
when {color} is "red":
use color red
when {color} is "blue":
use color blue
when {color} is "green":
use color green
when {color} is "yellow":
use color yellow
else:
tell "Color not recognized. Using black."
use color black
make main_loop:
set running to true
repeat while {running} is true:
call show_instructions
call get_user_choice
when {choice} = 1:
call move_forward_action
when {choice} = 2:
call move_backward_action
when {choice} = 3:
call turn_right_action
when {choice} = 4:
call turn_left_action
when {choice} = 5:
call change_color_action
when {choice} = 6:
pen up
tell "Pen is up. Turtle won't draw."
when {choice} = 7:
pen down
tell "Pen is down. Turtle will draw."
when {choice} = 8:
set running to false
tell "Goodbye! Thanks for drawing!"
else:
tell "Invalid choice. Please pick a number between 1 and 8."
# Start the program
call main_loop

This code creates a menu-driven drawing tool where users can interactively control the turtle. Here’s how it works:

  • make show_instructions: Defines a function to display a menu of available actions (move, turn, color change, etc.).
  • make get_user_choice: Prompts the user to enter a number corresponding to their chosen action and stores it in choice.
  • Action Functions: Separate functions handle each action:
    • move_forward_action and move_backward_action ask for a distance and move the turtle.
    • turn_right_action and turn_left_action ask for degrees and turn the turtle.
    • change_color_action lets the user pick a color, with a fallback to black if the input isn’t recognized.
  • make main_loop: The core of the program, which:
    • Uses a repeat while loop to keep running until the user chooses to exit.
    • Calls show_instructions and get_user_choice each iteration.
    • Uses when conditions to match the user’s choice to the appropriate action.
    • Sets running to false when the user selects option 8 to exit.
  • Program Start: call main_loop starts the interactive tool.

This example demonstrates advanced concepts like:

  • Function definitions for modular, reusable code.
  • A game loop structure with user input driving the program’s flow.
  • Nested conditionals for handling multiple user choices.
  • State management with variables like running to control the loop.
  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. Follow the menu prompts to control the turtle:
    • Choose numbers 1-4 to move or turn the turtle.
    • Choose 5 to change the drawing color.
    • Choose 6 or 7 to toggle drawing mode.
    • Choose 8 to exit the tool.
  4. Watch as the turtle responds to your commands, drawing shapes and patterns based on your input.
  • Add More Features: Extend the menu with options like drawing a circle (draw circle with radius N) or resetting the turtle’s position.
  • Save Drawings: If Ellex supports file output, add an option to save the current drawing state or commands.
  • Custom Colors: Allow users to input custom color values if Ellex supports a wider color range.
  • Command History: Keep track of the last few actions and let users repeat them.

This example is a foundation for building more complex interactive applications in Ellex, showing how natural language syntax can handle sophisticated user-driven programs. It’s a great step toward projects like games or educational tools!