-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
310 lines (260 loc) · 7.06 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
//***********MAIN.js**********//
var gameBoard;
var snake;
var moveDirection = 'right';
var gameExecutor;
var gameSpeed=100;
var roundNum = 1;
var eatenItemsCount =0;
var MAX_FOOD_ITEMS = 12;
//actual field size(600px) divided by corresponding bodypart size(10px)
var gameFieldRelativeWidth = 60;
var gameFieldRelativeHeight = 60;
//width and height of snake body element
var snakeElementWidth = 10;
var snakeElementHeight = 10;
//game keys
var ESC = 27;
var SPACE = 32;
var LEFT_ARROW = 37;
var UP_ARROW = 38;
var RIGHT_ARROW = 39;
var DOWN_ARROW = 40;
var food;
$(document).ready(function() {
$('#loseMsg').hide();
$('body').keydown(keyPressedHandler);
});
function move() {
generateFood();
snake.move(moveDirection);
if(snake.holdsPosition(food.xPos,food.yPos))
eatFood();
drawSnake();
};
function keyPressedHandler(e) {
var code = (e.keyCode ? e.keyCode : e.which);
switch(code) {
case LEFT_ARROW:
moveDirection = 'left';
break;
case UP_ARROW:
moveDirection = 'up';
break;
case RIGHT_ARROW:
moveDirection = 'right';
break;
case DOWN_ARROW:
moveDirection = 'down';
break;
case SPACE:
startGame();
break;
case ESC:
endGame();
break;
}
}
function startGame() {
$('#music').get(0).play();
gameBoard = new GameBoard();
moveDirection = 'right';
eatenItemsCount = 0;
roundNum = 1;
gameSpeed=100;
endGame();
gameBoard.clearGameInfo();
snake = new Snake(80,80);
snake.onCrash(snakeCrashHandler,{xPos:600,yPos:600});
drawSnake();
gameExecutor = setInterval(move,gameSpeed);
};
function endGame() {
if(gameExecutor)
clearInterval(gameExecutor);
gameBoard.clearBoard();
};
function drawSnake() {
gameBoard.removeSnakeBody();
//draw the new snake
var snakeBody = snake.getBody();
for(var i=0; i<snakeBody.length; i++){
gameBoard.drawElement('bodypart',snakeBody[i].xPos,snakeBody[i].yPos);
}
};
function generateFood() {
if(gameBoard.hasNoCreatedFood()){
do{
xpos = Math.floor(Math.random() * gameFieldRelativeWidth) * snakeElementWidth;
ypos = Math.floor(Math.random() * gameFieldRelativeHeight)* snakeElementHeight;
}
while(snake.holdsPosition(xpos,ypos));
food = {xPos:xpos,yPos:ypos};
gameBoard.drawElement('food',xpos,ypos);
}
};
function eatFood() {
snake.eatFood();
gameBoard.removeSnakeFood();
eatenItemsCount++;
if(eatenItemsCount >= MAX_FOOD_ITEMS)
startNextRound();
gameBoard.updateScore(roundNum);
};
function snakeCrashHandler() {
endGame();
$('#music').get(0).pause();
$('#music').get(0).currentTime=0;
$('#hit').get(0).play();
gameBoard.showLoseMessage();
};
function startNextRound() {
roundNum++;
eatenItemsCount = 0;
gameBoard.showNextRoundMsg();
gameSpeed = Math.floor(gameSpeed * 0.8);
clearInterval(gameExecutor);
gameExecutor = setInterval(move,gameSpeed);
};
//***************************//
//*********SNAKE.js*********//
function BodyPart(xpos,ypos,direction) {
this.xPos=xpos;
this.yPos=ypos;
this.direction=direction;;
};
function Snake(startX,startY) {
var moveStep = 10;
var bodyParts = [new BodyPart(startX,startY,'right')];
var reverseDirections = {'right':'left','left':'right','up':'down','down':'up'};
var gameRegion;
var onCrashCallback;
var self = this;
this.eatFood = function() {
bodyParts.push(getNewTail());
};
this.move = function(newDirection) {
if(isReverseDirection(newDirection))
reverseBodyMove();
var newHead = getNewHead(newDirection);
if(crash(newHead))
onCrashCallback();
else{
for(var i = bodyParts.length-1; i>0 ;i--){
bodyParts[i] = bodyParts[i-1];
}
bodyParts[0] = newHead;
}
};
this.getBody = function() {
return bodyParts;
};
this.holdsPosition = function(xpos,ypos) {
for(var i = 0; i< bodyParts.length; i++){
if(bodyParts[i].xPos == xpos && bodyParts[i].yPos == ypos)
return true;
}
return false;
};
this.onCrash = function(crashCallback,fieldSize) {
gameRegion = fieldSize;
onCrashCallback = crashCallback;
};
var getNewHead = function(direction){
var currentHead = bodyParts[0];
switch(direction){
case 'right':
return new BodyPart(currentHead.xPos+moveStep,currentHead.yPos,direction);
case 'left':
return new BodyPart(currentHead.xPos-moveStep,currentHead.yPos,direction);
case 'up':
return new BodyPart(currentHead.xPos,currentHead.yPos-moveStep,direction);
case 'down':
return new BodyPart(currentHead.xPos,currentHead.yPos+moveStep,direction);
};
};
var getNewTail = function(){
var currentTail = bodyParts[bodyParts.length-1];
var tailDirection = currentTail.direction;
switch(tailDirection){
case 'right':
return new BodyPart(currentTail.xPos-moveStep,currentTail.yPos,tailDirection);
case 'left':
return new BodyPart(currentTail.xPos+moveStep,currentTail.yPos,tailDirection);
case 'up':
return new BodyPart(currentTail.xPos,currentTail.yPos+moveStep,tailDirection);
case 'down':
return new BodyPart(currentTail.xPos,currentTail.yPos-moveStep,tailDirection);
};
};
var crash = function(head){
if(head.xPos >= gameRegion.xPos
|| head.yPos >= gameRegion.yPos
|| head.xPos < 0
|| head.yPos < 0
|| self.holdsPosition(head.xPos,head.yPos))
return true;
return false;
};
var isReverseDirection = function(newDirection) {
var currentHeadDirection = bodyParts[0].direction;
return newDirection == reverseDirections[currentHeadDirection];
};
var reverseBodyMove = function() {
var tmpBodyPart;
var halfBodyLength = Math.floor(bodyParts.length/2);
var bodyLength = bodyParts.length -1;
for(var i = 0; i< halfBodyLength; i++){
tmpBodyPart = bodyParts[i];
bodyParts[i] = bodyParts[bodyLength - i];
bodyParts[bodyLength - i] = tmpBodyPart;
bodyParts[i].direction = reverseDirections[bodyParts[i].direction];
bodyParts[bodyLength - i].direction = reverseDirections[bodyParts[bodyLength - i]];
}
};
};
//**************************//
//*******GAMEBOARD.js******//
function GameBoard() {
this.drawElement = function (classname, xpos,ypos) {
var $element = $('<div/>').addClass(classname);
$element.css('top',ypos+'px').css('left',xpos+'px');
$('#gameField').append($element);
};
this.clearBoard = function(){
$('div.bodypart').remove();
$('.food').remove();
};
this.clearGameInfo = function() {
$('#score').html('0');
$('#loseMsg').hide();
$('#speed').html('1');
};
this.hasNoCreatedFood = function() {
return $('.food').length == 0 ;
};
this.removeSnakeBody = function() {
$('div.bodypart').remove();
};
this.removeSnakeFood = function() {
$('.food').remove();
};
this.updateScore = function(currentRound) {
var $currentScore = Number($('#score').html());
$currentScore+=currentRound;
$('#score').html($currentScore);
};
this.showLoseMessage = function(){
$('#loseMsg').fadeIn();
};
this.showNextRoundMsg = function() {
$('#nextRndMsg').hide().css({visibility: 'visible'}).fadeIn(2000);
$('#nextRndMsg').fadeOut(2000, function() {
$(this).show().css({visibility: 'hidden'});
});
var $currentSpeed = Number($('#speed').html());
$currentSpeed++;
$('#speed').html($currentSpeed);
};
}
//************************//