“I fear not the man who has practiced 10,000 kicks once, but I fear the man who has practiced one kick 10,000 times.” -Bruce Lee
Recently, I read this famous quote and realized that exploring one language is more beneficial than learning many. While exploring Python, I have learned about several amazing pre-installed modules in python like random, venv, time, textwrap, tkinter, collections, etc.
So I initiate to explore one of the most interesting modules i.e. turtle module,
turtle is a pre-installed Python library. This module gives you a pen called a turtle and a canvas to create illustrations and intricate shapes. This is an amazing module as it uses animation and makes this module more fascinating.
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter(Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit, and is Python’s de-facto standard GUI.) for the underlying graphics.
Working with the turtle module is not so laborious the only prerequisites are your basic concepts and knowledge of Python. Into the bargain, this module helps you to make your logic strong and enhance your coding skills. The turtle module is not just about facile drawing and games only, this module also sanctions you for some complex stuff and to build games by incorporating some other module like random, time, etc.
Basic functions/methods you must know:
- forward() —forward() method is used to move the turtle. This method takes 1 argument.
- back() — back() method is used to move the turtle backward. This method takes 1 argument.
- right() — right() method is used to change the direction of the turtle() in right direction. This method takes 1 argument.
- left() — left() method is used to change the direction of the turtle() in left direction. This method takes 1 argument.
- circle() — This method is used to draw a circle with a given radius. This method takes 1 or 2 or 3 arguments.
- bgcolor() — To change the color of the screen /canvas. Argument type is string.
- pencolor() — To change the color of pen/turtle. Argument type is string.
- fillcolor()-To fill the color in shape. Argument type is string.
- done() /mainloop() — to hold screen or canvas. This method takes no argument.
Capabilities that turtle provides:
- You can set up the size of the screen
- You can change background color, fill the color in shapes
- You can change the shape, size, and color of your turtle
- You can even maintain the speed of the turtle.
- Moreover, you can play with angles.
- You can even insert images and shapes on the turtle screen
Here I am pasting a code for making a circular pattern along with output!
from turtle import *
speed(0)
bgcolor("black")
pencolor("white")
for i in range(6):
for colors in ("gold", "violet" , "purple", "navy" , "cyan", "turquoise"):
color(colors)
circle(100)
right(10)
hideturtle()
done()
Here I am pasting another code for making a flower along with output!
from turtle import *
speed(0)
bgcolor("black")
pencolor("blue")
fillcolor("red")
begin_fill()
while True:
forward(400)
left(170)
if abs(pos()) < 1:#pos() return a position of (x,y) coordinates
break #abs() function return the absolute value
end_fill()
ht()#to hide turtle
done()
Conclusion:
Taking everything into account I suggest you to must try this amazing module. If I talk about my personal experience, I am using this module since one week and in this span, I grasp a lot about this module. I constructed many shapes which include polygons, patterns, different flowers, and all alphabets of my name. Withal now I currently try to build a snake game using this turtle.
There is a lot of stuff prevailing on the internet including YouTube from where you can learn a lot about this module, you can also go through the documentation.
Happy Coding!!!