Some Creative Program of Python by using Turtle

0

 

Some Creative Program of Python by using Turtle


1.Spirograph Program


Input program :


  1. import turtle
  2. import math

  3. # Set up turtle
  4. t = turtle.Turtle()
  5. t.speed(0)
  6. t.hideturtle()

  7. # Set up parameters
  8. R = 200
  9. r = 50
  10. d = 100

  11. # Draw spirograph
  12. for theta in range(0, 360*4, 5):
  13.     x = (R-r)*math.cos(theta) + d*math.cos(((R-r)/r)*theta)
  14.     y = (R-r)*math.sin(theta) - d*math.sin(((R-r)/r)*theta)
  15.     t.goto(x, y)

  16. # Close window on click
  17. turtle.exitonclick()


Output of Program :




2.Fractal Tree Program


Input of Program :


  1. import turtle

  2. # Set up turtle
  3. t = turtle.Turtle()
  4. t.speed(0)
  5. t.left(90)
  6. t.penup()
  7. t.goto(0,-250)
  8. t.pendown()

  9. # Draw tree
  10. def draw_tree(branch_len, pensize):
  11.     if branch_len > 10:
  12.         t.pensize(pensize)
  13.         t.forward(branch_len)
  14.         t.right(30)
  15.         draw_tree(branch_len*0.7, pensize*0.8)
  16.         t.left(60)
  17.         draw_tree(branch_len*0.7, pensize*0.8)
  18.         t.right(30)
  19.         t.backward(branch_len)

  20. draw_tree(100, 10)

  21. # Close window on click
  22. turtle.exitonclick()


Output Of Program :




3.Sierpinnski  Triangle Program 

Input of Program :


  1. import turtle

  2. # Set up turtle
  3. t = turtle.Turtle()
  4. t.speed(0)
  5. t.hideturtle()
  6. t.penup()
  7. t.goto(-250, -250)
  8. t.pendown()

  9. # Draw Sierpinski Triangle
  10. def sierpinski(size, depth):
  11.     if depth == 0:
  12.         for i in range(3):
  13.             t.forward(size)
  14.             t.left(120)
  15.     else:
  16.         sierpinski(size/2, depth-1)
  17.         t.forward(size/2)
  18.         sierpinski(size/2, depth-1)
  19.         t.backward(size/2)
  20.         t.left(60)
  21.         t.forward(size/2)
  22.         t.right(60)
  23.         sierpinski(size/2, depth-1)
  24.         t.left(60)
  25.         t.backward(size/2)
  26.         t.right(60)

  27. sierpinski(500, 5)

  28. # Close window on click
  29. turtle.exitonclick()


Output Of Program :



4.Koch Snowflake Program 


Input of  Program :


  1. import turtle
  2. Input of  Program :
  3. # Set up turtle
  4. t = turtle.Turtle()
  5. t.speed(0)
  6. t.hideturtle()

  7. # Draw Koch Snowflake
  8. def koch_snowflake(size, depth):
  9.     if depth == 0:
  10.         t.forward(size)
  11.     else:
  12.         koch_snowflake(size/3, depth-1)
  13.         t.left(60)
  14.         koch_snowflake(size/3, depth-1)
  15.         t.right(120)
  16.         koch_snowflake(size/3, depth-1)
  17.         t.left(60)
  18.         koch_snowflake(size/3, depth-1)

  19. for i in range(3):
  20.     koch_snowflake(300, 4)
  21.     t.right(120)

  22. # Close window on click
  23. turtle.exitonclick()

Output Of Program :









Post a Comment

0Comments
Post a Comment (0)