Voici quelques exemples de code en Python et en bloc côte-à-côte.
|
Blocs Kwyk |
Python |
Les boucles |
{"options": {"blockly": {"readOnly": true}, "blocks": {"base_simplerepeattimes": {"compound": {"blocks": {"base_numberinput": {"output": "Number", "colour": 260, "tooltip": "Fourni un nombre", "inputs": [{"type": "dummy_input", "fields": [{"name": "value", "default": 0.0, "type": "number_input"}], "align": "left"}]}, "base_repeattimes": {"previous_statement": true, "next_statement": true, "colour": 120, "tooltip": "Ex\u00e9cute une suite d'instructions un certain nombre de fois", "inputs": [{"type": "value_input", "fields": [{"text": "R\u00e9p\u00e9ter", "type": "text"}], "align": "left", "name": "ntimes", "check": "Number"}, {"type": "dummy_input", "fields": [{"text": "fois", "type": "text"}], "align": "left"}, {"type": "statement_input", "fields": [], "align": "left", "name": "code_repeat", "check": null}], "inline": true}}, "ast": {"type": "base_repeattimes", "ntimes": {"type": "base_numberinput", "alias": {"value": "ntimes"}}}}, "previous_statement": true, "next_statement": true, "colour": 120, "tooltip": "Ex\u00e9cute une suite d'instructions un certain nombre de fois", "inline": true}, "road_forward": {"previous_statement": true, "next_statement": true, "colour": 160, "tooltip": "Avancer d'une case tout droit", "inputs": [{"type": "dummy_input", "fields": [{"text": "Avancer de 1 case", "type": "text"}], "align": "left"}]}}}, "ast": [[{"type": "base_simplerepeattimes", "code_repeat": [{"type": "road_forward"}], "ntimes": 10.0}]]} |
for i in range(10):
avancer_de_1_case()
|
Les conditions |
{"options": {"blockly": {"readOnly": true}, "blocks": {"road_surroundingcaseroad": {"compound": {"blocks": {"road_surroundingcase": {"output": "Boolean", "colour": 210, "tooltip": "Test le type d'une case autour de la position courante", "inputs": [{"type": "dummy_input", "fields": [{"text": "la case", "type": "text"}, {"name": "dir", "values": [["en face", "front"], ["\u00e0 gauche", "left"], ["\u00e0 droite", "right"], ["derri\u00e8re", "back"]], "type": "dropdown"}, {"text": "est", "type": "text"}, {"name": "case_type", "values": [["une route", "road"], ["de l'herbe", "grass"]], "type": "dropdown"}], "align": "left"}], "inline": true}}, "ast": {"type": "road_surroundingcase", "case_type": "road"}}, "output": "Boolean", "colour": 210, "tooltip": "Test le type d'une case autour de la position courante est une route", "inline": true}, "base_if": {"previous_statement": true, "next_statement": true, "colour": 210, "tooltip": "Ex\u00e9cute une suite d'instructions si une condition est v\u00e9rifi\u00e9e", "inputs": [{"type": "value_input", "fields": [{"text": "Si", "type": "text"}], "align": "left", "name": "cond", "check": "Boolean"}, {"type": "statement_input", "fields": [], "align": "left", "name": "code_if", "check": null}]}, "road_turnright": {"previous_statement": true, "next_statement": true, "colour": 160, "tooltip": "Tourner \u00e0 droite", "inputs": [{"type": "dummy_input", "fields": [{"text": "Tourner \u00e0 droite \u21b7", "type": "text"}], "align": "left"}]}, "base_simplerepeattimes": {"compound": {"blocks": {"base_numberinput": {"output": "Number", "colour": 260, "tooltip": "Fourni un nombre", "inputs": [{"type": "dummy_input", "fields": [{"name": "value", "default": 0.0, "type": "number_input"}], "align": "left"}]}, "base_repeattimes": {"previous_statement": true, "next_statement": true, "colour": 120, "tooltip": "Ex\u00e9cute une suite d'instructions un certain nombre de fois", "inputs": [{"type": "value_input", "fields": [{"text": "R\u00e9p\u00e9ter", "type": "text"}], "align": "left", "name": "ntimes", "check": "Number"}, {"type": "dummy_input", "fields": [{"text": "fois", "type": "text"}], "align": "left"}, {"type": "statement_input", "fields": [], "align": "left", "name": "code_repeat", "check": null}], "inline": true}}, "ast": {"type": "base_repeattimes", "ntimes": {"type": "base_numberinput", "alias": {"value": "ntimes"}}}}, "previous_statement": true, "next_statement": true, "colour": 120, "tooltip": "Ex\u00e9cute une suite d'instructions un certain nombre de fois", "inline": true}, "road_forward": {"previous_statement": true, "next_statement": true, "colour": 160, "tooltip": "Avancer d'une case tout droit", "inputs": [{"type": "dummy_input", "fields": [{"text": "Avancer de 1 case", "type": "text"}], "align": "left"}]}}}, "ast": [[{"type": "base_simplerepeattimes", "code_repeat": [{"type": "road_forward"}, {"type": "base_if", "cond": {"type": "road_surroundingcaseroad", "dir": "right"}, "code_if": [{"type": "road_turnright"}]}], "ntimes": 20.0}]]} |
for i in range(20):
avancer_de_1_case()
if case_est_une_route("droite"):
tourner_a_droite()
|
Les variables |
{"options": {"blockly": {"readOnly": true}, "blocks": {"base_setvar": {"previous_statement": true, "next_statement": true, "colour": 330, "tooltip": "Affecter une valeur \u00e0 une variable", "inputs": [{"type": "value_input", "fields": [{"text": "mettre", "type": "text"}, {"name": "var_name", "default": "x", "type": "variable"}, {"text": "\u00e0", "type": "text"}], "align": "left", "name": "value", "check": null}]}, "base_numberinput": {"output": "Number", "colour": 260, "tooltip": "Fourni un nombre", "inputs": [{"type": "dummy_input", "fields": [{"name": "value", "default": 0.0, "type": "number_input"}], "align": "left"}]}}}, "ast": [[{"type": "base_setvar", "value": {"type": "base_numberinput", "value": 20.0}, "var_name": "essence"}]]} |
|
Les fonctions |
{"options": {"blockly": {"readOnly": true}, "blocks": {"base_varvalue": {"output": true, "colour": 330, "tooltip": "Fourni la valeur d'une variable", "inputs": [{"type": "dummy_input", "fields": [{"name": "var_name", "default": "x", "type": "variable"}], "align": "left"}]}, "DefForwardN": {"compound": {"blocks": {"GenericFunction": {"colour": 65, "inputs": [{"type": "value_input", "fields": [{"text": "Fonction \u00ab", "type": "text"}], "align": "left", "name": "fn_def", "check": null}, {"type": "dummy_input", "fields": [{"text": "\u00bb", "type": "text"}], "align": "left"}, {"type": "statement_input", "fields": [], "align": "left", "name": "code_fn", "check": null}], "inline": true, "is_function_def": true, "specific_instance_data": {"show_in_toolbox": true, "function_call_block": {}}}, "TitleDef_DefForwardN": {"output": true, "colour": 160, "inputs": [{"type": "dummy_input", "fields": [{"text": "Avancer de", "type": "text"}, {"name": "arg_0", "default": "N", "type": "variable"}, {"text": "cases", "type": "text"}], "align": "left"}]}}, "ast": {"type": "GenericFunction", "fn_def": {"type": "TitleDef_DefForwardN"}}}, "colour": 160, "inline": true, "is_function_def": true, "specific_instance_data": {"show_in_toolbox": true, "function_call_block": ["function_DefForwardN", {"previous_statement": true, "next_statement": true, "colour": 160, "inputs": [{"type": "value_input", "fields": [{"text": "Avancer de", "type": "text"}], "align": "left", "name": "arg_0", "check": null}, {"type": "dummy_input", "fields": [{"text": "cases", "type": "text"}], "align": "left"}], "inline": true}]}}, "base_repeattimes": {"previous_statement": true, "next_statement": true, "colour": 120, "tooltip": "Ex\u00e9cute une suite d'instructions un certain nombre de fois", "inputs": [{"type": "value_input", "fields": [{"text": "R\u00e9p\u00e9ter", "type": "text"}], "align": "left", "name": "ntimes", "check": "Number"}, {"type": "dummy_input", "fields": [{"text": "fois", "type": "text"}], "align": "left"}, {"type": "statement_input", "fields": [], "align": "left", "name": "code_repeat", "check": null}], "inline": true}, "road_forward": {"previous_statement": true, "next_statement": true, "colour": 160, "tooltip": "Avancer d'une case tout droit", "inputs": [{"type": "dummy_input", "fields": [{"text": "Avancer de 1 case", "type": "text"}], "align": "left"}]}}}, "ast": [[{"type": "DefForwardN", "code_fn": [{"type": "base_repeattimes", "ntimes": {"type": "base_varvalue", "var_name": "N"}, "code_repeat": [{"type": "road_forward"}]}], "arg_0": "N"}]]} |
def avancer_cases(N):
for i in range(N):
avancer_de_1_case()
|