Turtle graphics

Turtle graphics

Introduction

Turtle graphics is the graphical user interface python libraries. Turtle graphics is an awesome and simple way of introducing programming to the kids.

Turtle graphic can be used to make different shapes, figures, and pictures using the method python turtle on a virtual canvas. When a turtle program is executed it automatically creates a canvas or windows where the shape is drawn. It can be used as giving order to the robotic turtle that works as per the order.

Using turtle, you can make many moving shapes and objects on the screen. Turtle graphics was developed in 1960s. It is still in used to help beginners to understand the programming. Turtle is not only for kids but also for adults. You can make your own games using turtle library in python. You can further read the turtle documentation here.


How to use turtle library in python?

You can use it by importing the library.

from turtle import Turtle
Or 
from turtle import *

You can give a turtle shape for your pointer using

turtle.shape(“turtle”)

Examples of turtle graphics. You can create the following.

Creating a dashed line

Import turtle as t
Tin = t.Turtle()
For _ in range(15):
    Tin.forward(10)
    Tin.penup()
    Tin.forward(10)
    Tin.pendown()

output:

dashed line

Creating a square  

from turtle import Turtle
tim = Turtle()
for _ in range(4):
    tim.forward(100)
    tim.left(90)

output:

square

Creating a random walk

import turtle as t
import random

tim = t.Turtle()
colours = ["CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "LightSeaGreen", "wheat", "SlateGray", "SeaGreen"]
directions = [0, 90, 180, 270]
tim.pensize(10)
tim.speed("fastest")
for _ in range(200):
    tim.color(random.choice(colours))
    tim.forward(30)
    tim.setheading(random.choice(directions))

output:

turtle graphics