-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
131 lines (125 loc) · 4.09 KB
/
code.js
File metadata and controls
131 lines (125 loc) · 4.09 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
var cards = [];
var r_index = [];
var box_list = [];
var turn = 0;
var wait = false;
var pairs_found = 0;
var won_theme = document.getElementById("won_theme");
var ok_bip = document.getElementById("ok_bip");
var score = 0;
createBoxes();
cards_init();
timer = setInterval(boxes_setColor,500);
timer2 = setInterval(timer_change,2000);
// clearTimeout(timer);
function createBoxes(){
var new_box;
for(i = 0;i<20;i++){
new_box = document.createElement("div");
// new_box.className = "box";
new_box.style.backgroundColor = "rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
new_box.classList.add("box");
new_box.addEventListener("click", clicked_box);
document.getElementById("row"+(Math.floor(i/5)+1)).appendChild(new_box);
box_list.push(new_box);
}
}
function boxes_setColor(){
for(i = 0;i<20;i++){
box_list[i].style.backgroundColor = "rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
}
}
function timer_change(){
clearTimeout(timer);
timer = setInterval(boxes_setColor, Math.round(Math.random()*1000)+500);
}
function init_rindex(){
for(i = 0;i<20;i++){
r_index[i] = i;
}
}
function get_rindex(){
var ran = Math.round(Math.random()*(r_index.length-1));
var index = r_index[ran];
r_index.splice(ran,1);
return index;
}
function cards_init(){
init_rindex();
var index = 0;
for(i = 0;i<10;i++){
index = get_rindex();
cards[index] = i;
index = get_rindex();
cards[index] = i;
}
}
var disp_img = [];
var disp_img_num = [-1,-1];
function clicked_box(){
if(!start_game){
chronometer = setInterval(chronometer_fx, 100);
start_game = true;
}
if(!wait){
for(i = 0;i<20;i++){
if(box_list[i] == this){
if(disp_img_num[0] != i && disp_img_num[1] != i){
this.style.backgroundColor = "rgb(0,0,0)";
image = document.createElement("img");
image.src = "images/img"+cards[i]+".jpg";
image.width = 100;
image.height = 100;
this.appendChild(image);
disp_img[turn] = image;
disp_img_num[turn] = i;
if(turn == 0){
turn++;
}else{
turn = 0;
if(cards[i]==cards[disp_img_num[0]]){
box_list[i].removeEventListener("click", clicked_box);
box_list[disp_img_num[0]].removeEventListener("click", clicked_box);
pairs_found++;
console.log(pairs_found);
ok_bip.play();
score += stepfx(1200-chr_time);
if(pairs_found >= 10){
won_theme.play();
clearInterval(chronometer);
document.getElementById("timer_txt").innerHTML = "Time: "+chr_time/10+"s";
document.getElementById("score_txt").innerHTML = "Score: "+score;
}
}else{
bad_bip.play();
wait = true;
setTimeout(erase_img,1000);
}
}
}
break;
}
}
}
}
function erase_img(){
box_list[disp_img_num[0]].removeChild(disp_img[0]);
box_list[disp_img_num[1]].removeChild(disp_img[1]);
disp_img = [];
disp_img_num = [];
wait = false;
}
function stepfx(v){
if(v < 0){
v = 0;
}
return v;
}
var chr_time = 0;
var start_game = false;
var chronometer;
function chronometer_fx(){
chr_time++;
document.getElementById("timer_txt").innerHTML = "Time: "+chr_time/10+"s";
document.getElementById("score_txt").innerHTML = "Score: "+score;
}