In this lesson we are going to ask the turtle some questions in order to get some answers. We will get to know where, exactly, the turtle is and where he is heading.
Write the following command: cs repeat 24 [ rt 17 fd 33]
After we draw something nice, our turtle is somewhere in his world in an unknown position. In order to know the turtle’s current location we can ask the turtle: “What is your position?”. To ask a question we must use the print command, which will display the answer we are looking for. In this case we will also use the command pos for position. The answer will appear as x- and y-coordinates.
Print the turtle's location.
Now we know how to ask the turtle for his x- and y-coordinates. If we choose so, we can ask him for only one of the coordinates, using xcor or ycor, respectively.
Print the turtle’s current x-coordinate.
To know the exact direction (angle) the turtle is heading to, we can use the heading command.
Print the direction in which the turtle is heading.
So far we know our location and the direction where we are currently heading. Suppose we want to get to some specific point such as (0,0). We should know how to set the turtle head towards the point. The first thing we have to do is find the direction / angle in which we have to turn to get to the specified point. The command will be: towards list XCOR YCOR, where XCOR and YCOR are the point’s x- and y- coordinates respectively. The answer will be an absolute angle based on the 360 degrees of a circle.
Print where will we have to set the turtle head in order to get to the point (0,0).
In the previous step, we got an angle / direction as an output. Now we want to set the turtle's head to face the desired point (0,0) in the direction of that angle. Note that only one number is needed after the decimal: XXX.X, 123.4, etc.
Set the head toward the point (0,0).