Python Turtle Graphics
Python Turtle Graphics
The Turtle module in Python can be used to create simple shapes and graphics. Turtle graphics are used to make different shapes and draw curves. In this post, we will see some sample programs for drawing different shapes using Turtle.
Getting Started
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 its location is denoted by x and y. Turtles also have orientation, i.e., the angle w.r.t. the x-axis at which they head.
import turtle
The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when the turtle is 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
Code Listing
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.
Program Output
—
Python Tutorials
Python Tutorial on this website can be found at:
Official Website
More Information on Python: