"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 | |
---|---|---|---|---|---|
forward () | amount | ||||
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 Pen | tr>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 nameChanges the color of the turtle’s pen | fillcolor () | Color name | Changes the color of the turtle will use to fill a polygon | |
heading () | None | ||||
position () | Returns the current position | ||||
t goto () > | x, y | Move the turtle to position x, y | |||
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:
- Import the turtle module
- Create a turtle to control.
- Draw using turtle methods
- Run turtle.done ().
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:
skk.forward (100)
We move forward 100 pixels, Cool! Now we end the program with the done () function, and you’re done!
turtle.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
|
Shape 2: Star
|
Shape 3: Hexagon
|
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
|
|
Output:
2. User input template
|
3. Spiral spiral model
|
|
Output:
Trees using turtle programming
References :
- Turtle documentation for Python 3 and 2
- eecs.wsu.edu [PDF] !
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.
Shop
Latest questions