Drawing Shapes
XTurtle is a package for drawing lines and shapes in a window. It is also a fun way to learn to program. Let's start by creating a window and drawing an equilateral triangle. To do this begin by starting IDLE as we did in past lessons. Create a New Window and then type in the code below.
This first line imports the xturtle code into your program. Then you can create a Turtle, in this example called t. Then you can tell the Turtle named t to do things. For instance it can move around leaving a trail behind it.
The t.exitOnClick() tells the turtle to wait for you to click the mouse inside the window when it is done drawing. Once you click the window will be closed. It also allows you to move the window around. If a Turtle graphics window ever gets stuck on the screen you can close the Python Shell window and the Turtle graphics window will close too. The next time you run a program in IDLE, a new Shell Window will appear.
Run your program and see what happens!
You Try It
Change your code above to draw a square. What has to happen?
Don't Make Me Repeat Myself!!
Having to write the same thing over and over again is boring. To avoid that, we can use a for loop like we saw in previous examples.
You Try It
Modify your program to use a for loop to draw a square.