Skip to content

Commit ebfe6c7

Browse files
committed
-
1 parent 7fe4fe0 commit ebfe6c7

5 files changed

Lines changed: 303 additions & 26 deletions

File tree

coderbot/.DS_Store

6 KB
Binary file not shown.

static/js/blocks.js

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/**
2+
* Blockly Apps: CoderBot Blocks
3+
*
4+
* Copyright 2012 Google Inc.
5+
* https://blockly.googlecode.com/
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
/**
21+
* @fileoverview Blocks for Blockly's CoderBot application.
22+
* @author fraser@google.com (Neil Fraser)
23+
*/
24+
'use strict';
25+
26+
// Extensions to Blockly's language and JavaScript generator.
27+
28+
Blockly.Blocks['coderbot_moveForward'] = {
29+
// Block for moving forward.
30+
init: function() {
31+
this.setHelpUrl('http://code.google.com/p/blockly/wiki/Move');
32+
this.setColour(290);
33+
this.appendDummyInput()
34+
.appendField('moveForward');
35+
this.setPreviousStatement(true);
36+
this.setNextStatement(true);
37+
this.setTooltip('CoderBot_moveForwardTooltip');
38+
}
39+
};
40+
41+
Blockly.JavaScript['coderbot_moveForward'] = function(block) {
42+
// Generate JavaScript for moving forward.
43+
return 'CoderBot.forward(2);\n';
44+
};
45+
46+
Blockly.Blocks['coderbot_moveBackward'] = {
47+
// Block for moving forward.
48+
init: function() {
49+
this.setHelpUrl('http://code.google.com/p/blockly/wiki/Move');
50+
this.setColour(290);
51+
this.appendDummyInput()
52+
.appendField('moveBackward');
53+
this.setPreviousStatement(true);
54+
this.setNextStatement(true);
55+
this.setTooltip('CoderBot_moveBackwardTooltip');
56+
}
57+
};
58+
59+
Blockly.JavaScript['coderbot_moveBackward'] = function(block) {
60+
// Generate JavaScript for moving forward.
61+
return 'CoderBot.backward(2);\n';
62+
};
63+
64+
Blockly.Blocks['coderbot_turnLeft'] = {
65+
// Block for turning left or right.
66+
init: function() {
67+
this.setHelpUrl('http://code.google.com/p/blockly/wiki/Turn');
68+
this.setColour(290);
69+
this.appendDummyInput()
70+
.appendField("turnLeft");
71+
this.setPreviousStatement(true);
72+
this.setNextStatement(true);
73+
this.setTooltip(('CoderBot_turnTooltip'));
74+
}
75+
};
76+
77+
Blockly.JavaScript['coderbot_turnRight'] = function(block) {
78+
// Generate JavaScript for turning left or right.
79+
var dir = block.getFieldValue('DIR');
80+
return 'CoderBot.right(2);\n';
81+
};
82+
83+
Blockly.Blocks['coderbot_turnRight'] = {
84+
// Block for turning left or right.
85+
init: function() {
86+
this.setHelpUrl('http://code.google.com/p/blockly/wiki/Turn');
87+
this.setColour(290);
88+
this.appendDummyInput()
89+
.appendField("turnRight");
90+
this.setPreviousStatement(true);
91+
this.setNextStatement(true);
92+
this.setTooltip(('CoderBot_turnTooltip'));
93+
}
94+
};
95+
96+
Blockly.JavaScript['coderbot_turnRight'] = function(block) {
97+
// Generate JavaScript for turning left or right.
98+
var dir = block.getFieldValue('DIR');
99+
return 'CoderBot.right(2);\n';
100+
};
101+
102+
103+
Blockly.Blocks['coderbot_if'] = {
104+
// Block for 'if' conditional if there is a path.
105+
init: function() {
106+
var DIRECTIONS =
107+
[['CoderBot_pathAhead', 'isPathForward'],
108+
['CoderBot_pathLeft', 'isPathLeft'],
109+
['CoderBot_pathRight', 'isPathRight']];
110+
this.setColour(210);
111+
this.appendDummyInput()
112+
.appendField(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
113+
this.appendStatementInput('DO')
114+
.appendField('CoderBot_doCode');
115+
this.setTooltip('CoderBot_ifTooltip');
116+
this.setPreviousStatement(true);
117+
this.setNextStatement(true);
118+
}
119+
};
120+
121+
122+
Blockly.JavaScript['coderbot_if'] = function(block) {
123+
// Generate JavaScript for 'if' conditional if there is a path.
124+
var argument = 'CoderBot.' + block.getFieldValue('DIR') +
125+
'(\'block_id_' + block.id + '\')';
126+
var branch = Blockly.JavaScript.statementToCode(block, 'DO');
127+
var code = 'if (' + argument + ') {\n' + branch + '}\n';
128+
return code;
129+
};
130+
131+
Blockly.Blocks['coderbot_ifElse'] = {
132+
// Block for 'if/else' conditional if there is a path.
133+
init: function() {
134+
var DIRECTIONS =
135+
[[('CoderBot_pathAhead'), 'isPathForward'],
136+
[('CoderBot_pathLeft'), 'isPathLeft'],
137+
[('CoderBot_pathRight'), 'isPathRight']];
138+
this.setColour(210);
139+
this.appendDummyInput()
140+
.appendField(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
141+
this.appendStatementInput('DO')
142+
.appendField(('CoderBot_doCode'));
143+
this.appendStatementInput('ELSE')
144+
.appendField(('CoderBot_elseCode'));
145+
this.setTooltip(('CoderBot_ifelseTooltip'));
146+
this.setPreviousStatement(true);
147+
this.setNextStatement(true);
148+
}
149+
};
150+
151+
Blockly.JavaScript['coderbot_ifElse'] = function(block) {
152+
// Generate JavaScript for 'if/else' conditional if there is a path.
153+
var argument = 'CoderBot.' + block.getFieldValue('DIR') +
154+
'(\'block_id_' + block.id + '\')';
155+
var branch0 = Blockly.JavaScript.statementToCode(block, 'DO');
156+
var branch1 = Blockly.JavaScript.statementToCode(block, 'ELSE');
157+
var code = 'if (' + argument + ') {\n' + branch0 +
158+
'} else {\n' + branch1 + '}\n';
159+
return code;
160+
};
161+
162+
Blockly.Blocks['coderbot_forever'] = {
163+
// Do forever loop.
164+
init: function() {
165+
this.setHelpUrl('http://code.google.com/p/blockly/wiki/Repeat');
166+
this.setColour(120);
167+
this.appendDummyInput()
168+
.appendField(('CoderBot_repeatUntil'))
169+
.appendField(new Blockly.FieldImage(CoderBot.SKIN.marker, 12, 16));
170+
this.appendStatementInput('DO')
171+
.appendField(('CoderBot_doCode'));
172+
this.setPreviousStatement(true);
173+
this.setTooltip(('CoderBot_whileTooltip'));
174+
}
175+
};
176+
177+
Blockly.JavaScript['coderbot_forever'] = function(block) {
178+
// Generate JavaScript for do forever loop.
179+
var branch = Blockly.JavaScript.statementToCode(block, 'DO');
180+
if (Blockly.JavaScript.INFINITE_LOOP_TRAP) {
181+
branch = Blockly.JavaScript.INFINITE_LOOP_TRAP.replace(/%1/g,
182+
'\'block_id_' + block.id + '\'') + branch;
183+
}
184+
return 'while (true) {\n' + branch + '}\n';
185+
};

static/js/coderbot.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
1-
function CoderBot() {
2-
this.url = "/bot";
3-
}
1+
var CoderBot {}
42

53
CoderBot.prototype.command = function(cmd, param) {
6-
data = {'cmd': cmd,
4+
var url = "/bot";
5+
var data = {'cmd': cmd,
76
'param': param};
87
$.ajax({url: this.url, data: data, type: "GET"});
98
}
109

1110
CoderBot.prototype.forward = function(t) {
12-
this.command('forward', t);
11+
CoderBot.command('forward', t);
1312
}
1413

1514
CoderBot.prototype.left = function(t) {
16-
this.command('left', t);
15+
CoderBot.command('left', t);
1716
}
1817

1918
CoderBot.prototype.right = function(t) {
20-
this.command('right', t);
19+
CoderBot.command('right', t);
2120
}
2221

2322
CoderBot.prototype.backward = function(t) {
24-
this.command('backward', t);
23+
CoderBot.command('backward', t);
2524
}
26-
27-
var bot = new CoderBot();
28-
29-
$(document).ready(function() {
30-
31-
$('#b_forward').on("click", function (){
32-
bot.forward(1);
33-
});
34-
$('#b_left').on("click", function (){
35-
bot.left(1);
36-
});
37-
$('#b_right').on("click", function (){
38-
bot.right(1);
39-
});
40-
$('#b_backward').on("click", function (){
41-
bot.backward(1);
42-
});
43-
});

static/js/control.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
$(document).ready(function() {
2+
3+
$('#b_forward').on("click", function (){
4+
CoderBot.forward(1);
5+
});
6+
$('#b_left').on("click", function (){
7+
CoderBot.left(1);
8+
});
9+
$('#b_right').on("click", function (){
10+
CoderBot.right(1);
11+
});
12+
$('#b_backward').on("click", function (){
13+
CoderBot.backward(1);
14+
});
15+
});

templates/blockly.html

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Blockly Demo: Fixed Blockly</title>
6+
<script type="text/javascript" src="../../blockly_compressed.js"></script>
7+
<script type="text/javascript" src="../../blocks_compressed.js"></script>
8+
<script type="text/javascript" src="../../javascript_compressed.js"></script>
9+
<script type="text/javascript" src="../../msg/js/en.js"></script>
10+
<script type="text/javascript" src="jquery-2.1.0.min.js"></script>
11+
<script type="text/javascript" src="coderbot.js"></script>
12+
<script type="text/javascript" src="blocks.js"></script> <style>
13+
body {
14+
background-color: #fff;
15+
font-family: sans-serif;
16+
}
17+
h1 {
18+
font-weight: normal;
19+
font-size: 140%;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<h1>CoderBot</h1>
25+
<p>CoderBot blockly programming</p>
26+
<p>
27+
<button onclick="showCode()">Show Code</button>
28+
<button onclick="runCode()">Run Code</button>
29+
</p>
30+
31+
<div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
32+
33+
<xml id="toolbox" style="display: none">
34+
<category name="Control">
35+
<block type="controls_if"></block>
36+
<block type="controls_repeat_ext">
37+
<value name="TIMES">
38+
<block type="math_number">
39+
<field name="NUM">10</field>
40+
</block>
41+
</value>
42+
</block>
43+
<block type="controls_whileUntil"></block>
44+
</category>
45+
<category name="Logic">
46+
<block type="logic_compare"></block>
47+
<block type="logic_operation"></block>
48+
<block type="logic_negate"></block>
49+
<block type="logic_boolean"></block>
50+
</category>
51+
<category name="Math">
52+
<block type="math_number"></block>
53+
<block type="math_arithmetic"></block>
54+
<block type="math_single"></block>
55+
</category>
56+
<category name="Text">
57+
<block type="text"></block>
58+
<block type="text_length"></block>
59+
<block type="text_print"></block>
60+
</category>
61+
<category name="CoderBot">
62+
<block type="coderbot_moveForward"></block>
63+
<block type="coderbot_moveBackward"></block>
64+
<block type="coderbot_turnLeft"></block>
65+
<block type="coderbot_turnRight"></block>
66+
</category>
67+
</xml>
68+
69+
<script>
70+
Blockly.inject(document.getElementById('blocklyDiv'),
71+
{path: '../../', toolbox: document.getElementById('toolbox')});
72+
73+
function showCode() {
74+
// Generate JavaScript code and display it.
75+
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
76+
var code = Blockly.JavaScript.workspaceToCode();
77+
alert(code);
78+
}
79+
80+
function runCode() {
81+
// Generate JavaScript code and run it.
82+
window.LoopTrap = 1000;
83+
Blockly.JavaScript.INFINITE_LOOP_TRAP =
84+
'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
85+
var code = Blockly.JavaScript.workspaceToCode();
86+
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
87+
try {
88+
eval(code);
89+
} catch (e) {
90+
alert(e);
91+
}
92+
}
93+
</script>
94+
95+
</body>
96+
</html>

0 commit comments

Comments
 (0)