-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
482 lines (404 loc) · 16.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Code Editor</title>
<link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="css/style.css" rel="stylesheet"/>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<!-- Toggle bar which appears when less than the appropriate size -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#/">Try It</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Examples <b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">Pong</li>
<li><a href="#">You're reading it!</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="#" class="view-api" title="View API" data-toggle="modal" data-target="#apiModal">
<span class="glyphicon glyphicon-question-sign"></span>
</a>
</li>
</ul>
</div>
</div>
</div>
<script id="api-detail" type="text/template">
<div>
<!-- TODO Is there a more idiomatic way of dealing with this? -->
<% if(name == "Available Entities") { %>
<%= description %>
<% _.each(todoNameMe, function(value, key) { %>
<div>
<h3><%= key %></h3>
<%= value.description %>
<h4>Available Attributes</h4>
<div>
<table class="table table-striped">
<thead>
<td>Name</td>
<td>Description</td>
<td>Default</td>
</thead>
<% _.each(value.defaults, function(value, key) { %>
<tr>
<td><%= key %></td>
<td><%= value.description %></td>
<td><%= value.default %></td>
</tr>
<% }) %>
</table>
</div>
</div>
<% }) %>
<% } %>
<% if(name == "Colors") { %>
<% _.each(todoNameMe, function(value, key) { %>
<div>
<%= key %> - <%= value %>
</div>
<% }) %>
<% } %>
<% if(name == "Keys") { %>
<% _.each(todoNameMe, function(value, key) { %>
<div>
<%= key %>
</div>
<% }) %>
<% } %>
</div>
</script>
<script id="code-editor-template" type="text/template">
<div class="text-right">
<a title="View API" class="copyCode" title="Copy-Code">
<span class="glyphicon glyphicon-pencil"></span>
<span></span>
</a>
</div>
<div <%= "id='" + id + "'" %>><%= code %></div>
</script>
<script id="api-overview" type="text/template">
<div class="panel panel-default">
<div class="panel-heading more">
<%= name %> - <%= description %>
</div>
<!-- Lazily populated -->
<div class="panel-body more-detail">
</div>
</div>
</script>
<div class="modal fade" id="apiModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Api Overview</h4>
</div>
<div class="modal-body">
The following functions have been defined for this demonstration.
<div intepreter-api>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="view">
<div id="tutorial-box">
<div class="col-md-4">
<div class="page">
<h2>Welcome</h2>
Let's learn how to code, click the next button to begin!
</div>
<div class="page">
<h2>Introduction</h2>
<div>Programming can be fun and challenging. Let's look at the 'Hello World' example below</div>
<div code-example>
log("Hello World")
</div>
<div>
You can copy this code into the editor using the copy button provided above the code example.
When copied into the main editor, this code can be ran by pressing the <a href="#" highlight-target="#run-code">Run Code button </a>.
</div>
<br />
<div>
This code, when ran, will output the message 'Hello World' to the <a href="#" highlight-target="#debug-window">console output</a>.
</div>
</div>
<div class="page">
<h2>Creating Shapes</h2>
<div>You can also create shapes! For instance you can create a Rectangle with the following code</div>
<div code-example>
var rectangle = Rectangle({
x: 10,
y: 20
});
</div>
<div>
You can create various entities (shapes) such as Circles and Text.
These are shown within the <a href="#" class="view-api" title="View API" data-toggle="modal" data-target="#apiModal">API</a>.
</div>
<div code-example>
var circle = Circle({
x: 50,
y: 50,
color: Colors.RED
});
</div>
</div>
<div class="page">
<h2>Loops</h2>
<div>Loops are able to run a block of code multiple times. For instance, this code can count up to 10</div>
<div code-example>
var count = 0;
while(count < 10) {
// Add one to the variable count
count++;
log("Adding 1 to the count " + count)
}
log("The final value is " + count)
</div>
<div>
Remember that these log messages will be added to the <a href="#" highlight-target="#debug-window">console</a>.
</div>
</div>
<div class="page">
<h2>Movement!</h2>
<div>Once an entity, such as a Circle is created, we can update its value within an 'update' function. As part of this update function we can logically update entities. In this example we increase the location of our circle by 5 on each update.</div>
<div code-example>
var velocity_x = 5;
var velocity_y = 5;
var circle = Circle({
x: 10,
y: 20
});
function update() {
circle.x += velocity_x;
circle.y += velocity_y;
}
</div>
<div>
We could update the code to to make sure it always stays within the bounds of the screen.
The size of the canvas is accessible via the world object.
</div>
<div code-example>
var WORLD_WIDTH = world.width;
var WORLD_HEIGHT = world.height;
var velocity_x = 5;
var velocity_y = 5;
var circle = Circle({
x: 10,
y: 20,
radius:10
});
function update() {
circle.x += velocity_x;
circle.y += velocity_y;
if(circle.x < 0 || circle.x > WORLD_WIDTH) {
velocity_x *= -1;
}
if(circle.y < 0 || circle.y > WORLD_HEIGHT) {
velocity_y *= -1;
}
}
</div>
</div>
<div class="page">
<h2>The End</h2>
<div>We hope you enjoyed yourself. It is possible to use all of the previous steps and create both basic and advanced programs!</div>
<div>Here is a simple game of pong!</div>
<div code-example>
log("Started Game!");
world.color = Colors.GREY;
var WORLD_WIDTH = world.width;
var WORLD_HEIGHT = world.height;
// Keeping track of the scores
var playerScore = 0;
var computerScore = 0;
var playerPaddle = Rectangle({
x: 10, y: (WORLD_HEIGHT / 2) - (80 / 2),
width: 20, height: 80,
color: Colors.GREEN,
velocity: 10
});
var computerPaddle = Rectangle({
x: WORLD_WIDTH - 30, y: (WORLD_HEIGHT / 2) - (80 / 2),
width: 20, height: 80,
color: Colors.GREEN,
velocity: 6
});
var score = Text({
x: (WORLD_WIDTH / 2) - 80,
y: 24,
text: "Player " + playerScore + " - Computers " + computerScore
});
var ball_radius = 10;
var ball_velocity = 10;
var ball = Circle({
x: (WORLD_WIDTH / 2) - (ball_radius / 2),
y: (WORLD_HEIGHT / 2) - (ball_radius / 2),
radius: ball_radius,
x_velocity: -ball_velocity,
y_velocity: ball_velocity
});
// Start the ball in the middle
resetBall();
function update() {
// Update the ball's position
updateBall();
// Check to see if anyone has one, and restart the game if they have
checkWinners();
// Human Controlling
movePlayer();
// Computer AI Mechanism for the paddle
moveComputer();
}
function movePlayer() {
if (keysDown.contains(Keys.UP) && (playerPaddle.y) > 0) {
playerPaddle.y -= playerPaddle.velocity;
}
if (keysDown.contains(Keys.DOWN) && (playerPaddle.y + playerPaddle.height) < WORLD_HEIGHT) {
playerPaddle.y += playerPaddle.velocity;
}
}
function moveComputer() {
var centerOfPaddle = computerPaddle.y + (computerPaddle.height / 2)
// Only begin to move the computer if the ball is more than half way across the game
if (ball.x > WORLD_WIDTH / 2) {
if (ball.y > centerOfPaddle) {
computerPaddle.y += computerPaddle.velocity;
} else {
computerPaddle.y -= computerPaddle.velocity;
}
}
}
function updateBall() {
// Update the ball position
ball.x += ball.x_velocity;
ball.y += ball.y_velocity;
// Check if we need to bounce on the top or bottom
if (ball.y <= 0 || ball.y >= WORLD_HEIGHT) {
ball.y_velocity *= -1;
}
// Check if we have bounced off a paddle or computer
if (ball.collidesWith(playerPaddle) || ball.collidesWith(computerPaddle)) {
ball.x_velocity *= -1;
}
}
function checkWinners() {
var ballPastPlayer = ball.x < 0;
var ballPastComputer = ball.x > WORLD_WIDTH;
if (ballPastPlayer || ballPastComputer) {
log("Game Lost!")
// Decide who gets the points!
if (ballPastPlayer) {
computerScore++;
} else if (ballPastComputer) {
playerScore++;
}
// Update the score text
score.text = "Player " + playerScore + " - Computers " + computerScore;
resetBall();
}
}
function resetBall() {
// restart the ball position in the center of the world
ball.x = (WORLD_WIDTH / 2) - (ball_radius / 2);
ball.y = (WORLD_HEIGHT / 2) - (ball_radius / 2);
ball.x_velocity = -ball_velocity;
ball.y_velocity = ball_velocity;
}
</div>
</div>
<div class="text-center" style="margin-top: 20px">
<input type="button" class="btn btn-default previousPage" value="previous"/>
<input type="button" class="btn btn-default nextPage" value="next"/>
</div>
</div>
</div>
<div class="col-md-8">
<div>
<a href="#"
id="run-code"
intepreter-runner
intepreter-code-box="code-box"
intepreter-target="#code-result"
intepreter-debugger="#debug-window"
intepreter-stop="#stop">
<span class="glyphicon glyphicon-refresh"></span>
<span>Run Code </span>
</a>
<a href="#" id="stop">
<span class="glyphicon glyphicon-exclamation-sign red"></span>
<span>Stop Code</span>
</a>
</div>
<div>
<div id="code-box">
</div>
</div>
<div>
<div class="text-center">Canvas</div>
<div id="code-result" class="environment">
Waiting for new program...
</div>
</div>
<div>
<div class="text-center">Log output</div>
<div id="debug-window" class="logging-container">
Waiting for new program...
</div>
</div>
</div>
</div>
</body>
<script>
var editorStyle = "ace/theme/dawn";
</script>
<script src="bower_components/jquery/dist/jquery.min.js" type="text/javascript"></script>
<script src="bower_components/jquery-ui/ui/minified/jquery-ui.min.js" type="text/javascript"></script>
<script src="bower_components/underscore/underscore.js" type="text/javascript" charset="utf-8"></script>
<script src="bower_components/ace-builds/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="bower_components/backbone/backbone.js" type="text/javascript" charset="utf-8"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
<script>
localStorage.latestCode = localStorage.latestCode || "";
$("#code-box").html(localStorage.latestCode);
var editor = ace.edit("code-box");
editor.setTheme(editorStyle);
editor.getSession().setMode("ace/mode/javascript");
editor.getSession().on('change', function(e) {
localStorage.latestCode = editor.getSession().getValue();
});
</script>
<script src="js/coderunner.js"></script>
<script src="js/binding.js"></script>
<script src="js/backbone.js"></script>
<script>
// Fake a click of the run button for debugging purposes
/*setTimeout(function () {
var runners = document.querySelectorAll("[intepreter-runner]");
[].forEach.bind(runners, function (runner) {
runner.click()
})()
}, 100);*/
</script>
</html>