-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.js
242 lines (215 loc) · 6.79 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
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
/**
* Snake directions enumeration
*/
var SnakeDirection = {
UP: 1,
DOWN: 2,
RIGHT: 3,
LEFT: 4
};
/**
* Snake class constructor
* @param Ground ground The ground object for the snake
*/
function Snake(ground){
//The snake direction
this.direction = SnakeDirection.RIGHT;
//The ground for the snake
this.ground = ground;
//Swicht to know if the snake can move or is collided
this.canMove = true;
//List of points to draw the snake
this.blocks = [];
//List of colors to draw the snake
this.colors = [];
}
/**
* Snake class definition
*/
Snake.prototype = {
/**
* Check if the snake contains a point specified by x and y params
* @param int x
* @param int y
*/
hasPoint: function(x, y)
{
for(var i=0;i<this.blocks.length; i++){
var point = this.blocks[i];
if(point.x == x && point.y == y){
return true;
}
}
return false;
},
/**
* Check if the snake's head is over an apple. If true, increment
* the snake's size.
* @param array apples List of apple objects in the ground
*/
eat: function(apples)
{
var head = this.blocks[this.blocks.length-1];
for(var i=0; i<apples.length; i++){
var apple = apples[i];
if(head.x == apple.point.x && head.y == apple.point.y
&& this.blocks.length < this.ground.groundBlocks*2){
this.blocks.push(head);
this.createColors();
return i;
}
}
return false;
},
/**
* Allocate the snake in the ground to start the game
*/
center: function()
{
var centerX = this.ground.blockSize;
var centerY = this.ground.blockSize;
this.blocks.push(new Point(centerX, centerY));
this.blocks.push(new Point(centerX*2, centerY));
this.createColors();
},
/**
* Allocate the snake inner game blocks
*/
reallocate: function(){
for(var i=0; i<this.blocks.length; i++){
var point = this.blocks[i];
while(point.x%this.ground.blockSize != 0){
point.x++;
}
while(point.y%this.ground.blockSize != 0){
point.y++;
}
}
},
/**
* Move the snake one position into the current direction and check for
* collisions.
* @return bool If it has collision
*/
move: function(){
var head = this.blocks[this.blocks.length-1];
var newHeadPoint;
switch(this.direction){
case SnakeDirection.RIGHT:
newHeadPoint = new Point(head.x+this.ground.blockSize,head.y);
break;
case SnakeDirection.LEFT:
newHeadPoint = new Point(head.x-this.ground.blockSize,head.y);
break;
case SnakeDirection.DOWN:
newHeadPoint = new Point(head.x,head.y+this.ground.blockSize);
break;
case SnakeDirection.UP:
newHeadPoint = new Point(head.x,head.y-this.ground.blockSize);
break;
}
var hasCollision = this.hasCollision(newHeadPoint);
if(!hasCollision){
this.blocks.push(newHeadPoint);
this.blocks.shift();
this.draw();
}
this.canMove = true;
return hasCollision;
},
/**
* Specified method for checking collisions
* @param Point point the point to checking collisions (snake's head)
* @return bool
*/
hasCollision: function(point){
//Checks if snake has collided with the wall
if(point.x < 0 || point.y < 0 ||
point.x >= this.ground.width || point.y >= this.ground.height){
Game.pause();
}
//Checks if the snake has eaten his own tail
for(var i=0;i<this.blocks.length; i++){
var p = this.blocks[i];
if(p.x == point.x && p.y == point.y){
return true;
}
}
return false;
},
/**
* Draw the snake on the ground
*/
draw: function(){
var ctx = this.ground.ctx;
var midBlock = parseInt(this.ground.blockSize/2)
//var colors = this.createColors(this.blocks.length);
var total = this.blocks.length;
for(var i=0; i<total; i++){
ctx.beginPath();
var point = this.blocks[i];
ctx.fillStyle = this.colors[i];
ctx.arc(point.x+midBlock, point.y+midBlock,
this.ground.blockSize/1.5, 0, Math.PI*2, false);
ctx.moveTo(point.x, point.y);
ctx.closePath();
ctx.fill();
}
},
/**
* Changhe the snake direction movement
* @param SnakeDirection direction
*/
setDirection: function(direction){
var sum = this.direction+direction;
//ARROW_LEFT+ARROW_RIGHT || ARROW_UP+ARROW_DOWN
if(sum != 7 && sum != 3 && this.canMove && !Game.paused){
this.direction = direction;
this.canMove = false;
}
},
/**
* Set a list of gradient colors to draw the snake
*/
createColors: function(){
var size = this.blocks.length;
var endColor = '#000a57';
var startColor = '#75799b';
var regexp = /#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})/i;
var startColorChannels = startColor.match(regexp);
var endColorChannels = endColor.match(regexp);
var startColors = {
'r': parseInt('0x' + startColorChannels[1]),
'g': parseInt('0x' + startColorChannels[2]),
'b': parseInt('0x' + startColorChannels[3])
};
var endColors = {
'r': parseInt('0x' + endColorChannels[1]),
'g': parseInt('0x' + endColorChannels[2]),
'b': parseInt('0x' + endColorChannels[3])
};
var redDif = endColors.r-startColors.r;
var greenDif = endColors.g-startColors.g
var blueDif = endColors.b-startColors.b;
var redInc = parseInt(redDif/size);
var greenInc = parseInt(greenDif/size);
var blueInc = parseInt(blueDif/size);
this.colors = [];
for(var i=0; i<size; i++){
startColors.r = startColors.r+redInc;
startColors.g = startColors.g+greenInc;
startColors.b = startColors.b+blueInc;
var hex = '#'+this.intToHex(startColors.r)
+this.intToHex(startColors.g)
+this.intToHex(startColors.b);
this.colors.push(hex);
}
},
/**
* Convert an integer to hexadecimal. Used by createColors
* @param int number
*/
intToHex: function(number){
return (0x1000 + number).toString(16).substr(-2);
}
};