-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrpt.js
152 lines (137 loc) · 3.34 KB
/
scrpt.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
const container = document.getElementById('container');
const r1 = document.getElementById('r-1');
const r2 = document.getElementById('r-2');
const r3 = document.getElementById('r-3');
const r4 = document.getElementById('r-4');
const r5 = document.getElementById('r-5');
const tvCount = document.getElementById('textView');
const play = document.getElementById('play');
const refresh = document.getElementById('refresh');
var count = 0;
var arr = [0,0,0,0,0];
var maxCount = 0;
var stopp = false;
function genKeyCode(){
return (Math.floor(Math.random()*20) + 65);
}
function updateCount(){
tvCount.innerHTML = "Score: "+ count;
if(count>maxCount){
maxCount = count;
}
}
function gameOver(){
if(count<0){
alert("\tGame Over!\n Your Max Score is"+maxCount);
window.location.reload();
}
}
function rmvChild(ind){
if(ind == 0){
if(r1.childElementCount >= 1){
r1.removeChild(r1.children[0]);
}
}
else if(ind == 1){
if(r2.childElementCount >= 1){
r2.removeChild(r2.children[0]);
}
}
else if(ind == 2){
if(r3.childElementCount >= 1){
r3.removeChild(r3.children[0]);
}
}
else if(ind == 3){
if(r4.childElementCount >= 1){
r4.removeChild(r4.children[0]);
}
}
else if(ind == 4){
if(r5.childElementCount >= 1){
r5.removeChild(r5.children[0]);
}
}
}
function check(prssdKey){
for(i=0; i<arr.length; i++){
if(arr[i] == prssdKey){
arr[i] = 0;
count++;
rmvChild(i);
}
}
updateCount();
gameOver();
}
function apndChild(num, newLetter){
if(num<=65){
rmvChild(0);
r1.appendChild(newLetter);
if(arr[0]!=0){
count--;
updateCount();
}
arr[0] = num;
}
else if(num<=70 && num>65){
rmvChild(1);
r2.appendChild(newLetter);
if(arr[1]!=0){
count--;
updateCount();
}
arr[1] = num;
}
else if(num<=75 && num>70){
rmvChild(2);
r3.appendChild(newLetter);
if(arr[2]!=0){
count--;
updateCount();
}
arr[2] = num;
}
else if(num<=80 && num>75){
rmvChild(3);
r4.appendChild(newLetter);
if(arr[3]!=0){
count--;
updateCount();
}
arr[3] = num;
}
else if(num<=85 && num>80){
rmvChild(4);
r5.appendChild(newLetter);
if(arr[4]!=0){
count--;
updateCount();
}
arr[4] = num;
}
gameOver();
}
function createElmnt(){
let newLetter = document.createElement('div');
let cpdKBD = document.createElement('kbd');
let num = genKeyCode();
let letter = String.fromCharCode(num);
cpdKBD.textContent= letter;
newLetter.appendChild(cpdKBD);
newLetter.classList.add('key');
apndChild(num, newLetter);
// const audio = document.getElementById('A');
// audio.currentTime = 0;
// audio.play();
}
play.addEventListener('click', function(){
stopp = true;
window.setInterval("createElmnt()", 2000);
});
refresh.addEventListener('click', function(){
window.location.reload();
});
window.addEventListener('keydown', function(e){
check(e.keyCode);
});