"Turtle" — it's a drawing board-like feature of Python that allows us to command the turtle to draw all over the body! We can use functions like turtle.forward (…) and turtle.right (…) that can move the turtle. The following turtle methods are commonly used:
|Method||Parameter||Turtle ()||None||Creates and returns a new tutrle object|
|backward ()||amount||Moves the turtle backward by the specified amount|
|right ()||angle||left ()||Turns the turtle counter clockwise|
|/ td>||None||Picks up the turttle's Pentr>||pendown ()||None||Puts down the turtle's Pen|
|up ()||None||Picks up the turtle's >|
|down ()||Ndone class = "amp-wp-inline-0786f4623984c46e15421e6979be740d"> Puts down the turtle's Pen|
|color ()td class = "amp-wp-inline-f74fb0de8d71e0cc53e95b25025d2853"> Color name||Changes the color of the turtle's pen||fillcolor ()||Color name||Changes the color of the turtle will use to fill a polygon|
|position ()||Returns the current position|
|t goto () >||x, y||Move the turtle to position x, ytr>|
|begin_fill ()||None||Remember the starting point for a filled polygon|
|end_fill ()||None||dot ()||None7446||Leave the dot at the current position|
|stamp ()||None||Leaves an impression of a turtle shape location|
|shape ()||shapename||Should be 'arrow', 'classic', 'turtle' or 'circle td>|
Drawing with turtle
To to use the methods and functionality of the turtle, we need to import the turtle. Turtle comes with a standard Python package and does not require external installation. The roadmap for executing the turtle program has 4 steps:
So, as stated above, before we can use the turtle, we need to import it. We import it as:
from turtle import * # or import turtle
After importing the turtle library and giving us all the turtle functionality, we need to create a new drawing board (window) and turtle. Let's call the window wn and the turtle — skk. So we code as:
wn = turtle.Screen () wn.bgcolor ("light green") wn.title ("Turtle") skk = turtle.Turtle ()
Now that we have created the window and the turtle, we need to move the turtle. To move forward 100 pixels in the skk direction, we code:
We move forward 100 pixels, Cool! Now we end the program with the done () function, and you're done!
So, we've created a program that draws a line 100 pixels long. We can draw different shapes and fill different colors using turtle techniques. There are many functions and programs that need to be coded using the turtle library in python. Let's learn how to draw some of the basic shapes.
Shape 1: Square
| tr> |
Shape 2: Star
| tr> |
Shape 3: Hexagon
| tr> |
Visit pythonturtle.org, to get a taste of Turtle without python preinstalled. The shell in PythonTurtle is a complete Python shell, and you can do almost anything you can with it using the standard Python shell. You can create loops, define functions, create classes, etc.
You can access these codes for great turtle programs here
Some amazing turtle programs
1. Spiral square from outside to inside and inside out
2. User input template
3. Spiral spiral model
Trees using turtle programming
This article is courtesy of Amartya Ranjan Saikia . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please write in comments if you find anything wrong or if you'd like to share more information on the topic discussed above.