Basic with Turtle

  1. Create turtle window

import turtle

t=turtle.Turtle()

Output:

2. Change the turtle window background color

import turtle

t=turtle.Turtle()

wn=turtle.Screen()

wn.bgcolor(‘red’)

Output:

3. Change the color of the turtle

import turtle

t=turtle.Turtle()

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)

Output:

4.  Set the heading of the turtle

#a

import turtle
t=turtle.Turtle()

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.setheading(90)

Output

#b

import turtle
t=turtle.Turtle()

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.left(45)

Output:

#c

import turtle
t=turtle.Turtle()

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.left(135)

Output:

#d

import turtle
t=turtle.Turtle()

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.right(45)

Output:

5. Change the turtle shape

The valid turtle shapes are:

  • Arrow
  • Circle
  • Square
  • Triangle
  • Turtle
  • Classic

#a

import turtle
t=turtle.Turtle()

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.setheading(90)
t.shape(‘turtle’)

#b

import turtle
t=turtle.Turtle(‘turtle’)

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.setheading(90)
#t.shape(‘turtle’)

Output

#c

import turtle
t=turtle.Turtle(‘triangle’)

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.setheading(45)

#d

import turtle
t=turtle.Turtle(‘circle’)

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.setheading(45)

6. turtle shape size

import turtle
t=turtle.Turtle()

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.setheading(90)
t.shapesize(20)
t.shape(‘turtle’)

Output:

7. Go to specify position

To draw a line use the syntax below

t.fd(distance)

or

t.goto(x,y)

distance is the value of the length of the line.

t.goto(x,y) moves the turtle to an absolute position

Note:

The Cartesian system is the coordinate system for Turtle. So x and y represent values in the x and y axis respectively.

t.fd(distance)  moves the turtle by the specified distance.

#a.

import turtle
t=turtle.Turtle()
t.shape(‘turtle’)
t.shapesize(1)

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.setheading(0)
t.goto(100,0)

Output:

#b

import turtle
t=turtle.Turtle(‘turtle’)
t.shapesize(2)

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
t.setheading(0)
t.goto(100,0)

Output:

#c

import turtle
t=turtle.Turtle(‘turtle’)

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.color(“yellow”)
#t.goto(100,0)
t.shapesize(2)
t.setheading(45)
t.forward(100)

 

#8. Thickness of line

import turtle
t=turtle.Turtle(‘turtle’)

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.pensize(8)
t.shapesize(2)
t.color(“yellow”)
#t.goto(100,0)
t.setheading(45)
t.forward(100)

Output:

#9. Line with turtle turn

import turtle
t=turtle.Turtle(‘turtle’)

wn=turtle.Screen()

wn.bgcolor(‘red’)

t.pensize(8)
t.shapesize(2)
t.color(“yellow”)
#t.goto(100,0)
t.setheading(45)
t.forward(100)
t.right(90)

Output:

#10 Two lines

import turtle
t=turtle.Turtle(‘turtle’)
t.screen.bgcolor(‘red’)
t.pensize(8)
t.shapesize(2)
t.color(“yellow”)
#t.goto(100,0)
t.setheading(45)
t.forward(100)
t.right(90)
t.forward(100)
t.hideturtle()

Output: