-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanaconda.js
executable file
·390 lines (320 loc) · 9.71 KB
/
anaconda.js
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
var canvas;
var ctx;
var outputPanel;
var x = 250;
var y = 190;
var rightDown = false;
var leftDown = false;
var boost = false;
var anaconda = null;
var apples = [];
var anacondaTail = [];
var pause = false;
var death = false;
//Defined constant(ish) variables
var WIDTH;
var HEIGHT;
var SEGMENT_WIDTH = 1;
var SEGMENT_SIZE = 8;
var APPLE_SIZE = 10;
var APPLE_WIDTH = 2;
var APPLE_TOTAL = 5;
var APPLE_CHANCE = 0.005;
var ANACONDA_SPEED = 4;
var ANACONDA_BOOST_SPEED = 7;
var ANACONDA_NEW_SEGMENT = 800;
var INTERVAL = 25;
var DIRECTION_CHANGE_AMOUNT = 8;
var PI_180 = Math.PI/180;
function anacondaGameInit()
{
outputPanel = $("#output-panel");
canvas = $("#anaconda-canvas");
WIDTH = canvas.width();
HEIGHT = canvas.height();
$(document).keydown(onKeyDown);
$(document).keyup(onKeyUp);
ctx = canvas[0].getContext("2d"); //Grab the 2D context
createAnaconda(); //Create the anaconda
draw();
addApple();
return setInterval(draw, INTERVAL);
}
function createAnaconda() {
anaconda = new ob_anaconda();
}
function draw() {
if(!pause && !death) {
ctx.setTransform(1,0,0,1,0,0); //Reset to identity
ctx.clearRect(0,0,WIDTH, HEIGHT); //Clear the canvas
updateOutputPanel(); //Update the ouput panel
var rand = Math.random();
//Randomly create an apple APPLE_CHANCE of the time provided the field isn't full of apples
if (apples.length < APPLE_TOTAL && rand < APPLE_CHANCE) {
addApple();
}
//Draw each apple, collision detect, then move it
ctx.strokeStyle = 'red';
$.each(apples, function(index, apple) {
if(apple) { //Make sure the apple really exists
ctx.lineWidth = APPLE_WIDTH;
//Draw
ctx.beginPath();
ctx.moveTo(apple.x - (APPLE_SIZE/2), apple.y - (APPLE_SIZE/2));
ctx.lineTo(apple.x + (APPLE_SIZE/2), apple.y + (APPLE_SIZE/2));
ctx.moveTo(apple.x + (APPLE_SIZE/2), apple.y - (APPLE_SIZE/2));
ctx.lineTo(apple.x - (APPLE_SIZE/2), apple.y + (APPLE_SIZE/2));
ctx.closePath();
ctx.stroke();
//Collision detect walls
if (apple.x + apple.dx > WIDTH || apple.x + apple.dx < 0)
apple.dx = -apple.dx;
if (apple.y + apple.dy > HEIGHT || apple.y + apple.dy < 0)
apple.dy = -apple.dy;
//Collision with anaconda mouth - Chomp
ax = apple.x;
ay = apple.y;
fuzz = 15;
if(anaconda.x < (ax + fuzz) && anaconda.x > (ax - fuzz) ) {
if (anaconda.y < (ay + fuzz) && anaconda.y > (ay - fuzz) ) {
//Remove the apple
apples.splice(index, 1);
//Add another segment to the anaconda
anacondaTail.push(new ob_anacondaSegment(anaconda.x,anaconda.y,0,0,anaconda.direction));
}
}
//Move
apple.x += apple.dx;
apple.y += apple.dy;
}
});
/*
*
* Draw the anaconda
*
*/
//Draw each snake segment
$.each(anacondaTail, function(index, segment) {
ctx.setTransform(1,0,0,1,0,0); //Reset to identity
ctx.translate(segment.x,segment.y); //Move to the centre of the segment
seg_size = SEGMENT_SIZE;
//Make the tail taper to a point
if(index <= 4 ) {
switch(index) {
case 4:
seg_size -= 1;
break;
case 3:
seg_size -= 2;
break;
case 2:
seg_size -= 4;
break;
case 1:
seg_size -= 6;
break;
case 0:
seg_size -= 8;
break;
}
}
ctx.lineWidth = SEGMENT_WIDTH;
//Draw the leading edge
ctx.strokeStyle = 'blue';
ctx.rotate(segment.direction*(PI_180));
ctx.moveTo(0,-seg_size);
ctx.lineTo(0,seg_size);
ctx.strokeStyle = 'green';
//Draw the lines to the previous segment
if(index !== 0) {
anacondaTail[index].width = seg_size;
//Left edge
ctx.setTransform(1,0,0,1,0,0); //Reset to identity
ctx.translate(anacondaTail[index-1].x,anacondaTail[index-1].y); //Move to the centre of the segment
ctx.rotate(anacondaTail[index-1].direction*(PI_180));
ctx.lineTo(0,anacondaTail[index-1].width);
//Right edge
ctx.setTransform(1,0,0,1,0,0); //Reset to identity
ctx.translate(segment.x,segment.y); //Move to the centre of the segment
ctx.rotate(segment.direction*(PI_180));
ctx.moveTo(0,-seg_size);
ctx.setTransform(1,0,0,1,0,0); //Reset to identity
ctx.translate(anacondaTail[index-1].x,anacondaTail[index-1].y); //Move to the centre of the segment
ctx.rotate(anacondaTail[index-1].direction*(PI_180));
ctx.lineTo(0,-seg_size);
}
});
ctx.stroke();
ctx.setTransform(1,0,0,1,0,0); //Reset to identity
//Detect if left/right are pressed and rotate anaconda as needed
if (rightDown) {
anaconda.direction += DIRECTION_CHANGE_AMOUNT;
}
else if (leftDown) {
anaconda.direction -= DIRECTION_CHANGE_AMOUNT;
}
if(anaconda.direction > 360) {
anaconda.direction -= 360;
}
if(anaconda.direction< 0) {
anaconda.direction += 360;
}
//Detect boost mode
speed = ANACONDA_SPEED;
if(boost) {
speed = ANACONDA_BOOST_SPEED;
}
angleInRadians = anaconda.direction * Math.PI / 180;
facingX = Math.cos(angleInRadians);
facingY = Math.sin(angleInRadians);
anaconda.dx = speed * facingX;
anaconda.dy = speed * facingY;
//Mark the centre of the anaconda head
if(anacondaTail.length > 0) {
ctx.strokeStyle = 'white';
ctx.setLineWidth(5);
ctx.strokeRect(anaconda.x, anaconda.y, 1, 1);
ctx.lineWidth = SEGMENT_WIDTH;
}
//Collision detect the anaconda
//Collision with wall
if(anaconda.x < 0 || anaconda.x > WIDTH || anaconda.y < 0 || anaconda.y > HEIGHT) {
death = true;
}
ctx.strokeStyle = 'green';
ctx.translate(anaconda.x,anaconda.y);
//Move the anaconda
anaconda.x += anaconda.dx;
anaconda.y += anaconda.dy;
anaconda.distance += anaconda.x > anaconda.y ? anaconda.x : anaconda.y;
//Add new segements once the anaconda has moved a certain distance
if(anaconda.distance >= ANACONDA_NEW_SEGMENT) {
anaconda.distance = 0;
anacondaTail.push(new ob_anacondaSegment(anaconda.x,anaconda.y,0,0,anaconda.direction));
//Remove the anaconda tail as it grows
if (anacondaTail.length > anaconda.length) {
anacondaTail.splice(0,1);
}
}
ctx.rotate(anaconda.direction*(PI_180));
//Draw the leading edge
ctx.strokeStyle = 'green';
ctx.beginPath();
ctx.moveTo(0,-SEGMENT_SIZE);
ctx.lineTo(0,SEGMENT_SIZE);
ctx.stroke();
//Draw the line from the head to the first segment
if(anacondaTail.length > 0) {
//Left edge 1
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anacondaTail[anacondaTail.length - 1].x,anacondaTail[anacondaTail.length - 1].y); //Move to the centre of the segment
ctx.rotate(anacondaTail[anacondaTail.length - 1].direction*(PI_180));
ctx.moveTo(0,SEGMENT_SIZE);
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anaconda.x,anaconda.y);
ctx.rotate((anaconda.direction + 2) * (PI_180));
ctx.lineTo(8,SEGMENT_SIZE + 6);
//Left edge 2
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anaconda.x,anaconda.y);
ctx.rotate((anaconda.direction + 2) * (PI_180));
ctx.lineTo(20,SEGMENT_SIZE - 3);
//Right edge 1
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anacondaTail[anacondaTail.length - 1].x,anacondaTail[anacondaTail.length - 1].y); //Move to the centre of the segment
ctx.rotate(anacondaTail[anacondaTail.length - 1].direction*(PI_180));
ctx.moveTo(0,-SEGMENT_SIZE);
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anaconda.x,anaconda.y);
ctx.rotate((anaconda.direction - 2)*(PI_180));
ctx.lineTo(8,-SEGMENT_SIZE - 6);
//Right edge 2
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anaconda.x,anaconda.y);
ctx.rotate((anaconda.direction - 2)*(PI_180));
ctx.lineTo(20,SEGMENT_SIZE - 15);
//Join left 1 and right 1
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anaconda.x,anaconda.y);
ctx.rotate((anaconda.direction + 2) * (PI_180));
ctx.moveTo(8,SEGMENT_SIZE + 6);
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anaconda.x,anaconda.y);
ctx.rotate((anaconda.direction + 2) * (PI_180));
ctx.lineTo(8,-SEGMENT_SIZE - 6);
//Join left 2 and right 2
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anaconda.x,anaconda.y);
ctx.rotate((anaconda.direction + 2) * (PI_180));
ctx.moveTo(20,SEGMENT_SIZE - 17);
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(anaconda.x,anaconda.y);
ctx.rotate((anaconda.direction + 2) * (PI_180));
ctx.lineTo(20,SEGMENT_SIZE - 3);
}
ctx.stroke();
}
}
function updateOutputPanel() {
outputPanel.html(anaconda.direction + ' º | ' + Math.round(anaconda.x) + ':' + Math.round(anaconda.y) + ' | ' + Math.round(anaconda.dx) + ':' + Math.round(anaconda.dy));
}
function addApple() {
apples.push(new ob_apple());
}
function onKeyDown(evt) {
switch(evt.keyCode) {
case 39:
rightDown = true;
break;
case 37:
leftDown = true;
break;
case 32:
case 38:
boost = true;
break;
case 80:
pause = !pause;
}
}
function onKeyUp(evt) {
switch(evt.keyCode) {
case 39:
rightDown = false;
break;
case 37:
leftDown = false;
break;
case 32:
case 38:
boost = false;
}
}
/***
*
* Define objects
*
***/
function ob_anaconda() {
this.x = 150;
this.y = 80;
this.dx = 0;
this.dy = 1;
this.direction = 0;
this.length = 15;
this.distance = 0;
}
function ob_anacondaSegment(x, y, dx,dy, direction){
// console.log('New anaconda segment: x:y=' + this.x + ':' + this.y + ' dx:dy= ' +this.x + ':' + this.y);
this.x = typeof(x) != 'undefined' ? x : null;
this.y = typeof(y) != 'undefined' ? y : null;
this.direction = typeof(direction) != 'undefined' ? direction : null;
this.width = SEGMENT_SIZE;
}
function ob_apple() {
// console.log('New apple created');
this.x = randomnumber=Math.floor(Math.random()*WIDTH);
this.y = randomnumber=Math.floor(Math.random()*HEIGHT);
this.dx = randomnumber=Math.floor(Math.random()*5) -5;
this.dy = randomnumber=Math.floor(Math.random()*5) -5;
}