Baabtra.com little coder chapter - 4

Preview:

Citation preview

Little Coder Program

by

Drawing with TurtlesChapter 4

Turtle in python

• A module in Python is a way of providing useful code to be used by another

program. We’ll learn more about modules later on.

• Python has a special module called turtle that we can use to learn how

computers draw pictures on a screen.

• In the world of Python, a turtle is a small, black arrow that moves slowly

around the screen

Turtle in real world Turtle in Python

Lets start drawing by turtle module

>>> import turtleWe are asking python to import a

module named ‘turtle’ so that we

can use the functions and variables

written inside it

>>> import turtle

>>> t = turtle.Pen()we need to create a

Canvas - a blank space to draw on,

like an artist’s canvas. To do so,

we call the function Pen from the

turtle module, which automatically

creates a canvas.

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(500)

The forward instruction tells the

turtle to move forwardby 50 pixels,.

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(500)

>>>t.left(90)

The left() will turn the turtle 90

degree

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>>t.forward(50)

Yaay !! You have successfully drawn a square !!

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.left(90)

>>>t.forward(50)

>>>t.reset()

Reset function will erase your drawing and

set the turtle to the initial position

>>>t.reset()

>>>t.right(120)

>>>t.forward(50)

You can use the right function similar

to left

>>>t.reset()

>>>t.right(120)

>>>t.forward(50)

>>>t.backward(100)

Backward function act just in the

opposite direction of forward

>>>t.reset()

>>>t.right(120)

>>>t.forward(50)

>>>t.backward(100)

>>>t.left(60)

>>>t.up()

>>> t.forward(50)

>>> t.down()

>>> t.forward(100)

We can use up to lift the pen off the

page (in other words, tell the turtle to

stop drawing), and down to start

drawing.

Exercise !

Exercise !

End of Chapter 4