turtletest.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <html>
  2. <!--
  3. Example of using the turtle module in skulpt.
  4. Author: Brad Miller
  5. Note: One important convention, since I plan to use
  6. multiple turtle canvases on a page I am passing the runit
  7. function a prefix to use in creating the id for the following:
  8. - textarea containing the code
  9. - pre tag for any printed output
  10. - canvas tag for the turtle
  11. I've enclosed the whole group of them in a div because I was thinking
  12. at one point about creating the pre tag and the canvas tag on the fly
  13. the more I think about it the more I wonder...
  14. -->
  15. <head>
  16. <script src="skulpt.min.js" type="text/javascript"></script>
  17. <script src="skulpt-stdlib.js" type="text/javascript"></script>
  18. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript" >
  19. </script>
  20. <script src="codemirror.js" type="application/javascript"></script>
  21. <script src="python.js"></script>
  22. <link rel="stylesheet" type="text/css" media="all" href="codemirror.css">
  23. <link rel="stylesheet" type="text/css" media="all" href="solarized.css">
  24. </head>
  25. <body>
  26. <h3>Try This</h3>
  27. <div id="example1">
  28. <form>
  29. <textarea edit_id="eta_5" id="example1_code" cols="60" rows="10">
  30. import turtle
  31. wn = turtle.Screen()
  32. t = turtle.Turtle()
  33. wn.setup(800,800)
  34. t.speed(10)
  35. t.tracer(10,30)
  36. print "initial heading = ", t.heading()
  37. for i in range(10,361,10):
  38. t.left(10)
  39. print i, t.heading()
  40. t.up()
  41. for i in range(4):
  42. t.forward(100)
  43. t.dot(20,'blue')
  44. t.right(90)
  45. t.goto(50,50)
  46. t.down()
  47. t.shape('square')
  48. t.color('green')
  49. for i in range(4):
  50. t.forward(100)
  51. t.right(90)
  52. t.up()
  53. t.goto(-300,200)
  54. t.down()
  55. t.fillcolor('red')
  56. t.begin_fill()
  57. for i in range(4):
  58. t.forward(150)
  59. t.right(90)
  60. t.end_fill()
  61. t.up();
  62. t.goto(10,-200)
  63. for i in range(0,400,10):
  64. t.goto(i,-200)
  65. t.dot(2,'violet')
  66. t.goto(400,250)
  67. t.down()
  68. t.setheading(t.towards(0,0))
  69. t.forward(200)
  70. print "turtle position is: ", t.position()
  71. t.fillcolor("black")
  72. t.write("Hello world",None,None,"bold 30px sans-serif")
  73. t.up()
  74. t.goto(300,-100)
  75. t.down()
  76. t.circle(20)
  77. t.up()
  78. t.goto(-200,-200)
  79. t.setheading(0)
  80. t.color('orange')
  81. t.width(8)
  82. for i in range(20):
  83. if i % 2 == 0:
  84. t.down()
  85. else:
  86. t.up()
  87. t.forward(10)
  88. wn.exitonclick()
  89. </textarea>
  90. <button onclick="runit('example1')" type="button">Run</button>
  91. </form>
  92. <div id="example1_canvas">
  93. </div>
  94. <pre id="example1_pre"></pre>
  95. </div>
  96. <script type="text/javascript">
  97. function outf(text) {
  98. var mypre = document.getElementById(Sk.pre);
  99. mypre.innerHTML = mypre.innerHTML + text;
  100. }
  101. window.Cm = CodeMirror.fromTextArea(document.getElementById("example1_code"), {
  102. parserfile: ["parsepython.js"],
  103. autofocus: true,
  104. theme: "solarized dark",
  105. lineNumbers: true,
  106. textWrapping: false,
  107. indentUnit: 4,
  108. height: "160px",
  109. fontSize: "9pt",
  110. autoMatchParens: true,
  111. parserConfig: { 'pythonVersion': 2, 'strictErrors': true }
  112. });
  113. function builtinRead(x)
  114. {
  115. if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
  116. throw "File not found: '" + x + "'";
  117. return Sk.builtinFiles["files"][x];
  118. }
  119. function runit(myDiv) {
  120. var prog = window.Cm.getValue();
  121. var mypre = document.getElementById(myDiv+"_pre");
  122. mypre.innerHTML = '';
  123. (Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'example1_canvas';
  124. Sk.pre = myDiv+"_pre";
  125. Sk.configure({ output:outf, read: builtinRead });
  126. Sk.misceval.asyncToPromise(function() {
  127. return Sk.importMainWithBody("<stdin>",false,prog,true);
  128. });
  129. }
  130. </script>
  131. </body>
  132. </html>