Turtle module in Python can be used to create simple shapes and graphics. Turtle graphics is used to make different shapes and drawing curves. In this post, we will see some sample programs to draw different shapes using Turtle.
You need to import the turtle module to start using it. A turtle object can draw lines of various sizes using the in-built methods. The turtle lives in the graphics window and the location is denoted by x and y. Turtles also have orientation i.e angle w.r.t x-axis at which they head.
The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when the turtle used as a standalone tool for doing graphics.
Sample code to draw Square
Steps to create the program.
- Launch PyCharm IDE.
- Create a project and add a file. say turtleProgram.py
Add the below program code.
import turtle # main def main(): screen = turtle.Screen() t = turtle.Turtle() square(t, 100) screen.exitonclick() # square def square(t, size): t.pendown() # loop for _ in range(4): t.forward(size) t.right(90) t.penup() if __name__ == '__main__': main()
Run the program. Right-click on the program window and choose Run option.