-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
38 lines (33 loc) · 869 Bytes
/
Copy pathsnake.js
File metadata and controls
38 lines (33 loc) · 869 Bytes
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
function Snake(row, col, course)
{
this.body = [{x: row, y: col}];
this.course = course;
this.getNextCoords = function()
{
var coords = {};
switch (this.course)
{
case 'left':
coords = {x: this.body[0].x, y: this.body[0].y - 1};
break;
case 'up':
coords = {x: this.body[0].x - 1, y: this.body[0].y};
break;
case 'right':
coords = {x: this.body[0].x, y: this.body[0].y + 1};
break;
case 'down':
coords = {x: this.body[0].x + 1, y: this.body[0].y};
}
return coords;
};
this.grow = function()
{
this.body.unshift(this.getNextCoords());
};
this.move = function()
{
this.grow();
this.body.pop();
};
}