/** * Turtles! */ //Blockly.HSV_SATURATION = 0.523; //Blockly.HSV_VALUE = 0.847; var Turtle_HUE = 237; Blockly.Blocks['turtle_create'] = { init: function() { this.appendDummyInput() .appendField("create new turtle"); this.setPreviousStatement(true); this.setNextStatement(true); this.setOutput(false); this.setInputsInline(true); this.setColour(Turtle_HUE); this.setTooltip(''); this.setHelpUrl(''); } }; Blockly.Python['turtle_create'] = function(block) { // TODO: Assemble Python into code variable. var code = 'import turtle \nturtle = turtle.Turtle()\n'; return code; }; Blockly.Blocks['turtle_color'] = { init: function() { this.appendValueInput("COLOR") .setCheck(null) .appendField("make turtle color"); this.setPreviousStatement(true); this.setNextStatement(true); this.setOutput(false); this.setInputsInline(true); this.setColour(Turtle_HUE); this.setTooltip(''); this.setHelpUrl(''); } }; 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.Blocks['turtle_forward'] = { init: function() { this.appendValueInput("DISTANCE") .setCheck(null) .appendField("make turtle move forward by"); this.setPreviousStatement(true); this.setNextStatement(true); this.setInputsInline(true); this.setOutput(false); this.setColour(Turtle_HUE); this.setTooltip(''); this.setHelpUrl(''); } }; 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.Blocks['turtle_backward'] = { init: function() { this.appendValueInput("DISTANCE") .setCheck(null) .appendField("make turtle move backward by"); this.setPreviousStatement(true); this.setInputsInline(true); this.setNextStatement(true); this.setOutput(false); this.setColour(Turtle_HUE); this.setTooltip(''); this.setHelpUrl(''); } }; 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.Blocks['turtle_left'] = { init: function() { this.appendValueInput("ANGLE") .setCheck(null) .appendField("make turtle turn left by"); this.appendDummyInput("") .appendField("degree"); this.setPreviousStatement(true); this.setNextStatement(true); this.setInputsInline(true); this.setOutput(false); this.setColour(Turtle_HUE); this.setTooltip(''); this.setHelpUrl(''); } }; 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.Blocks['turtle_right'] = { init: function() { this.appendValueInput("ANGLE") .setCheck(null) .appendField("make turtle turn right by"); this.appendDummyInput("") .appendField("degree"); this.setPreviousStatement(true); this.setNextStatement(true); this.setInputsInline(true); this.setOutput(false); this.setColour(Turtle_HUE); this.setTooltip(''); this.setHelpUrl(''); } }; 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.Blocks['turtle_shape'] = { init: function() { this.appendDummyInput() .appendField("make turtle shape") .appendField(new Blockly.FieldDropdown([["turtle","turtle"], ["circle","circle"], ["classic","classic"], ["square","square"], ["triangle","triangle"]]), "Shape"); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Turtle_HUE); this.setTooltip(""); this.setHelpUrl(""); } }; 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.Blocks['turtle_pen'] = { init: function() { this.appendDummyInput() .appendField("make turtle pen") .appendField(new Blockly.FieldDropdown([["up","up"], ["down","down"]]), "Pen"); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Turtle_HUE); this.setTooltip(""); this.setHelpUrl(""); } }; 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.Blocks['turtle_circle'] = { init: function() { this.appendValueInput("RADIUS") .setCheck("Number") .appendField("make turtle circle with radius:"); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Turtle_HUE); this.setTooltip(""); this.setHelpUrl(""); } }; 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.Blocks['turtle_goto'] = { init: function() { this.appendValueInput("X") .setCheck("Number") .appendField("make turtle goto location X:"); this.appendValueInput("Y") .setCheck("Number") .appendField(",Y:"); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Turtle_HUE); this.setTooltip(""); this.setHelpUrl(""); } }; 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.Blocks['turtle_pos'] = { init: function() { this.appendDummyInput() .appendField("pos"); this.setOutput(true, 'Turtle_Position'); this.setColour(Turtle_HUE); this.setTooltip('Turtle position'); this.setHelpUrl(''); } }; 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"] } /*PythonToBlocks.KNOWN_ATTR_FUNCTIONS['pos'] = function(func, args, keywords, starargs, kwargs, node) { if (args.length != 0) { throw new Error("Incorrect number of arguments to turtle.pos!"); } return [block("turtle_pos", {}, {}, { }, {"inline": "true"})]; }*/ Blockly.Blocks['turtle_stamp'] = { init: function() { this.appendDummyInput() .appendField("make turtle stamp"); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Turtle_HUE); this.setTooltip(""); this.setHelpUrl(""); } }; 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.Blocks['turtle_begin_fill'] = { init: function() { this.appendDummyInput() .appendField("make turtle begin fill"); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Turtle_HUE); this.setTooltip(""); this.setHelpUrl(""); } }; 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.Blocks['turtle_end_fill'] = { init: function() { this.appendDummyInput() .appendField("make turtle end fill"); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Turtle_HUE); this.setTooltip(""); this.setHelpUrl(""); } }; 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.Blocks['turtle_speed'] = { init: function() { this.appendValueInput("SPEED") .setCheck("Number") .appendField("make turtle speed"); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Turtle_HUE); this.setTooltip(""); this.setHelpUrl(""); } }; 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"})]; }