Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Ruby
Posted by: psichic
Added: Dec 2, 2020 5:54 AM
Modified: Dec 2, 2020 5:55 AM
Views: 6
  1. # https://docs.python.org/2/library/turtle.html
  2. # Important commands : fd lt lt rt
  3. ### DEMO 1 Assignments and Print Statement
  4. a=10
  5. b=23.5
  6. print a+b
  7.  
  8. ### DEMO 2 Compound Print Statement
  9. a=10
  10. b=23.5
  11. print "a+b=",a+b
  12.  
  13. ### DEMO 3 Calculating on the print line is not a good idea
  14. a=10
  15. b=23.5
  16. c=a+b
  17. print "a+b=",c
  18.  
  19. ### DEMO 4 Python as a calculator
  20. from math import *
  21. a=10
  22. b=23.5
  23. print sin(pi/2)
  24.  
  25. ### DEMO 5 Learning by drawing,
  26. ##5.1, Show heading
  27. from turtle import *
  28. clear()
  29. home()
  30. ##5.2 Show move, heading after one move
  31. from turtle import *
  32. clear()
  33. home()
  34. stride=100
  35. turn=90
  36. fd(stride);rt(turn)
  37. ##5.3 Set of statements
  38. from turtle import *
  39. clear()
  40. home()
  41. stride=100
  42. turn=90
  43. fd(stride);rt(turn)
  44. fd(stride/10);lt(turn)
  45. fd(stride/2);rt(turn)
  46. ### Demo 6 Draw a triangle: Repeat version
  47. from turtle import *
  48. clear()
  49. home()
  50. stride=100
  51. turn=120
  52. fd(stride);rt(120)
  53. fd(stride);rt(120)
  54. fd(stride);rt(120)
  55.  
  56. ### Demo 7 Draw a triangle: infinite loop version
  57. from turtle import *
  58. clear()
  59. home()
  60. stride=100
  61. turn=120
  62. steps=3
  63. nstep=0
  64. while nstep <= steps:
  65.   fd(stride);rt(120)
  66.  
  67. ### DEMO 8 Fixing the infinite loop
  68. from turtle import *
  69. clear()
  70. home()
  71. stride=100
  72. turn=120
  73. steps=3
  74. nstep=0
  75. while nstep <= steps:
  76.   fd(stride);rt(120)
  77.   nstep=nstep+1
  78.  
  79. ## 8.1 Print number of steps
  80. from turtle import *
  81. clear()
  82. home()
  83. stride=100
  84. turn=120
  85. steps=3
  86. nstep=0
  87. while nstep <= steps:
  88.   fd(stride);rt(120)
  89.   nstep=nstep+1
  90.   print "nstep=", nstep
  91.  
  92. ## 8.2 What do we do when we are outside the loop
  93. ## Shift tab deindents
  94. from turtle import *
  95. clear()
  96. home()
  97. stride=100
  98. turn=120
  99. steps=3
  100. nstep=0
  101. while nstep <= steps:
  102.   fd(stride);rt(120)
  103.   nstep=nstep+1
  104.   print nstep
  105. ht()
  106. print "we are done"
  107.  
  108. ### DEMO 9 turn=100, Guess what, change steps to 10, Q: Predict whether trajectory is closed
  109. ### and repeatative.
  110. from turtle import *
  111. clear()
  112. home()
  113. stride=100
  114. turn=100
  115. steps=3
  116. nstep=0
  117. while nstep <= steps:
  118.   fd(stride);rt(turn)
  119.   nstep=nstep+1
  120.   print nstep
  121. ht()
  122. print "we are done"
  123.  
  124. # Change steps to 30 and observe that the turtle eventually returns. Ask: What is the logic?
  125. Can you tell at what step the turtle would begin to retrace?
  126.  
  127. ###DEMO 10 Logic, Numbered vertices and coloring, stride -> 150
  128. ###18*(120-100)=360
  129. from turtle import *
  130. clear()
  131. home()
  132. stride=150
  133. turn=100
  134. steps=30
  135. nstep=0
  136. while nstep <= steps:
  137.   if nstep >= 0 and nstep <10:
  138.     color('red')
  139.   if nstep >= 10 and nstep <20:
  140.     color('blue')
  141.   elif nstep >= 20 and nstep <30:
  142.     color('green')
  143.   fd(stride);rt(turn);write(str(nstep+1), font=('Arial', 12, 'normal'))
  144.   nstep=nstep+1
  145.  
  146. ### DEMO 11 Turtle finds home, can also run it with turn=110
  147. from turtle import *
  148. clear()
  149. home()
  150. here=pos()
  151. stride=150
  152. turn=100
  153. steps=30
  154. nstep=0
  155. while nstep <= steps:
  156.   if nstep >= 0 and nstep <10:
  157.     color('red')
  158.   if nstep >= 10 and nstep <20:
  159.     color('blue')
  160.   elif nstep >= 20 and nstep <30:
  161.     color('green')
  162.   fd(stride);rt(turn);write(str(nstep+1), font=('Arial', 12, 'normal'))
  163.   nstep=nstep+1
  164.   there=pos()
  165.   if there==here:
  166.     break
  167. print "Turtle found home in ", steps, " steps"
  168.  
  169. ### DEMO 12 Random Walk!
  170. ### special import syntax and function assignment f()
  171. ### Change turn to 120 or 90 for constrained random walk, and do not modify strides.
  172. from turtle import *
  173. import random
  174. f=random.random
  175. clear()
  176. home()
  177. here=pos()
  178. stride=50
  179. turn=360
  180. steps=100
  181. nstep=0
  182. while nstep <= steps:
  183.   if nstep >= 0 and nstep <10:
  184.     color('red')
  185.   if nstep >= 10 and nstep <20:
  186.     color('blue')
  187.   elif nstep >= 20 and nstep <30:
  188.     color('green')
  189.   fd(stride*f());rt(turn*f())
  190.   nstep=nstep+1
  191.   there=pos()
  192.   if there==here:
  193.     break
  194. print "End of Walk reached"
  195. ## 12.1 Simpler variant using random color
  196. ## You can find length between initial and final point
  197. from turtle import *
  198. import random
  199. f=random.random
  200. fc=random.randint
  201. clear()
  202. home()
  203. here=pos()
  204. stride=50
  205. turn=90
  206. steps=100
  207. nstep=0
  208. COLORS=['red','green','blue']
  209. while nstep <= steps:
  210.   cc=COLORS[fc(0,2)]
  211.   color(cc)
  212.   fd(stride*f());rt(turn*f())
  213.   nstep=nstep+1
  214. print "End of Walk reached"
  215.  
  216.  
  217.  
  218.  
  219.  
  220.