turtles.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. 'use strict';
  2. goog.provide('Blockly.Python.turtles');
  3. goog.require('Blockly.Python');
  4. Blockly.Python['turtle_create'] = function (block) {
  5. // TODO: Assemble Python into code variable.
  6. var code = 'import turtle \nturtle = turtle.Turtle()\n';
  7. return code;
  8. };
  9. Blockly.Python['turtle_color'] = function (block) {
  10. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  11. // TODO: Assemble Python into code variable.
  12. var code = 'turtle.color(' + color + ')\n';
  13. return code;
  14. };
  15. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['color'] = function (func, args, keywords, starargs, kwargs, node) {
  16. if (args.length < 1 || args.length > 2) {
  17. throw new Error("Incorrect number of arguments to turtle.color!");
  18. }
  19. return [block("turtle_color", func.lineno, {}, {
  20. "COLOR": this.convert(args[0])
  21. }, { "inline": "true" })];
  22. }
  23. Blockly.Python['turtle_forward'] = function (block) {
  24. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  25. var distance = Blockly.Python.valueToCode(block, 'DISTANCE', Blockly.Python.ORDER_ATOMIC);
  26. // TODO: Assemble Python into code variable.
  27. var code = 'turtle' + '.forward(' + distance + ')\n';
  28. return code;
  29. };
  30. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['forward'] = function (func, args, keywords, starargs, kwargs, node) {
  31. if (args.length != 1) {
  32. throw new Error("Incorrect number of arguments to turtle.forward!");
  33. }
  34. return [block("turtle_forward", func.lineno, {}, {
  35. "DISTANCE": this.convert(args[0]),
  36. }, { "inline": "true" })];
  37. }
  38. Blockly.Python['turtle_backward'] = function (block) {
  39. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  40. var distance = Blockly.Python.valueToCode(block, 'DISTANCE', Blockly.Python.ORDER_ATOMIC);
  41. // TODO: Assemble Python into code variable.
  42. var code = 'turtle.backward(' + distance + ')\n';
  43. return code;
  44. };
  45. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['backward'] = function (func, args, keywords, starargs, kwargs, node) {
  46. if (args.length != 1) {
  47. throw new Error("Incorrect number of arguments to turtle.backward!");
  48. }
  49. return [block("turtle_backward", func.lineno, {}, {
  50. "DISTANCE": this.convert(args[0]),
  51. }, { "inline": "true" })];
  52. }
  53. Blockly.Python['turtle_left'] = function (block) {
  54. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  55. var angle = Blockly.Python.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_ATOMIC);
  56. // TODO: Assemble Python into code variable.
  57. var code = 'turtle.left(' + angle + ')\n';
  58. return code;
  59. };
  60. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['left'] = function (func, args, keywords, starargs, kwargs, node) {
  61. if (args.length != 1) {
  62. throw new Error("Incorrect number of arguments to turtle.left!");
  63. }
  64. return [block("turtle_left", func.lineno, {}, {
  65. "ANGLE": this.convert(args[0]),
  66. //"TURTLE": this.convert(func.value)
  67. }, { "inline": "true" })];
  68. }
  69. Blockly.Python['turtle_right'] = function (block) {
  70. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  71. var angle = Blockly.Python.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_ATOMIC);
  72. // TODO: Assemble Python into code variable.
  73. var code = 'turtle.right(' + angle + ')\n';
  74. return code;
  75. };
  76. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['right'] = function (func, args, keywords, starargs, kwargs, node) {
  77. if (args.length != 1) {
  78. throw new Error("Incorrect number of arguments to turtle.right!");
  79. }
  80. return [block("turtle_right", func.lineno, {}, {
  81. "ANGLE": this.convert(args[0]),
  82. //"TURTLE": this.convert(func.value)
  83. }, { "inline": "true" })];
  84. }
  85. Blockly.Python['turtle_shape'] = function (block) {
  86. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  87. var dropdown_shape = block.getFieldValue('Shape');
  88. // TODO: Assemble Python into code variable.
  89. var code = 'turtle.shape(\"' + dropdown_shape + '\")\n';
  90. return code;
  91. };
  92. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['shape'] = function (func, args, keywords, starargs, kwargs, node) {
  93. if (args.length != 1) {
  94. throw new Error("Incorrect number of arguments to turtle.shape!");
  95. }
  96. var shape_supported = ["turtle", "circle", "classic", "square", "triangle"];
  97. var input_arg = args[0]["s"]["v"];
  98. if (!shape_supported.includes(input_arg)) {
  99. throw new Error("The shape is not supported!");
  100. }
  101. return [block("turtle_shape", func.lineno, { "Shape": input_arg }, {},
  102. { "inline": "true" })];
  103. }
  104. Blockly.Python['turtle_pen'] = function (block) {
  105. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  106. var pen = block.getFieldValue('Pen');
  107. // TODO: Assemble Python into code variable.
  108. var code = 'turtle.pen' + pen + '()\n';
  109. return code;
  110. };
  111. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['penup'] = function (func, args, keywords, starargs, kwargs, node) {
  112. if (args.length != 0) {
  113. throw new Error("Incorrect number of arguments to turtle.shape!");
  114. }
  115. return [block("turtle_pen", func.lineno, { "Pen": "up" }, {},
  116. { "inline": "true" })];
  117. }
  118. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['pendown'] = function (func, args, keywords, starargs, kwargs, node) {
  119. if (args.length != 0) {
  120. throw new Error("Incorrect number of arguments to turtle.shape!");
  121. }
  122. return [block("turtle_pen", func.lineno, { "Pen": "down" }, {},
  123. { "inline": "true" })];
  124. }
  125. Blockly.Python['turtle_circle'] = function (block) {
  126. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  127. var radius = Blockly.Python.valueToCode(block, 'RADIUS', Blockly.Python.ORDER_ATOMIC);
  128. // TODO: Assemble Python into code variable.
  129. var code = 'turtle.circle(' + radius + ')\n';
  130. return code;
  131. };
  132. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['circle'] = function (func, args, keywords, starargs, kwargs, node) {
  133. if (args.length != 1) {
  134. throw new Error("Incorrect number of arguments to turtle.circle!");
  135. }
  136. return [block("turtle_circle", func.lineno, {}, {
  137. "RADIUS": this.convert(args[0]),
  138. //"TURTLE": this.convert(func.value)
  139. }, { "inline": "true" })];
  140. }
  141. Blockly.Python['turtle_goto'] = function (block) {
  142. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  143. var x_coordinate = Blockly.Python.valueToCode(block, 'X', Blockly.Python.ORDER_ATOMIC);
  144. var y_coordinate = Blockly.Python.valueToCode(block, 'Y', Blockly.Python.ORDER_ATOMIC);
  145. // TODO: Assemble Python into code variable.
  146. var code = 'turtle.goto(' + x_coordinate + ',' + y_coordinate + ')\n';
  147. return code;
  148. };
  149. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['goto'] = function (func, args, keywords, starargs, kwargs, node) {
  150. if (args.length != 2) {
  151. throw new Error("Incorrect number of arguments to turtle.goto!");
  152. }
  153. return [block("turtle_goto", func.lineno, {}, {
  154. "X": this.convert(args[0]),
  155. "Y": this.convert(args[1]),
  156. //"TURTLE": this.convert(func.value)
  157. }, { "inline": "true" })];
  158. }
  159. Blockly.Python['turtle_pos'] = function (block) {
  160. var code = "turtle.pos()";
  161. // TODO: Change ORDER_NONE to the correct strength.
  162. return [code, Blockly.Python.ORDER_ATOMIC];
  163. };
  164. PythonToBlocks.KNOWN_MODULES['turtle'] = {
  165. "pos": ["turtle_pos"]
  166. }
  167. Blockly.Python['turtle_stamp'] = function (block) {
  168. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  169. // TODO: Assemble Python into code variable.
  170. var code = 'turtle.stamp()\n';
  171. return code;
  172. };
  173. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['stamp'] = function (func, args, keywords, starargs, kwargs, node) {
  174. if (args.length != 0) {
  175. throw new Error("Incorrect number of arguments to turtle.goto!");
  176. }
  177. return [block("turtle_stamp", func.lineno, {}, {
  178. //"TURTLE": this.convert(func.value)
  179. }, { "inline": "true" })];
  180. }
  181. Blockly.Python['turtle_begin_fill'] = function (block) {
  182. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  183. // TODO: Assemble Python into code variable.
  184. var code = 'turtle.begin_fill()\n';
  185. return code;
  186. };
  187. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['begin_fill'] = function (func, args, keywords, starargs, kwargs, node) {
  188. if (args.length != 0) {
  189. throw new Error("Incorrect number of arguments to turtle.begin_fill!");
  190. }
  191. return [block("turtle_begin_fill", func.lineno, {}, {
  192. //"TURTLE": this.convert(func.value)
  193. }, { "inline": "true" })];
  194. }
  195. Blockly.Python['turtle_end_fill'] = function (block) {
  196. var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  197. // TODO: Assemble Python into code variable.
  198. var code = 'turtle.end_fill()\n';
  199. return code;
  200. };
  201. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['end_fill'] = function (func, args, keywords, starargs, kwargs, node) {
  202. if (args.length != 0) {
  203. throw new Error("Incorrect number of arguments to turtle.end_fill!");
  204. }
  205. return [block("turtle_end_fill", func.lineno, {}, {
  206. //"TURTLE": this.convert(func.value)
  207. }, { "inline": "true" })];
  208. }
  209. Blockly.Python['turtle_speed'] = function(block) {
  210. //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  211. var speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC);
  212. // TODO: Assemble Python into code variable.
  213. var code = 'turtle.speed(' +speed+')\n';
  214. return code;
  215. };
  216. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['speed'] = function(func, args, keywords, starargs, kwargs, node) {
  217. if (args.length != 1) {
  218. throw new Error("Incorrect number of arguments to turtle.speed!");
  219. }
  220. return [block("turtle_speed", func.lineno, {}, {
  221. "SPEED": this.convert(args[0]),
  222. //"TURTLE": this.convert(func.value)
  223. }, {"inline": "true"})];
  224. }