123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- 'use strict';
- goog.provide('Blockly.Python.turtles');
- goog.require('Blockly.Python');
- Blockly.Python['turtle_create'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'import turtle \nturtle = turtle.Turtle()\n';
- return code;
- };
- Blockly.Python['turtle_color'] = function (block) {
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.color(' + color + ')\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['color'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length < 1 || args.length > 2) {
- throw new Error("Incorrect number of arguments to turtle.color!");
- }
- return [block("turtle_color", func.lineno, {}, {
- "COLOR": this.convert(args[0])
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_forward'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var distance = Blockly.Python.valueToCode(block, 'DISTANCE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle' + '.forward(' + distance + ')\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['forward'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 1) {
- throw new Error("Incorrect number of arguments to turtle.forward!");
- }
- return [block("turtle_forward", func.lineno, {}, {
- "DISTANCE": this.convert(args[0]),
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_backward'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var distance = Blockly.Python.valueToCode(block, 'DISTANCE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.backward(' + distance + ')\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['backward'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 1) {
- throw new Error("Incorrect number of arguments to turtle.backward!");
- }
- return [block("turtle_backward", func.lineno, {}, {
- "DISTANCE": this.convert(args[0]),
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_left'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var angle = Blockly.Python.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.left(' + angle + ')\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['left'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 1) {
- throw new Error("Incorrect number of arguments to turtle.left!");
- }
- return [block("turtle_left", func.lineno, {}, {
- "ANGLE": this.convert(args[0]),
- //"TURTLE": this.convert(func.value)
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_right'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var angle = Blockly.Python.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.right(' + angle + ')\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['right'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 1) {
- throw new Error("Incorrect number of arguments to turtle.right!");
- }
- return [block("turtle_right", func.lineno, {}, {
- "ANGLE": this.convert(args[0]),
- //"TURTLE": this.convert(func.value)
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_shape'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var dropdown_shape = block.getFieldValue('Shape');
- // TODO: Assemble Python into code variable.
- var code = 'turtle.shape(\"' + dropdown_shape + '\")\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['shape'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 1) {
- throw new Error("Incorrect number of arguments to turtle.shape!");
- }
- var shape_supported = ["turtle", "circle", "classic", "square", "triangle"];
- var input_arg = args[0]["s"]["v"];
- if (!shape_supported.includes(input_arg)) {
- throw new Error("The shape is not supported!");
- }
- return [block("turtle_shape", func.lineno, { "Shape": input_arg }, {},
- { "inline": "true" })];
- }
- Blockly.Python['turtle_pen'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var pen = block.getFieldValue('Pen');
- // TODO: Assemble Python into code variable.
- var code = 'turtle.pen' + pen + '()\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['penup'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 0) {
- throw new Error("Incorrect number of arguments to turtle.shape!");
- }
- return [block("turtle_pen", func.lineno, { "Pen": "up" }, {},
- { "inline": "true" })];
- }
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['pendown'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 0) {
- throw new Error("Incorrect number of arguments to turtle.shape!");
- }
- return [block("turtle_pen", func.lineno, { "Pen": "down" }, {},
- { "inline": "true" })];
- }
- Blockly.Python['turtle_circle'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var radius = Blockly.Python.valueToCode(block, 'RADIUS', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.circle(' + radius + ')\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['circle'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 1) {
- throw new Error("Incorrect number of arguments to turtle.circle!");
- }
- return [block("turtle_circle", func.lineno, {}, {
- "RADIUS": this.convert(args[0]),
- //"TURTLE": this.convert(func.value)
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_goto'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var x_coordinate = Blockly.Python.valueToCode(block, 'X', Blockly.Python.ORDER_ATOMIC);
- var y_coordinate = Blockly.Python.valueToCode(block, 'Y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.goto(' + x_coordinate + ',' + y_coordinate + ')\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['goto'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 2) {
- throw new Error("Incorrect number of arguments to turtle.goto!");
- }
- return [block("turtle_goto", func.lineno, {}, {
- "X": this.convert(args[0]),
- "Y": this.convert(args[1]),
- //"TURTLE": this.convert(func.value)
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_pos'] = function (block) {
- var code = "turtle.pos()";
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- PythonToBlocks.KNOWN_MODULES['turtle'] = {
- "pos": ["turtle_pos"]
- }
- Blockly.Python['turtle_stamp'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.stamp()\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['stamp'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 0) {
- throw new Error("Incorrect number of arguments to turtle.goto!");
- }
- return [block("turtle_stamp", func.lineno, {}, {
- //"TURTLE": this.convert(func.value)
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_begin_fill'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.begin_fill()\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['begin_fill'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 0) {
- throw new Error("Incorrect number of arguments to turtle.begin_fill!");
- }
- return [block("turtle_begin_fill", func.lineno, {}, {
- //"TURTLE": this.convert(func.value)
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_end_fill'] = function (block) {
- var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.end_fill()\n';
- return code;
- };
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['end_fill'] = function (func, args, keywords, starargs, kwargs, node) {
- if (args.length != 0) {
- throw new Error("Incorrect number of arguments to turtle.end_fill!");
- }
- return [block("turtle_end_fill", func.lineno, {}, {
- //"TURTLE": this.convert(func.value)
- }, { "inline": "true" })];
- }
- Blockly.Python['turtle_speed'] = function(block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.speed(' +speed+')\n';
- return code;
- };
-
- PythonToBlocks.KNOWN_ATTR_FUNCTIONS['speed'] = function(func, args, keywords, starargs, kwargs, node) {
- if (args.length != 1) {
- throw new Error("Incorrect number of arguments to turtle.speed!");
- }
-
- return [block("turtle_speed", func.lineno, {}, {
- "SPEED": this.convert(args[0]),
- //"TURTLE": this.convert(func.value)
- }, {"inline": "true"})];
- }
|