forked from vikingeducation/project_game_center_browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
333 lines (272 loc) · 7.88 KB
/
script.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
'use strict;'
var model = {
init: function(gridSize) {
model.gridSize = gridSize;
// build array of cells
model.cells = model.buildGrid();
// create snake with starting coordinates
model.snake.spawn();
// spawn food
model.food.spawn();
},
gridSize: 0,
score: 0,
gameover: false,
cells: [],
snake: {
cells: [],
direction: 'right',
nextDirection: 'right',
spawn: function() {
var startingCell = model.findCellByCoords(3,4);
model.snake.cells = [startingCell];
startingCell.snake = true;
},
move: function() {
model.snake.direction = model.snake.nextDirection;
var newX = model.snake.cells[0].x + model.snake.movementX();
var newY = model.snake.cells[0].y + model.snake.movementY();
var newCell = model.findCellByCoords(newX, newY);
model.gameover = (model.snake.flagWallCollision(newX, newY) || model.snake.flagSnakeCollision(newCell))
if (!model.gameover) {
model.snake.cells.unshift(newCell);
// sets current cell to be part of the snake
model.snake.cells[0].snake = true;
if (newCell.food) {
model.food.eaten()
}
else {
// pops off the last cell in the snake if no food is eaten
model.snake.cells.pop().snake = false;
}
return newCell;
}
},
movementX: function () {
if (model.snake.direction === 'right') {
return 1
}
else if (model.snake.direction === 'left') {
return -1
}
else {
return 0
};
},
movementY: function () {
if (model.snake.direction === 'down') {
return 1
}
else if (model.snake.direction === 'up') {
return -1
}
else {
return 0
};
},
flagWallCollision: function(newX, newY) {
return (model.outOfBounds(newX) || model.outOfBounds(newY));
},
flagSnakeCollision: function(newCell) {
return !!newCell.snake;
},
newDirection: function() {
// event.which property normalizes event.keyCode and event.charCode
model.snake.nextDirection = model.snake.filterInput(event.which);
},
filterInput: function(input) {
// if current is left or right, should only accept up or down
if (model.snake.direction === 'left' || model.snake.direction === 'right') {
switch (input) {
case 38:
return 'up';
break;
case 40:
return 'down';
break;
default:
return model.snake.nextDirection;
};
}
else {
switch(input) {
case 37:
return 'left';
break;
case 39:
return 'right';
break;
default:
return model.snake.nextDirection;
};
};
}
},
food: {
cell: [],
spawn: function() {
var sample = model.food.randomSpawn();
model.food.cell = [sample];
sample.food = true;
},
randomSpawn: function() {
// grep method removes items from an array so that remaining items pass a provided test
// return all cells not flagged as containing snake
var freeCells = $.grep(model.cells, function(cell) {
return (cell.snake === false);
});
// choose random cell for food placement
return freeCells[Math.floor(freeCells.length * Math.random())];
},
eaten: function() {
model.score += 1;
model.food.cell[0].food = false;
model.food.spawn();
}
},
buildGrid: function() {
var output = [];
for (var i = 0; i < Math.pow(model.gridSize, 2); i++) {
var newCell = new model.cellConstructor(i);
output.push(newCell);
}
// returns cells from cellConstructor with id, (x, y) coords, snake and food booleans to determine what the cell contains
return output;
},
cellConstructor: function(i) {
this.id = i;
// produce (x, y) coordinates
this.x = i % model.gridSize;
this.y = Math.floor(i / model.gridSize);
this.snake = false;
this.food = false;
},
findCellByCoords: function(x, y) {
// reproduces cell ID from coordinates to find cell
var i = y * (model.gridSize) + x;
return model.cells[i];
},
outOfBounds: function(coordinate) {
// checks if newX or newY coordinates are off the grid
if (coordinate < 0 || coordinate >= model.gridSize) {
return true
};
},
getSnakeIDs: function() {
// translates snake cells to their IDs and returns that array
return $.map(model.snake.cells, function(cell) {
return cell.id
});
},
getFoodIDs: function() {
// only one cell contains food at any time
return model.food.cell[0].id;
},
getSnakeDirection: function() {
return model.snake.direction;
},
nextFrame: function() {
// simplifies gameloop
var newCell = model.snake.move();
}
}
var view = {
init: function(gridSize) {
view.buildGrid(gridSize);
controller.show();
// checks for keys pressed; newDirection() only responds to up, down, left, right
$(window).on('keydown', model.snake.newDirection)
},
buildGrid: function(size) {
// creates cells in view based on given gridSize
for(var i = 0; i < Math.pow(size, 2); i++) {
$('.board').append("<div class='cell'></div>")
}
view.setDimensions(size);
},
setDimensions: function(size) {
// keeps the dimensions of the board and cells proportional to the gridSize when the model and view are initiated
// integer value is the pixel width of the board
var cellSizePx = Math.floor(720 / size) + 'px';
$('.board').css('line-height', cellSizePx);
$('.cell').css('height', cellSizePx);
$('.cell').css('width', cellSizePx);
},
renderFrame: function(snakeIDs, snakeHeadID, foodID, score, gameover) {
view.resetFrame();
view.renderScore(score);
// get all snake parts
$.each( snakeIDs, function(i, id) { view.drawSnake(id) } );
// get snake head
view.drawSnakeHead(snakeHeadID, gameover);
// get food location
view.drawFood(foodID);
},
renderScore: function(score) {
$('.scoreboard em')[0].innerHTML = score;
},
renderEndgame: function() {
$('.board').after("<center><h3>Game Over!</h3></center>");
},
resetFrame: function() {
$('.board').children().removeClass('food snake head left right up down');
},
drawFood: function(i) {
// add food class to appropriate cell given ID
$('.board').children().eq(i).addClass('food');
},
drawSnake: function(i) {
// add snake class to appropriate cell given ID
$('.board').children().eq(i).addClass('snake');
},
drawSnakeHead: function(i, gameover) {
// snake head is red if the game is over; else green
if (gameover) {
$('.board').children().eq(i).addClass('dead');
}
else {
$('.board').children().eq(i).addClass('head');
}
}
}
var controller = {
init: function(size, speed) {
controller.size = size;
controller.speed = speed;
// setup board
model.init(controller.size);
view.init(controller.size);
// start the loop
controller.play();
},
size: 0,
speed: 0,
gameInterval: null,
show: function() {
var snakeIDs = model.getSnakeIDs();
var snakeHeadID = snakeIDs[0];
var direction = model.getSnakeDirection();
var foodID = model.getFoodIDs();
var score = model.score;
var gameover = model.gameover;
view.renderFrame(snakeIDs, snakeHeadID, foodID, score, gameover);
},
play: function() {
controller.gameInterval = setInterval(controller.gameloop, controller.speed);
},
gameloop: function() {
// update model
model.nextFrame();
controller.show();
if (model.gameover) {
controller.endGame();
}
},
endGame: function() {
clearInterval(controller.gameInterval);
view.renderEndgame();
}
}
$(document).ready( function() {
controller.init(30, 100);
})