Learn the basics of python turtle graphics!
Learning about python using the turtle library is a great start towards game building and implementing logic and ideas over the screen.
This article is focusing on the basics of turtle use and you will learn how to interact with this feature of python.
What is a python turtle?
Turtle is a python package or can say module which is used to create graphics, designs, and game building and it provides a virtual canvas. For any new bee in the programming world, if wants to start with python, I recommend that after learning the basics of python must learn using a turtle library as it will help you to understand the concepts of objects, classes, and onscreen changes. Turtle starting at (0,0) x-y plane.
What are modules?
Python modules are files containing python code which includes definitions, functions, classes and statements etc.
Do we need to install it?
So, the answer is No! This library of python is pre-installed to make use of this feature we need to import turtle.
Note: Here *(asterisk) import everything from the turtle library.
It provides onscreen pen which enables us to draw according to us on a canvas. Here the question arises from where we get the screen?
The screen or window functionality is also coded inside the turtle library while importing turtle it also gets imported.
So basically, we need to create an object for the window as well.
We can also set the height and width of the window with the functions window.setup(width =X, height=Y).
There are different - different ways of importing the module. For better understanding:
Note: Turtle() and Screen() are called methods.
There are different methods coded in the turtle library to make it moveable, coloured, resizing. Likewise screen has its properties. For the chart of methods and functions, look to the turtle documentation.
turtle — Turtle graphics — Python 3.11.2 documentation
Let us jump to the answer of - how to use the turtle after creating an object?
Turtle can move in forward and backward directions in paces and can set their face heading by using setheading (In input it takes angle example 90, 180, 270 360 degree and any angle.) method.
Let’s code: Output:
Attributes of the turtle:
The turtle has three value attributes:
Location
Orientation (or direction)
Pen
Where each has its attributes.
A pen has attributes like color, width, up and down (on/off state). The turtle moves with commands that are relatives to its own positions such as “move forward 50 spaces”, “turn right 90 degrees”, “change pen color to red” and many more.
Fact: Snake game, where the snake is a turtle which moves around and food is also a turtle which randomly change its position in certain amount of time. Try to build the snake game by yourself. Ping-pong game can be build using it.
Conclusion:
In this tutorial, we have learned the basic flow and use of the turtle library such as how to import it and make it functional.
We have provided a window or can say a plane where a turtle is at (0,0) saddle point.
Turtle has its attributes and each of them has its functionalities. There are several methods and inputs.
There are lots of things to explore in this library. where you can put up your ideas into designs and actions on screen.
Hope you get the idea of using turtle and eagerly wish to build your own project from scratch.
Share your thoughts and project ideas. We will start a fruitful discussion for sure.
Thank you!
Subscribe to my newsletter
Read articles from Yukti Mahoviya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by