-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
274 lines (220 loc) · 6.06 KB
/
main.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
// DISCLAIMER - THIS CODE WAS REMIXED FROM Kushagra Agarwal - FIND HIM AT -http://twitter.com/SolitaryDesigns
// If you ever see this Kushagra you are a G
// Randomly select and dynamically update audio source - EXTRA CODE Secttion added too Kushagra's orginal game
var foodMusicArr = [
"./audio/food/alrighty.mp3",
"./audio/food/big.mp3",
"./audio/food/bubblers.mp3",
"./audio/food/door.mp3",
"./audio/food/holdtight.mp3",
"./audio/food/listen.mp3",
"./audio/food/mydj.mp3",
"./audio/food/selecta.mp3",
"./audio/food/session.mp3",
"./audio/food/stinky.mp3",
"./audio/food/thisone.mp3"
];
var currentFoodMusic = "./audio/food/thisone.mp3";
function randomiseAudio () {
currentFoodMusic = foodMusicArr[Math.floor(Math.random() * foodMusicArr.length)];
}
randomiseAudio();
function updateSource() {
var audio = document.getElementById('food');
var source = document.getElementById('mp3Source');
source.src = currentFoodMusic;
audio.load();
}
updateSource();
//Preloading audio stuff
console.log(currentFoodMusic, 'current foodm usic')
var mainMusic = document.getElementById("main_music"),
foodMusic = document.getElementById("food"),
goMusic = document.getElementById("gameOver");
var files = [mainMusic, foodMusic, goMusic];
var counter = 0;
var start = document.getElementById("start"),
loading = document.getElementById("loading");
for(var i = 0; i < files.length; i++) {
var file = files[i];
console.log(file, i)
file.addEventListener("loadeddata", function() {
console.log('file loaded', file)
counter++;
var percent = Math.floor((counter/files.length)*100);
loading.innerHTML = "Loading " + percent + "%";
if(percent == 100) showButton();
});
}
function showButton() {
start.style.top = "30%";
loading.style.top = "100%";
}
//Initializing Canvas
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
//Full width and height
w = window.innerWidth,
h = window.innerHeight;
canvas.height = h;
canvas.width = w;
var reset, scoreText,menu, reMenu, score = 0;
function init() {
mainMusic.play();
menu.style.zIndex = "-1";
var snake,
size = 10,
speed = 25,
dir,
game_loop,
over = 0,
hitType;
//Custom funny gameover messages
var msgsSelf = [];
msgsSelf[0] = "Deadout";
msgsSelf[1] = "Check yourself";
var msgsWall = [];
msgsWall[0] = "Murked";
msgsWall[1] = "Devastating";
function paintCanvas() {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, w, h);
}
var Food = function(){
this.x = Math.round(Math.random() * (w - size) / size);
this.y = Math.round(Math.random() * (h - size) / size);
this.draw = function() {
ctx.fillStyle = "white";
ctx.fillRect(this.x*size, this.y*size, size, size);
}
}
var f = new Food();
//Initialize the snake
function initSnake() {
var length = 10;
snake = [];
for(var i = length - 1; i >= 0; i--) {
snake.push({x: i, y: 0});
}
}
function paintSnake() {
for(var i = 0; i < snake.length; i++) {
var s = snake[i];
ctx.fillStyle = "white";
ctx.fillRect(s.x*size, s.y*size, size, size);
}
}
function updateSnake() {
//Update the position of the snake
var head_x = snake[0].x;
var head_y = snake[0].y;
//Get the directions
document.onkeydown = function(e) {
var key = e.keyCode;
//console.log(key);
if(key == 37 && dir != "right") setTimeout(function() {dir = "left"; }, 30);
else if(key == 38 && dir != "down") setTimeout(function() {dir = "up"; }, 30);
else if(key == 39 && dir != "left") setTimeout(function() {dir = "right"; }, 30);
else if(key == 40 && dir != "up") setTimeout(function() {dir = "down"; }, 30);
if(key) e.preventDefault();
}
//Directions
if(dir == "right") head_x++;
else if(dir == "left") head_x--;
else if(dir == "up") head_y--;
else if(dir == "down") head_y++;
//Move snake
var tail = snake.pop();
tail.x = head_x;
tail.y = head_y;
snake.unshift(tail);
//Wall Collision
if(head_x >= w/size || head_x <= -1 || head_y >= h/size || head_y <= -1) {
if(over == 0) {
hitType = "wall";
gameover();
}
over++
}
//Food collision
if(head_x == f.x && head_y == f.y) {
coll = 1;
f = new Food();
var tail = {x: head_x, y:head_y};
snake.unshift(tail);
score += 10;
scoreText.innerHTML = "Score: "+score;
randomiseAudio();
updateSource();
foodMusic.pause();
foodMusic.currentTime = 0;
foodMusic.play();
//Increase speed
if(speed <= 45) speed ++;
clearInterval(game_loop);
game_loop = setInterval(draw, 1000/speed);
}
else {
//Check collision between snake parts
for(var j = 1; j < snake.length; j++) {
var s = snake[j];
if(head_x == s.x && head_y == s.y) {
if(over == 0) {
hitType = "self";
gameover();
}
over++;
}
}
}
}
function draw() {
paintCanvas();
paintSnake();
updateSnake();
//Draw food
f.draw();
}
reset = function() {
initSnake();
f = new Food();
reMenu.style.zIndex = "-1"
dir = "right";
over = 0;
speed = 30;
if(typeof game_loop != "undefined") clearInterval(game_loop);
game_loop = setInterval(draw, 1000/speed);
score = 0;
scoreText.innerHTML = "Score: "+score;
mainMusic.currentTime = 0;
mainMusic.play();
return;
}
function gameover() {
clearInterval(game_loop);
mainMusic.pause();
foodMusic.pause();
goMusic.play();
var tweet = document.getElementById("tweet");
tweet.href='http://twitter.com/share?url=http://77signalfromspace.co.uk&text=I scored ' +score+ ' points in the Techno remixed version of snake via @77signal_ - Techno Snake!';
//Get the gameover text
var goText = document.getElementById("info2");
//Show the messages
if(hitType == "wall") {
goText.innerHTML = msgsWall[Math.floor(Math.random() * msgsWall.length)];
}
else if(hitType == "self") {
goText.innerHTML = msgsSelf[Math.floor(Math.random() * msgsSelf.length)];
}
reMenu.style.zIndex = "1";
}
reset();
}
//Menus
function startMenu() {
menu = document.getElementById("menu");
reMenu = document.getElementById("reMenu");
scoreText = document.getElementById("score");
reMenu.style.zIndex = "-1"
}
startMenu();