This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
249 lines (215 loc) · 5.75 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
243
244
245
246
247
248
249
var SnakeGame = (function(){
var SIZE = 20
var SNAKESPEED = 100
var SNAKEINTERVAL
var Board = function(size){
this.width = size;
this.height = size;
this.grid = [];
//initialize grid
var that = this
_.times(size*size, function(){
that.grid.push("_");
})
this.getPosition = function(coords){
return this.grid[coords[0]+coords[1]*this.width];
};
this.setPosition = function(coords, elem){
this.grid[coords[0]+coords[1]*this.width] = elem;
};
this.offBoard = function(coords){
return coords[0] < 0 || coords[0] > this.width - 1 ||
coords[1] < 0 || coords[1] > this.height - 1 ;
}
this.removeApple = function(){
var applePos = 0;
_.each(this.grid, function(elem, index){
if (elem === "apple"){
applePos = index;
}
});
this.grid[applePos] ="_";
}
this.addApple = function(){
this.removeApple();
var openPositions = []
_.each(this.grid, function(pos, index){
if (pos === "_"){
openPositions.push(index);
}
});
this.grid[openPositions[_.random(0,openPositions.length - 1)]] = "apple"
}
this.update = function(snake){
var that = this;
this.clearBoard();
_.each(snake.bodyParts, function(bodyPart, index){
var item = ""
if (index === 0){
item = "rattle"
}
else {
item = "snake"
}
that.setPosition([bodyPart.xPos, bodyPart.yPos], item)
})
}
this.clearBoard = function(){
var that = this;
_.each(this.grid, function(elem, index){
if (elem === "snake" || elem === "rattle"){
that.grid[index] = "_";
}
})
}
this.printBoard = function(){
var that = this;
boardString = "";
_.each(this.grid, function(el, index){
if (index % that.width === 0){
boardString += "\n"
}
if (el === "apple")
boardString += "a";
else if(el === "snake")
boardString += "s";
else
boardString += "_";
})
return boardString
}
//add initial apple
}
var Snake = function(board){
this.bodyParts = [new SnakeElement(board.width/2 - 1, board.height/2),
new SnakeElement(board.width/2, board.height/2)]
this.direction = [1,0];
this.board = board;
this.front = function(){
return this.bodyParts[this.bodyParts.length - 1]
};
this.back = function(){
return this.bodyParts[0];
};
this.setDirection = function(direc){
if (this.direction[0] === direc[0]* -1 ||
this.direction[1] === direc[1]* -1){
this.direction = this.direction;
}
else {
this.direction = direc;
}
};
this.nextSpot = function(){
var front = this.front();
var nextXSpot = front.xPos + this.direction[0];
var nextYSpot = front.yPos + this.direction[1];
return [nextXSpot, nextYSpot];
};
this.dead = function(){
var next = this.nextSpot();
return this.board.getPosition(next) === "snake" ||
this.board.offBoard(next);
};
this.eat = function(){
var next = this.nextSpot();
return this.board.getPosition(next) === "apple";
};
this.move = function(){
var back = this.bodyParts.shift();
var front = this.front();
back.xPos = front.xPos + this.direction[0];
back.yPos = front.yPos + this.direction[1];
this.bodyParts.push(back);
};
this.grow = function(){
var xPos = this.front().xPos + this.direction[0];
var yPos = this.front().yPos + this.direction[1];
var newPart = new SnakeElement(xPos, yPos);
this.bodyParts.push(newPart);
};
};
var SnakeElement = function(xPos, yPos){
this.xPos = xPos;
this.yPos = yPos;
};
var Game = function(){
this.board = new Board(SIZE);
this.snake = new Snake(this.board);
this.east = false;
this.west = false;
this.north = false;
this.south = false;
this.score = 0;
this.stop = false;
this.nextMove = function(){
var direc = this.snake.direction;
if (this.east){
direc = [1,0];
}
else if(this.west){
direc = [-1,0];
}
else if(this.south){
direc = [0,-1];
}
else if(this.north){
direc = [0,1];
}
this.snake.setDirection(direc);
};
this.gameOver = function(){
this.snake = new Snake(this.board);
this.board.addApple();
this.east = false;
this.west = false;
this.north = false;
this.south = false;
window.clearInterval(SNAKEINTERVAL);
this.stop = true;
};
this.resetGame = function(){
var that = this;
SNAKEINTERVAL = window.setInterval(function(){
that.step();
}, SNAKESPEED);
this.score = 0;
this.stop = false;
};
this.increaseSpeed = function(){
var that = this;
window.clearInterval(SNAKEINTERVAL);
SNAKEINTERVAL = window.setInterval(function(){
that.step();
}, SNAKESPEED - that.score/10);
};
this.step = function(){
this.nextMove();
if (this.snake.dead()){
this.gameOver();
}
else if (this.snake.eat()){
this.snake.grow();
this.board.addApple();
this.score += 10;
this.increaseSpeed();
}
else {
this.snake.move();
}
this.board.update(this.snake);
};
this.start = function(){
var that = this;
that.board.update(this.snake);
that.board.setPosition([8,5], "apple");
SNAKEINTERVAL = window.setInterval(function(){
that.step();
}, SNAKESPEED);
};
};
return {
Game: Game,
size: SIZE
};
})();