Updated April 14, 2023
Introduction to Python Turtle
Turtle graphics is a remarkable way to introduce programming and computers to kids and others with zero interest and is fun. Shapes, figures and other pictures are produced on a virtual canvas using the method Python turtle. A turtle created on the console or a window of display (canvas-like) which is used to draw, is actually a pen (virtual kind). Turtle backward function and turtle up, down, left-right, and other functions are used to move the turtle along the canvas to draw patterns along with its motion.
Python turtle() function is used to create shapes and patterns like this.
Syntax:
from turtle import *
Parameters Describing the Pygame Module:
Use of Python turtle needs an import of Python turtle from Python library.
- Methods of classes: Screen and Turtle are provided using a procedural oriented interface. For understandability, methods have the same names as correspondence. An object-oriented interface is to be used to create multiple turtles on a screen.
Methods of Python Turtle
Common methods used for Python turtle are:
- Turtle(): Used to create and return a new turtle object.
- forward(value): With respect to the value specified, the turtle moves forward.
- backward(value): With respect to the value specified, the turtle moves backwards.
- right(angle): Clockwise turn of the turtle.
- left(angle): Counterclockwise turn of the turtle.
- penup(): Turtle pen is picked up.
- pendown(): Turtle pen put down.
- up(): Same as penup ().
- down(): same as pendown ().
- color(color name): Turtle pen’s color is changed.
- fillcolor(color name): Color used to fill a particular shape is changed.
- heading(): Current heading is returned.
- position(): Current position is returned.
- goto(x, y): Moves position of turtle to coordinates x, y.
- end_fill(): Current fill color is filled after closing the polygon.
- begin_fill(): The starting point is remembered for a filled polygon.
- dot(): Dot is left at the current position.
- stamp(): Impression of turtle shape is left at the current position.
- Shape(): Should be – ‘turtle’, ‘classic‘, ‘arrow‘ or ‘circle‘.
Examples of Python Turtle
A package called the standard python package contains the turtle, which is not needed to be installed externally.
- The turtle module is imported.
- A turtle to control is created.
- Methods of turtle are used to play or draw around.
- Run the code using turtle.done() .
Initially, the turtle is imported as:
import turtle
or
from turtle import *
A new drawing board or a window screen is to be created and a turtle to draw. To do that, let’s give commands such and call them window_ for creating a window and aaa as our turtle name.
Window_ = turtle.Screen()
window_.bgcolor ( " light green " )
window_.title ( " Turtle " )
aaa = turtle.Turtle ()
Now that a window to draw is created, we need to make the window accessible to draw. Giving commands and turtle methods can help us do the same. Let’s say we want to move the turtle forward 120 pixels, meaning the turtle’s direction is extended with a movement of 200 pixels and is shown by a line of distance 200. The command will be:
aaa.forward ( 200 )
Now that we have given a command, the turtle moves forward for 120 pixels. We should complete the code by using the done() function.
turtle.done ()
Example #1
Code:
import turtle
polygon_ = turtle.Turtle()
for i in range(6):
polygon_.forward(100)
polygon_.right(300)
turtle.done()
Output:
Example #2
Code:
import turtle
star = turtle.Turtle()
num_of_sides = 5
length_of_side = 50
each_angle = 720.0 / num_of_sides
for i in range(num_of_sides):
star.forward(length_of_side)
star.right(each_angle)
turtle.done()
Output:
Example #3
Code:
from turtle import *
colors = ['orange', 'red', 'pink', 'yellow', 'blue', 'green']
for x in range(360):
pencolor(colors[x % 6])
width(x / 5 + 1)
forward(x)
left(20)
Output:
Example #4
Code:
from turtle import *
penup()
for a in range(40, -1, -1):
stamp()
left(a)
forward(20)
Output:
Conclusion
Using basic commands, which are readable and understandable, any person can create a window canvas like draw box to draw whatever they want just by giving the parameters for the turtle to move in the direction of desire. All the functions are the instructions for the Python program to follow. Results can be beautiful if created patterns and beautiful designs. Python Turtle is a great method to encourage kids to acknowledge more about programming, especially Python.
Recommended Articles
We hope that this EDUCBA information on “Python Turtle” was beneficial to you. You can view EDUCBA’s recommended articles for more information.