-
Notifications
You must be signed in to change notification settings - Fork 0
/
hitTheNote.js
360 lines (316 loc) · 11.6 KB
/
hitTheNote.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
var canvas = document.getElementById('canvasID');
var context = canvas.getContext('2d');
var selectBox = document.getElementById('selectBox');
let frame = 100;
const cMajor = ["C", "D", "E", "F", "G", "A", "B"];
const cWrongNote = ["C#", "Dd", "D#", "Ed", "F#", "Gd", "G#", "Ad", "A#", "Bd"];
const dMajor = ["D", "E", "F#", "G", "A", "B", "C#"];
const dWrongNote = ["D#", "Ed", "F", "Gd", "G#", "Ad", "A#", "Bd", "C", "Dd"];
//["C", "C#", "Dd", "D", "D#", "Ed", "E", "F", "F#", "Gd", "G", "G#", "Ad", "A", "A#", "Bd", "B"];
var gameOver = false;
let score = 0;
let countWrongNotehit = 0;
let selectedRightNote = [];
let selectedWrongNote = [];
let trackStartBtnClick = 0;
const audio = {
eat : new Audio("audio/eatSound.mp3"),
shoot : new Audio("audio/shootSound.mp3"),
worng : new Audio("audio/wrong.wav"),
gameOver : new Audio("audio/gameOverSound.wav"),
background : new Audio("audio/background.mp3"),
}
// button click left, right, up
const buttonClick = {
left : false,
right : false,
up : false,
};
function screenSize(){// adjust according to screenSize
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
}
window.addEventListener('keydown', function(event){
if(event.key == 'ArrowLeft'){
buttonClick.left = true;
}
else if(event.key == 'ArrowRight'){
buttonClick.right = true;
}
else if(event.key == 'ArrowUp'){
buttonClick.up = true;
}
});
window.addEventListener('keyup', function(){
buttonClick.left = false;
buttonClick.right = false;
buttonClick.up = false;
});
class Player{
constructor() {
this.x = canvas.width/2;
this.y = canvas.height;
this.angle = -90;
this.length = 80;
this.radius = 40;
this.moveToX; // moving X direction of player gun
this.moveToY; // moving Y direction of player gun
}
update(){
// increasing or decreasing angle of gun
if(buttonClick.left & this.angle >= -160){
this.angle -= 10;
buttonClick.left = false;
}
else if(buttonClick.right & this.angle <= -20){
this.angle += 10;
buttonClick.right = false;
}
// calculating the gun facing direction
this.moveToX = this.x + Math.cos(Math.PI * this.angle / 180) * this.length;
this.moveToY = this.y + Math.sin(Math.PI * this.angle / 180) * this.length;
}
draw(){
// ball gun
context.beginPath();
context.moveTo(this.moveToX, this.moveToY);
context.lineTo(this.x, this.y);
context.stroke();
context.closePath();
//player ball
drawBubble(this.x, this.y, this.radius, 0, Math.PI, 'red', true);
}
}
const bulletArray=[];
class Bullet{
constructor(){
this.x = player.moveToX;//player.x
this.y = player.moveToY;//player.y
this.radius = 20;
this.speed = 10;
this.angle = Math.abs(player.angle); // abs change the negitive number to positive number
this.velocityX = this.speed*Math.cos(Math.PI*this.angle/180);
this.velocityY = this.speed*Math.sin(Math.PI*this.angle/180);
this.distance = 0; // distance travel by each bullet
}
update(){
if(this.x+this.radius > canvas.width || this.x <= 0+this.radius){ // if bullet touch left and right wall
this.velocityX = -this.velocityX;
}
this.x += this.velocityX;
this.y += -this.velocityY;
}
draw(){
drawBubble(this.x, this.y, this.radius, 0, Math.PI*2, 'black', false);
// text inside circle 'bullet'
writeText(noteName,"", this.x-10, this.y+15, 35, 'white');
}
}
// it shoot (darw and update) the black bubble in every up array click
function shootBullet(){
if(buttonClick.up){ // create new buttet in every up arrow click
audio.shoot.play();
audio.shoot.currentTime = 0;
bulletArray.push(new Bullet());
}
buttonClick.up = false;
for(let i = 0; i<bulletArray.length; i++){ // iterating over bullet array
bulletArray[i].update();
bulletArray[i].draw();
bulletArray[i].distance++; // increating distance of every bullet
}
for(let i = 0; i<bulletArray.length; i++){ // removing bullets from array
if(bulletArray[i].y < 0 || bulletArray[i].distance >= 160 || bulletArray.length >= 5){
bulletArray.splice(i,1);
}
}
}
let rightNotesArray = [];
class RightNote{
constructor() {
this.x = Math.random() * canvas.width;
this.y = 0;
this.radius = 30;
this.speed = 2;
this.random = Math.floor(Math.random()*selectedRightNote.length);
this.note = selectedRightNote[this.random];
}
update(){ this.y += this.speed; }
draw(){
drawBubble(this.x, this.y, this.radius, 0, Math.PI*2, 'orange', false);
// text inside circle 'bullet'
writeText(this.note,"", this.x-10, this.y+15, 35, 'white');
}
}
let wrongNotesArray = [];
class WrongNote{
constructor() {
this.x = Math.random()*canvas.width;
this.y = 0;
this.radius = 30;
this.speed = 2;
this.random = Math.floor(Math.random()*selectedWrongNote.length); // selectedWrongNote value is getting from getSelectedNote() function;
this.note = selectedWrongNote[this.random];
}
update(){ this.y += this.speed; }
draw(){
drawBubble(this.x, this.y, this.radius, 0, Math.PI*2, 'Green', false);
// text inside circle 'bullet'
writeText(this.note,"", this.x-20, this.y+10, 35, 'white');
}
}
// handle display of wrong and right note moving from top to buttom
let turn = ["wrongNoteTurn", "rightNoteTurn"];
function handleNotes(){
if(frame % 100 == 0){
if(turn[Math.floor(Math.random()*turn.length)] == "wrongNoteTurn"){
wrongNotesArray.push(new WrongNote());
}else{
rightNotesArray.push(new RightNote());
}
}
//---------for right notes-------------------------//
for(var j = 0; j < rightNotesArray.length; j++){
rightNotesArray[j].update();
rightNotesArray[j].draw();
}
// removing notes that crosses canvas height
for(var j = 0; j < rightNotesArray.length; j++){
if(rightNotesArray[j].y >= canvas.height){ // if right note "orange bubble" cross canvas height
score --;
rightNotesArray.splice(j, 1);
if(score < -2 ){ gameOver = true;}
}
}
//---------for wrong notes-------------------------//
for(var k = 0; k < wrongNotesArray.length; k++){
wrongNotesArray[k].update();
wrongNotesArray[k].draw();
}
// removing notes that crosses canvas height
for(var k = 0; k < wrongNotesArray.length; k++){
if(wrongNotesArray[k].y >= canvas.height){
wrongNotesArray.splice(k, 1);
}
}
}
// if bullet "black bubble" hit right note "orange bubble" or wronng note "green bubble";
function hitNote(){
let bulletX, bulletY, bulletR;
let rightNoteX, rightNoteY, rightNoteR;
let wrongNoteX, wrongNoteY, wrongNoteR;
let distanceBulletAndRightNote, distanceBulletAndWrongNote;
//for bullet
for(let i = 0; i < bulletArray.length; i++){
bulletX = bulletArray[i].x;
bulletY = bulletArray[i].y;
bulletR = bulletArray[i].radius;
//for rightNote
for(let j = 0; j <rightNotesArray.length; j++){
rightNoteX = rightNotesArray[j].x;
rightNoteY = rightNotesArray[j].y;
rightNoteR = rightNotesArray[j].radius;
// distance btn bullet and right note
distanceBulletAndRightNote = calculateDistance(bulletX, bulletY, rightNoteX, rightNoteY);
// if distance btn bullet "black bubble" is less then right note "orange bubble" / bullet "black bubble" radius
if(distanceBulletAndRightNote < bulletR || distanceBulletAndRightNote < rightNoteR){
score ++;
audio.eat.play();
rightNotesArray.splice(j, 1);
bulletArray.splice(i, 1);
}
}
//for wrong Note
for(let k = 0; k < wrongNotesArray.length; k++){
wrongNoteX = wrongNotesArray[k].x;
wrongNoteY = wrongNotesArray[k].y;
wrongNoteR = wrongNotesArray[k].radius;
// distance btn bullet and wrong note
distanceBulletAndWrongNote = calculateDistance(bulletX, bulletY, wrongNoteX, wrongNoteY);
// if distance btn bullet "black bubble" is less then wrong note "green bubble" / bullet "black bubble" radius
if(distanceBulletAndWrongNote < bulletR || distanceBulletAndWrongNote < wrongNoteR){
audio.worng.play();
countWrongNotehit ++;
wrongNotesArray.splice(k, 1);
bulletArray.splice(i, 1);
// if hit wrong note 3 time
if(countWrongNotehit >= 3){ gameOver = true; }
}
}
}
}
var noteName = "";
function getSelectedNote(){
var noteArrayName = selectBox.value;
let notesStore = {"cMajor" : [cMajor, cWrongNote], "dMajor" : [dMajor, dWrongNote]};
selectedRightNote = notesStore[noteArrayName][0];
selectedWrongNote = notesStore[noteArrayName][1];
// it helps to witer choosen note name in black bubble
if(noteArrayName == "cMajor"){ noteName = "C"; }
else if(noteArrayName == "dMajor"){ noteName = "D"; }
}
function calculateDistance(x1,y1,x2,y2){
let dx = x2 - x1;
let dy = y2 - y1;
let distance = Math.sqrt(dx*dx + dy*dy);
return distance;
}
function drawBubble(x, y, radius, startAngle, endAngel, color, anticlockwise){
context.beginPath();
context.fillStyle = color;
context.arc(x, y, radius, startAngle, endAngel, anticlockwise);
context.fill();
context.stroke();
context.closePath();
}
function writeText(title, skore, x, y, fontSize, color){
context.beginPath();
context.font = fontSize+"px Georgia";
context.fillStyle = color;
context.fillText(title+skore, x ,y);
context.closePath();
}
function animate(){
frame ++;
context.clearRect(0,0, canvas.width, canvas.height);
player.update();
player.draw();
shootBullet(); // shoot black bubble
handleNotes(); // darw and update green and orange bubble "wrong and right note"
hitNote(); // remove the bubble that strike with each other
writeText("Hit The Right Note","", canvas.width/2-200, 60, 50, 'black');
//scoreboard
writeText("Wrong Note : ", countWrongNotehit, 250, canvas.height-10, 50, 'black');
writeText("Score : ", score, 0, canvas.height-10, 50, 50, 'black');
if(!gameOver){
requestAnimationFrame(animate);
}else{
audio.gameOver.play();
audio.background.pause();
audio.background.currentTime = 0; // it helps to make audio to 0:00
writeText("Game Over","", canvas.width/2-150, canvas.height/2, 60, 'red');
writeText("Highest Score : ","--", canvas.width/2-200 , canvas.height/2+100, 60, 'red');
}
}
function start(){
audio.background.play();
if(trackStartBtnClick == 0){
trackStartBtnClick = 1;
gameOver = false;
score = 0 ;
countWrongNotehit = 0;
getSelectedNote();
animate();
}
}
function stop(){
trackStartBtnClick = 0;
gameOver = true;
rightNotesArray = [];
wrongNotesArray = [];
bulletArray = [];
}
screenSize();
const player = new Player();
writeText("Hit The Right Note","", canvas.width/2-200, 60, 50, 'black');
writeText("Press play button to start game","", canvas.width/4, canvas.height/2, 50,'black');