Robot Turtle with Python
Robot Turtles
#Turtle Graphics programs are a cool part of computing history, and a useful way to teach programming.
One of the simpler ways to try out Turtle programming, today, is through the Python Turtle Module.
Getting Ready
#Tip: Be sure that Python is installed with the Tkinter module option enabled. This is usually a checkbox during install on Windows. On Linux and Mac it may be a specially named package, such as
python3-tk
.
sudo apt install python3-tk
Launching the Turtle Program
#Type the command python
into a terminal. Then, in the interactive Python session, type:
from turtle import *
Now, to list the available commands, type:
dir()
To learn more about a particular command, type use the help
function:
help(forward)
Tip: Press
Q
to exit the help viewer and return to Python.
To display the Turtle window, type your first command:
forward(100)
Tip: A new separate window should appear with an arrow pointing to the right. The tip of the arrow is your turle. The line coming out of the arrow is the 100 points of travel the turtle just completed.
Some interesting commands to explore:
penup()
- lift the pen to allow moving without drawingpendown()
- lower the pen to resume drawingright(45) / left(90)
rotate left or right the number of degrees specifiedxcor() / ycor()
- show the current x or y coordinateheading()
- get the current heading (in degrees)goto(0,0)
- go directly to the x and y cordinates given- `color('green') - change the turtle (and ink) to the given color
- `write('hello world') - write the given text next to the turtle
setx(100) / sety(100)
- jump to the given x / y coordiantestamp()
- "stamp" the turtle's current appearance into the canvasclearscreen()
- empty the canvas of all drawingspensize(10)
- change the pen line thickness to the given number of pixelshideturtle()
- hide the turtleshowturtle()
- show the turtleundo()
- undo the previous command
Tip: All commands whould end in
()
- and some take arguments - text between the()
.