Catalogue of Posts

Tuesday, September 8, 2020

Basic Graphics #5 - Diagonal

Using  -  The Python Computer Language
                Turtle Graphics Library


5.  DIAGONAL


SAVE AS
 
>>> import turtle
>>>
>>> Pt = turtle.Turtle()
>>>
>>> for i in range(4):
        Pt.forward(200)
        Pt.right(90)
 
    
>>> Pt.right(45)
>>>
>>> Pt.goto(200, -200)
>>>
>>> turtle.done()
>>> 

SAVE
 

Pt.goto(x,y)
    Enables Pt to go to  (x , y)  on the grid





DEFINING A PROCESS
 
 
We can combine a number of actions into one process,
to make it easier to repeat them a number of times.
 
This is called    “Defining”    a process.
 
So, this combination of actions –
for i in range(4):
     Pt.forward(200)
     Pt.right(90)
 
Can be defined as,
def square():
            for i in range(4):
          Pt.forward(200)
          Pt.right(90)
 
To define any process –
def name():
     actions


Definitions are descriptions,
and need to be written into the program,
before they need to be used,
so that the computer understands them.
 
 
When needing to use a defined process,
type in –
 
name()
 

 
THE STAR
 

SAVE AS

>>> import turtle
>>>
>>> Pt = turtle.Turtle()
>>>
>>> def square():
       for i in range(4):
           Pt.forward(200)
           Pt.right(90)
 
         
>>> square()
>>> Pt.right(45)
>>> Pt.goto(200,-200)
>>> Pt.goto(0,0)
>>> Pt.right(45)
>>>
>>> square()
>>> Pt.right(45)
>>> Pt.goto(-200,-200)
>>> Pt.goto(0,0)
>>> Pt.right(45)
>>> 
>>> square()
>>> Pt.right(45)
>>> Pt.goto(-200,200)
>>> Pt.goto(0,0)
>>> Pt.right(45)
>>>
>>> square()
>>> Pt.right(45)
>>> Pt.goto(200,200)
>>> Pt.goto(0,0)
>>> Pt.right(45)
>>>
>>> turtle.done()

SAVE



(c) Katherine Stuart 2020
Dochas Books Film

No comments:

Post a Comment