Friday, 9 August 2024

Python turtle

 -----------------------------------

Python Turtle:

----------------------------------

-Turtle graphics is a popular way for introducing programming to kids.

-The "turtle" is an imaginary pen that is given drawing commands, such as go forward and turn right. On screen, the turtle is shaped like a triangle


Turtle Methods:

-------------------

Method     Description

----------        ----------------

1) Turtle     - Creates and returns a new turtle object

2) forward     - Moves the turtle forward

3) backward  -Moves the turle backward

4) right     -Turns the turtle clockwise

5) left    -Turns the turtle counter clockwise

6) up    -Picks up the turtles tail

7) down    -Puts down the turtles tail

8) color    -Changes the color of the turtle’s tail

9) fillcolor    -Changes the color of the turtle will use to fill a polygon

10) heading  -Returns the current heading

11) position  -Returns the current position

12) goto     -Move the turtle to position x,y

13) begin_fill-Remember the starting point for a filled polygon

14) end_fill   -Close the polygon and fill with the current fill color

15) dot     -Leave a dot at the current position

16) stamp     -Leaves an impression of a turtle shape at the current location

17) shape     -Should be ‘arrow’, ‘classic’, ‘turtle’, ‘circle’ or ‘square’

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Mouse Events:

----------------

1) onclick()

2) ondrag()

3) onrelease()


Keyboard Events:

---------------------

1) onkey()

2) listen()

~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 1:  draw a arrow (Method - I)

import turtle

turtle.forward(50)

turtle.backward(50)

~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 2:  draw a arrow (Another Method - II)

import turtle

t1=turtle.Turtle()

t1.forward(50)

t1.backward(50)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 3:  draw a arrow (Another Method - III)

from turtle import Turtle

t1=Turtle()

t1.setheading(270)  #setheading(angle)

t1.forward(50)

t1.backward(150)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 4:  set turtle color

import turtle

t=turtle.Turtle()

t.screen.bgcolor("black")

t.screen.title("turtle Color")

t.color("blue")

~~~~~~~~~~~~~~~~~~~~~~~

#Program 5:  set background image

import turtle

t=turtle.Turtle()

t.hideturtle()

t.screen.bgpic("one.gif")

~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 6:  Change shape size

import turtle

turtle.shape("turtle")

turtle.shapesize(30)

~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 7:  Write text to screen

import turtle

turtle.hideturtle()

turtle.write("Cool Python Codes",move=True,align="center",font=("Freestyle Script",50,"normal"))

~~~~~~~~~~~~~~~~~~~~~

#Program 8:  drawing a circle using onclick method(Mouse)

from turtle import Turtle

t=Turtle()

t.screen.bgcolor("black")

t.color("orange")


def circle(x,y):

    t.circle(60)

 

t.onclick(circle)

~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 9:  drawing a any shape using ondrag method(Mouse)

from turtle import Turtle

t=Turtle()

t.screen.bgcolor("black")

t.color("blue")

 

def goto(x,y):

    t.goto(x,y)

 

t.ondrag(goto)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 10:  drawing a square

import turtle

turtle.forward(50)

turtle.left(90)

turtle.forward(50)

turtle.left(90)

turtle.forward(50)

turtle.left(90)

turtle.forward(50)

turtle.left(90)

~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 11:  drawing a square using loop

import turtle

for i in range(4): 

    turtle.forward(50) 

    turtle.right(90)

turtle.done()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 12:  drawing a rectangle

import turtle

turtle.forward(100)

turtle.left(90)

turtle.forward(50)

turtle.left(90)

turtle.forward(100)

turtle.left(90)

turtle.forward(50)

turtle.left(90)

~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 13:  drawing a star

import turtle

for i in range(10): 

    turtle.forward(50) 

    turtle.right(144) 

turtle.done() 

~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 14:  drawing a spiral square

import turtle   #Outside_In 

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) 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 15:  drawing a spiral square

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) 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 16:  drawing a rainbow color

import turtle 

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

t = turtle.Pen() 

turtle.bgcolor('black') 

for x in range(50): 

    t.pencolor(colors[x%6]) 

    t.width(x/100 + 1) 

    t.forward(x) 

    t.left(59) 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 17:  Keyboard Events(move left, right, up and down)

from turtle import Turtle

t=Turtle()

def up():

    if not(t.heading() == 90):

        t.setheading(90)

        t.fd(50)

    else:

        t.fd(50)

    

def down():

    if not(t.heading() == 270):

        t.setheading(270)

        t.fd(50)

    else:

        t.fd(50)

    

def right():

    if not (t.heading() == 0):

        t.setheading(0)

        t.fd(50)

    else:

        t.fd(50)

    

def left():

    if not (t.heading() ==180):

        t.setheading(180)

        t.fd(50)

    else:

        t.fd(50)

 

def undo_button():

    t.undo()

        

def keyboard_commands():

    t.screen.onkey(up,"Up")

    t.screen.onkey(down,"Down")

    t.screen.onkey(right,"Right")

    t.screen.onkey(left,"Left")

    t.screen.onkey(undo_button,"End")

    t.screen.listen()

 

keyboard_commands()

t.screen.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

No comments:

Post a Comment