-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeGame.js
239 lines (223 loc) · 7.28 KB
/
snakeGame.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
/*******************************
* *
* Project: Snake Game v2 *
* File: snakeGame.js *
* Version: 2.0 *
* *
*******************************/
window.onload = function() {
if(document.body.clientWidth <= 1920) {
var canvasWidth = 1800;
if(document.body.clientWidth <= 1680) { canvasWidth = 1500; }
if(document.body.clientWidth <= 1440) { canvasWidth = 1200; }
if(document.body.clientWidth <= 1024) { canvasWidth = 900; }
if(document.body.clientHeight <= 1080) {
var canvasHeight = 900;
if(document.body.clientHeight <= 900) { canvasHeight = 600; }
if(document.body.clientHeight <= 320) { canvasHeight = 300; }
}
}
var difficulty = document.location.hash === '#hardcore';
var blockSize = 30;
var game;
var delay = 100;
var snakee;
var applee;
var widthInBlocks = canvasWidth/blockSize;
var heightInBlocks = canvasHeight/blockSize;
var score;
var timeOut;
var k = 0;
function init() {
if(difficulty) { document.getElementById('sound').play(); }
canvas = document.createElement('canvas');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.backgroundColor = '#EEEEEE';
canvas.style.border = '1px solid #999999';
canvas.style.margin = ' 30px auto';
canvas.style.display = 'block';
document.body.appendChild(canvas);
game = canvas.getContext('2d');
snakee = new snake([[2, 0], [1, 0], [0, 0]], 'right');
applee = new apple([10, 10]);
score = 0;
update();
}
function hardcore() {
if(((score*5) === 100 || (score*5) === 200) && (delay === 100 || delay === 50)) { delay -= 25; }
if(k > 1) { k = 0; }
marg = [20, 20];
marg[k] += 1*score;
canvas.style.margin = marg[0]+'px 0px 0px '+marg[1]+'px';
return k++;
}
function update() {
snakee.progress();
if(snakee.collision()) { gameOver(); }
else {
if(snakee.eat(applee)) {
score++;
window.player['score'] = score*5;
snakee.eatApple = true;
do { applee.setNewPos(); }
while(applee.onSnake(snakee))
}
game.clearRect(0, 0, canvas.width, canvas.height);
snakee.draw();
applee.draw();
game.font = 'bold 20px Monospace';
game.fillStyle = '#222222';
game.textAlign = 'center';
game.fillText("SCORE : "+score*5, canvasWidth/2, canvasHeight-20)
game.fillText("APPLE EAT : "+score, canvasWidth/2, canvasHeight-5)
timeOut = setTimeout(update, delay);
if(difficulty) hardcore();
}
}
function gameOver() {
game.save();
game.font = 'bold 60px Monospace';
game.fillText("GAME OVER", canvasWidth/2, canvasHeight/2);
game.font = 'bold 20px Monospace';
game.fillText("PRESS SPACE TO RESTART", canvasWidth/2, (canvasHeight/2)+19);
game.fillText("OR ESCAPE TO CHANGE PLAYER", canvasWidth/2, (canvasHeight/2)+38);
game.font = 'bold 30px Monospace';
game.fillText("YOUR PLACE : "+window.player['place'], canvasWidth/2, (canvasHeight-50));
game.font = '15px Monospace';
game.textAlign = 'left';
game.fillText("> Snake Game 2.0", 5, 15);
game.fillText("> Game coded by 4N4RCHY", 5, 30);
if(difficulty) {
game.fillText("> Music composed by ROBORG", 5, 45);
game.fillText(" Midnight Murder (feat. NeoSlave)", 5, 60);
}
leaderBoard();
game.restore();
}
function leaderBoard(i, z) {
if(!z) { var z = 185; }
if(!i) { var i = 0; game.fillText("--- Top 10 Best Players ---", 25, canvasHeight-205); }
game.font = '15px Monospace';
game.textAlign = 'left';
game.fillText(leaderboard['player'][i], 5, canvasHeight-z);
game.fillText(leaderboard['score'][i], 205, canvasHeight-z);
z -= 20;
i++;
if(i < 10) { setTimeout(function() { leaderBoard(i, z); }, 25); }
}
function restart() {
snakee = new snake([[2, 0], [1, 0], [0, 0]], 'right');
applee = new apple([10, 10]);
delay = 100;
score = 0;
clearTimeout(timeOut);
update();
}
function drawBlock(game, pos) {
var x = pos[0]*blockSize;
var y = pos[1]*blockSize;
game.fillRect(x, y, blockSize, blockSize);
}
function snake(body, direction) {
this.body = body;
this.direction = direction;
this.eatApple = false;
this.draw = function() {
game.save();
game.fillStyle = '#AAAAAA';
for(var i = 0; i < this.body.length; i++) { drawBlock(game, this.body[i]); }
game.restore();
}
this.progress = function() {
var nextPos = this.body[0].slice();
switch(this.direction) {
case 'left': nextPos[0]--; break;
case 'right': nextPos[0]++; break;
case 'down': nextPos[1]--; break;
case 'up': nextPos[1]++; break;
default: throw('ERROR PROGRESS DIRECT');
}
this.body.unshift(nextPos);
if(!this.eatApple) { this.body.pop(); }
else { this.eatApple = false; }
}
this.setDirect = function(newDirect) {
var allowDirect;
switch(this.direction) {
case 'left':
case 'right': allowDirect = ['up', 'down']; break;
case 'down':
case 'up': allowDirect = ['left', 'right']; break;
default: throw('ERROR SET DIRECT');
}
if(allowDirect.indexOf(newDirect) > -1) { this.direction = newDirect; }
}
this.collision = function() {
var wallCollide = false;
var snakeCollide = false;
var horizonWall = this.body[0][0] < 0 || this.body[0][0] > widthInBlocks-1;
var verticalWall = this.body[0][1] < 0 || this.body[0][1] > heightInBlocks-1;
if(horizonWall || verticalWall) { wallCollide = true; }
for(var i = 0; i < this.body.slice(1).length; i++) {
if(this.body[0][0] === this.body.slice(1)[i][0] && this.body[0][1] === this.body.slice(1)[i][1]) { snakeCollide = true; }
} return wallCollide || snakeCollide;
}
this.eat = function(appleEat) {
var head = this.body[0];
if(head[0] === appleEat.position[0] && head[1] === appleEat.position[1]) { return true; }
return false;
}
}
function apple(pos) {
this.position = pos;
this.draw = function() {
game.save();
game.fillStyle = '#3DAB92';
game.beginPath();
var radius = blockSize/2;
var x = this.position[0]*blockSize+radius;
var y = this.position[1]*blockSize+radius;
game.arc(x, y, radius, 0, Math.PI*2, true);
game.fill();
game.restore();
}
this.setNewPos = function() {
var newX = Math.round(Math.random()*(widthInBlocks-1));
var newY = Math.round(Math.random()*(heightInBlocks-1));
this.position = [newX, newY];
}
this.onSnake = function(checkSnake) {
var onSnake = false;
for(var i = 0; i < checkSnake.body.length; i++) {
if(this.position[0] === checkSnake.body[i][0] && this.position[1] === checkSnake.body[i][1]) { onSnake = true; }
} return onSnake;
}
}
document.onkeydown = function handleKey(e) {
var key = e.keyCode;
var newDirect;
switch(key) {
case 27: document.location.href = './'; break;
//case 32: restat(); break;
case 32: document.location.href = './?player='+window.player['name']+'&score='+window.player['score']; break;
case 37: newDirect = 'left'; break;
case 38: newDirect = 'down'; break;
case 39: newDirect = 'right'; break;
case 40: newDirect = 'up'; break;
default: return;
}
snakee.setDirect(newDirect);
}
init();
}
function snake(x) {
switch(x) {
case 0: document.location.hash = "#easy"; document.location.reload(); break;
case 1: document.location.hash = "#hardcore"; document.location.reload(); break;
default: console.log('snake() --- HELP COMMAND\nsnake(0) --- EASY MOD\nsnake(1) --- HARDCORE MOD'); break;
} return;
}
/******
* END *
******/