-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
230 lines (213 loc) · 5.9 KB
/
Copy pathmain.js
File metadata and controls
230 lines (213 loc) · 5.9 KB
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
let x = 0;
let score = 0;
let time = 0;
let setTM1 = 0;
let Min_ = -20;
let Max_ = 20;
let canvas = document.getElementById("out");
let ctx = canvas.getContext("2d");
ctx.font = "45px Comic Sans MS";
ctx.textAlign = "center";
let hightScore = 0;
let songs = document.getElementById("effectSound");
let pause_ = 10;
let err = 0;
let ColorA = 255;
let ColorB = 0;
resetTime();
new1();
Time();
// đặt thời gian kết thúc
function resetTime() {
time = 10;
}
function restMinMax() {
Min_ = -20;
Max_ = 20;
}
//hàm đếm ngược
function Time() {
timeOut();
var setTM = setInterval(timeOut, 1000);
setTM1 = setTM;
function timeOut() {
if (time > -1) {
document.getElementById("time").innerHTML = " " + time;
time--;
}
if (time == 0) {
document.getElementById("BtnPause").style.display = "none";
}
if (time == -1) {
resetTime();
gameOver();
}
}
}
//hàm tạo phép toán mới
function new1() {
var a = rand(Min_, Max_);
var b = rand(Min_, Max_);
var Color = "rgb(" + ColorB + " ," + ColorA + ",0)";
let result = 0;
var Case = rand(1, 4);
ColorA -= 5;
ColorB += 5;
//tạo kết quả fake
function resetRe() {
var result_fake = rand(Min_ - 20, Max_ + 20);
return result_fake;
}
//kiểm tra âm dương
function check_abs(a) {
var x = "";
if (a < 0) x = "(" + a + ")";
else x = a;
return x;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
switch (Case) {
case 1:
var bth_ = check_abs(a) + " + " + check_abs(b);
ctx.fillText(bth_, canvas.width / 2, canvas.height / 2 + 11);
result = a + b;
break;
case 2:
var bth_ = check_abs(a) + " - " + check_abs(b);
ctx.fillText(bth_, canvas.width / 2, canvas.height / 2 + 11);
result = a - b;
break;
case 3:
var bth_ =
check_abs(Math.floor(a / 2)) + " x " + check_abs(Math.floor(b / 2));
ctx.fillText(bth_, canvas.width / 2, canvas.height / 2 + 11);
result = Math.floor(a / 2) * Math.floor(b / 2);
break;
}
x = result;
switch (rand(1, 4)) {
case 1:
document.getElementById("var1").innerHTML = result;
document.getElementById("var2").innerHTML = resetRe();
document.getElementById("var3").innerHTML = resetRe();
break;
case 2:
document.getElementById("var1").innerHTML = resetRe();
document.getElementById("var2").innerHTML = result;
document.getElementById("var3").innerHTML = resetRe();
break;
case 3:
document.getElementById("var1").innerHTML = resetRe();
document.getElementById("var2").innerHTML = resetRe();
document.getElementById("var3").innerHTML = result;
break;
}
document.getElementById("score").innerHTML = " " + score;
if (hightScore > 0) {
document.getElementById("hightScore").style.display = "block";
document.getElementById("hightScore").innerHTML = " " + hightScore;
} else {
document.getElementById("hightScore").style.display = "none";
}
document.getElementById("container").style.background = Color;
}
// bắt sự kiện phím
function Key(event) {
var x = event.key;
if (x == 1 && pause_ != 0 && pause_ != 1) check(1);
else if (x == 2 && pause_ != 0 && pause_ != 1) check(2);
else if (x == 3 && pause_ != 0 && pause_ != 1) check(3);
else if (x == "m") playSong();
else if (x == " " && (pause_ == 0 || pause_ == 1)) {
play_again();
pause_ = 10;
} else if (x == "p" && pause_ != 0 && time > 0) {
if (pause_ != 1) {
pause();
pause_ = 1;
} else Continue();
}
console.log(pause_);
}
//chơi lại từ đầu
function play_again() {
score = 0;
resetTime();
restMinMax();
new1();
Time();
document.getElementById("BtnPause").style.display = "block";
if (pause_ == 0 && err == 1) {
document.getElementById("show-task").id = "end-task";
} else if (pause_ == 1 || pause_ == 0) {
document.getElementById("continue").id = "pause";
}
}
// continue
function Continue() {
Time();
pause_ = 10;
if (time > 0 && pause_ == 10) {
document.getElementById("BtnPause").style.display = "block";
}
document.getElementById("continue").id = "pause";
}
// pause
function pause() {
clearInterval(setTM1);
document.getElementById("pause").id = "continue";
document.getElementById("BtnPause").style.display = "none";
pause_ = 1;
document.getElementById("score2").innerHTML = "Score: " + score;
}
//kiểm tra các lựa chọn
function check(index) {
index = "var" + index;
var a = document.getElementById(index).innerHTML;
if (a == x) {
document.getElementById("true").play();
score++;
if (score % 10 == 0) {
Min_ -= 10;
Max_ += 10;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
new1();
resetTime();
} else {
gameOver();
}
}
// game over
function gameOver() {
if (score > hightScore) {
hightScore = score;
document.getElementById("score1").innerHTML = "Hight Score: " + score;
} else {
document.getElementById("score1").innerHTML = "Score: " + score;
}
document.getElementById("false").play();
document.getElementById("end-task").id = "show-task";
document.getElementById("BtnPause").style.display = "none";
err = 1;
pause_ = 0;
ColorA = 255;
ColorB = 0;
clearInterval(setTM1);
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// effecrSound
function playSong() {
console.log(songs.paused);
if (songs.paused === true) {
songs.play();
songs.volume = 0.3;
document.getElementById("btnMute").className = "fas fa-music";
} else {
songs.pause();
document.getElementById("btnMute").className = "fas fa-volume-mute";
}
}
function rand(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}