Turtle programming in Python

Counters | Loops | Python Methods and Functions

"Turtle" — it's a drawing board-like feature of Python that allows us to command the turtle to draw all over the body! We can use functions like turtle.forward (…) and turtle.right (…) that can move the turtle. The following turtle methods are commonly used:

tr> td class = "amp-wp-inline-f74fb0de8d71e0cc53e95b25025d2853"> Color name >
Method Parameter Turtle () None Creates and returns a new tutrle object
forward () amount
backward () amount Moves the turtle backward by the specified amount
right () angle left () Turns the turtle counter clockwise
/ td> None Picks up the turttle's Pen pendown () None Puts down the turtle's Pen
up () None Picks up the turtle's >
down () Ndone class = "amp-wp-inline-0786f4623984c46e15421e6979be740d"> Puts down the turtle's Pen
color () Changes the color of the turtle's pen
fillcolor () Color name Changes the color of the turtle will use to fill a polygon
heading () None
position () Returns the current position
t goto () x, y Move the turtle to position x, y
begin_fill () None Remember the starting point for a filled polygon
end_fill () None dot () None7446 Leave the dot at the current position
stamp () None Leaves an impression of a turtle shape location
shape () shapename Should be 'arrow', 'classic', 'turtle' or 'circle td>

Drawing with turtle

To to use the methods and functionality of the turtle, we need to import the turtle. Turtle comes with a standard Python package and does not require external installation. The roadmap for executing the turtle program has 4 steps:

  1. Import the turtle module
  2. Create a turtle to control.
  3. Draw using turtle methods
  4. Run turtle.done ().

So, as stated above, before we can use the turtle, we need to import it. We import it as:

 from turtle import * # or import turtle 

After importing the turtle library and giving us all the turtle functionality, we need to create a new drawing board (window) and turtle. Let's call the window wn and the turtle — skk. So we code as:

 wn = turtle.Screen () wn.bgcolor ("light green") wn.title ("Turtle") skk = turtle.Turtle () 

Now that we have created the window and the turtle, we need to move the turtle. To move forward 100 pixels in the skk direction, we code:

 skk.forward (100) 

We move forward 100 pixels, Cool! Now we end the program with the done () function, and you're done!

 turtle.done () 

So, we've created a program that draws a line 100 pixels long. We can draw different shapes and fill different colors using turtle techniques. There are many functions and programs that need to be coded using the turtle library in python. Let's learn how to draw some of the basic shapes.

Shape 1: Square

# Python program to draw a square
# using turtle programming

import turtle 

skk = turtle.Turtle ()

 

for i in range ( 4 ):

skk.forward ( 50 )

  skk.right ( 90 )

 
turtle.done ()

Shape 2: Star

# Python star drawing program
# using turtle programming

import turtle 

 

star = turtle.Turtle ()

 

for i in range ( 50 ):

star.forward ( 50 )

  star.right ( 144 )

 
turtle.done ()

Shape 3: Hexagon

# Python hexagon drawing program
# using turtle programming

import turtle 

polygon = turtle.Turtle ()

 

num_sides = 6

side_length = 70

angle = 360.0 / num_sides 

 

for i in range (num_sides):

  polygon.forward (side_length)

  polygon.right (angle)

 
turtle.done ()

Visit pythonturtle.org, to get a taste of Turtle without python preinstalled. The shell in PythonTurtle is a complete Python shell, and you can do almost anything you can with it using the standard Python shell. You can create loops, define functions, create classes, etc.
You can access these codes for great turtle programs here

Some amazing turtle programs

1. Spiral square from outside to inside and inside out

# Python drawing program
# Spiral area outside and inside
# using turtle programming

import turtle  # Outside

wn = turtle.Screen ()

wn.bgcolor ( "light green" )

wn.title ( "Turtle" )

skk = turtle.Turtle ()

skk.color ( " blue " )

  

def sqrfunc (size):

for i in range ( 4 ):

skk.fd (size)

skk.left ( 90 )

size = size - 5

 

sqrfunc ( 146 )

sqrfunc ( 126 )

sqrfunc ( 106 )

sqrfunc ( 86 )

sqrfunc ( 66 )

sqrfunc ( 46 )

sqrfunc ( 26 )


import turtle  # Inside out

wn = turtle.Screen ()

wn.bgcolor ( "light green" )

skk = turtle.Turtle ()

skk.color ( "blue" )

 

def sqrfunc (size):

for i in range ( 4 ):

skk.fd (size)

skk.left ( 90 )

size = size + 5

  

sqrfunc ( 6 )

sqrfunc ( 26 )

sqrfunc ( 46 )

sqrfunc ( 66 )

sqrfunc ( 86 )

sqrfunc ( 106 )

sqrfunc ( 126 )

sqrfunc ( 146 )

Output:

2. User input template

# Python program for custom input template
# usage programming turtle

import turtle  # Outside

import turtle

import time

import random

 

print ( "This program draws shapes based on the number you enter in a uniform pattern." )

num_str = in put ( "Enter the side number of the shape you want to draw:" )

if num_str.isdigit ():

squares = int (num_str)

 

angle = 180 - 180 * (squares - 2 ) / squares

 
turtle.up

 

x = 0  

y = 0

turtle.setpos (x, y)

 

 

numshapes = 8

for x in range (numshapes):

turtle. color (random.random (), random.random (), random.random ())

x + = 5

  y + = 5

turtle.forward (x)

turtle.left (y)

for i in range (squares):

turtle. begin_fill ()

turtle.down ()

turtle.forward ( 40 )

turtle.left (angle)

turtle. forward ( 40 )

print (turtle.pos ())

turtle.up ()

turtle .end_fill ()

 

time.sleep ( 11 )

turtle.bye ()

3. Spiral spiral model

Output:

4. Rainbow Benzene

# Python drawing program
# Spiral spiral pattern
# using turtle programming

 

import turtle

loadWindow = turtle.Screen ()

turtle.speed ( 2 )

 

for i in range ( 100 ):

  turtle.circle ( 5 * i)

turtle.circle ( - 5 * i)

turtle.left (i)

 
turtle.exitonclick ()

# Python drawing program
# Rainbow Benzene
# using turtle programming

import turtle

colors = [ 'red' , ' purple' , ' blue' , 'green' , 'orange' , ' yellow' ]

t = turtle.Pen ()

turtle.bgcolor ( 'black' )

for x in range ( 360 ):

t.pencolor (colors [x % 6 ])

  t .width (x / 100 + 1 )

t.forward (x)

t.left ( 59 )

Output:

Trees using turtle programming


References :

This article is courtesy of Amartya Ranjan Saikia . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please write in comments if you find anything wrong or if you'd like to share more information on the topic discussed above.





Tutorials