Programming With Turtle
- The first thing you’ll learn when it comes to programming with the Python turtle library is how to make the turtle move in the direction you want it to go.
- You’ll learn how to customize your turtle and its environment.
- You’ll learn a couple of extra commands with which you can perform some special tasks.
Moving the Turtle
There are four directions that a turtle can move in:
- Forward
- Backward
- Left
- Right
→ The turtle moves .forward() or .backward() in the direction that it’s facing. You can change this direction by turning it .left() or .right() by a certain degree. You can try each of these commands like so:
Code:
#Follow @Programmerfect
import turtle as t
t.right(90)
t.forward(100)
t.left(90)
t.backward(100)
import turtle as t
t.right(90)
t.forward(100)
t.left(90)
t.backward(100)
Note:When you run these commands, the turtle will turn right by ninety
degrees, go forward by a hundred units, turn left by ninety degrees, and
move backward by a hundred units.
→ You can use the shortened versions of these commands as well:
- t.rt() instead of t.right()
- t.fd() instead of t.forward()
- t.lt() instead of t.left()
- t.bk() instead of t.backward()
You can also draw a line from your current position to any other arbitrary position on the screen. This is done with the help of coordinates:
The screen is divided into four quadrants. The point where the turtle is initially positioned at the beginning of your program is (0,0). This is called Home. To move the turtle to any other area on the screen.
→You use .goto() and enter the coordinates like this:
Code:
#Follow @Programmerfect
import turtle as t
t.goto(100,100)
import turtle as t
t.goto(100,100)