-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
136 lines (116 loc) · 3.2 KB
/
snake.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
class Snake {
constructor(snakeTail, verticalSquares, horizontalSquares) {
this.tail = snakeTail;
this.trail = [];
this.verticalSquares = verticalSquares;
this.horizontalSquares = horizontalSquares;
this.vertical = 0;
this.horizontal = -1; // start going left
this.buildTail();
}
buildTail() {
// start in the middle of screen
let middleOfScreenVertical = Math.trunc(this.verticalSquares / 2);
let middleOfScreenHorizontal = Math.trunc(this.horizontalSquares / 2);
for (let i=0; i<this.tail; i++) {
this.trail.push({
vertical: middleOfScreenVertical,
horizontal: middleOfScreenHorizontal + i
})
}
}
get positions() {
return this.trail;
}
crawl() {
this.trail.unshift({
vertical: this.trail[0].vertical + this.vertical,
horizontal: this.trail[0].horizontal + this.horizontal
});
this.trail.pop();
this.checkEdgeLimits();
}
checkEdgeLimits() {
for (let i=0; i<this.trail.length; i++) {
// when snake overlap the left edge
if (this.trail[i].horizontal < 1) {
this.trail[i].horizontal = this.horizontalSquares;
}
// when snake overlap the right edge
if (this.trail[i].horizontal > this.horizontalSquares) {
this.trail[i].horizontal = 1;
}
// when snake overlap the top edge
if (this.trail[i].vertical < 1) {
this.trail[i].vertical = this.verticalSquares;
}
// when snake overlap the bottom edge
if (this.trail[i].vertical > this.verticalSquares) {
this.trail[i].vertical = 1;
}
}
}
pointUp() {
this.vertical = -1;
this.horizontal = 0;
}
pointDown() {
this.vertical = +1;
this.horizontal = 0;
}
pointLeft() {
this.vertical = 0;
this.horizontal = -1;
}
pointRight() {
this.vertical = 0;
this.horizontal = +1;;
}
invertingTail(futureDirection) {
// going left
if (this.horizontal < 0 && futureDirection == 'right')
return true;
// going right
if (this.horizontal > 0 && futureDirection == 'left')
return true;
// going up
if (this.vertical < 0 && futureDirection == 'down')
return true;
// going down
if (this.vertical > 0 && futureDirection == 'up')
return true;
return false;
}
ateOwnTail() {
for (let i=0; i<this.trail.length; i++) {
for (let z=0; z<this.trail.length; z++) {
if (z!=i) {
if ((this.trail[i].horizontal == this.trail[z].horizontal) &&
(this.trail[i].vertical == this.trail[z].vertical)) {
return true
}
}
}
}
return false;
}
ateApple(apple) {
return this.overlapsApple(apple);
}
growTail() {
this.trail.push({
vertical: this.trail[this.trail.length - 1].vertical + this.vertical,
horizontal: this.trail[this.trail.length - 1].horizontal + this.horizontal
});
}
overlapsApple(apple) {
for (let i=0; i<this.trail.length; i++) {
// when a snake tail part overlaps the apple
if ((this.trail[i].horizontal == apple.position.horizontal) &&
(this.trail[i].vertical == apple.position.vertical)) {
return true
}
}
return false;
}
}