Have Tracy draw an ‘X’ on the canvas to mark where the treasure is hidden!

Your code should:

Use a for loop that repeats 4 times
Draw an X with a total diagonal distance of 200 pixels
Note: Make sure Tracy is ending facing the same direction as in the result world!

1 answer

To draw an 'X' on a canvas using a for loop, you can create a simple code snippet in a drawing environment like Python's Turtle graphics, for example. Here, I'm providing a sample code that fulfills your requirements:

import turtle

# Set up the turtle
tracy = turtle.Turtle()
tracy.speed(1)  # Set a speed for drawing

# Function to draw an X
def draw_x():
    for _ in range(4):
        tracy.forward(100)  # Move forward 100 pixels
        tracy.backward(100)  # Move back to the start
        tracy.right(90)  # Turn right 90 degrees

# Move to the starting position
tracy.penup()  # Don't draw when moving to the start
tracy.goto(0, 0)  # Center the X on the canvas
tracy.pendown()  # Start drawing

draw_x()

# Keep the window open until clicked
turtle.done()

Explanation:

  1. Import the Turtle module: This is necessary to use turtle graphics.
  2. Set up the turtle: Create an instance of the turtle.
  3. Define a function draw_x: Inside this function:
    • Use a for loop that runs 4 times.
    • Draw each line of the 'X' by moving forward and then backward to create the diagonal.
    • Use right(90) to change direction for the next line of the 'X'.
  4. Position the turtle: Before drawing, move the turtle to the center of the canvas (0, 0), so the 'X' is centered.
  5. Call the draw_x function: This carries out the repeating action to draw the 'X'.
  6. Keep the window open: This is to allow us to see the drawn 'X' until we close the window.

Replace the movements and angles as per the requirements to get the exact orientation if needed. The final orientation adjustments can also be made, depending on the starting direction of the turtle.