-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
179 lines (130 loc) · 3.4 KB
/
script.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
const MIN_TIME = 800; //milliseconds
const MAX_TIME = 1000; //milliseconds
const PLAY_TIME = 30; //seconds
let height = 0;
let highScore = 0;
let timeUp = true;
let lastHole = 1000;
let myScore = 0;
$("#startBtn").click(start);
let images = document.body.querySelectorAll('.mole');
let holes = document.body.querySelectorAll('.mole');
scoreStart();
$('.hammer').animate({
'opacity': 0
})
function start(){
if(timeUp){
timeUp = false;
myScore = 0;
updateScore();
/* photos hide */
height = document.body.getElementsByClassName('img-holder')[0].clientHeight;
$('.mole').css("transform", "translateY(" + (height+10) + "px)");
$('#time').text(PLAY_TIME);
moleUp();
countdown();
}
else{
console.log('Game is still running!');
}
}
function updateScore(){
$('#myScore').text(myScore);
$('#highScore').text(highScore);
}
function redEffect(image){
image.parentNode.childNodes[1].classList.add('mask')
setTimeout(()=>{
image.parentNode.childNodes[1].classList.remove('mask')
}, 75)
setTimeout(()=>{
image.parentNode.childNodes[1].classList.add('mask')
}, 150)
setTimeout(()=>{
image.parentNode.childNodes[1].classList.remove('mask')
}, 300)
}
function hammerEffect(target){
console.log(target.parentNode);
console.log(target.parentNode.offsetTop);
console.log(target.parentNode.offsetLeft);
let height = $('.img-holder')[0].clientHeight;
let width = $('.img-holder')[0].clientWidth;
hammer = $('.hammer').first()
hammer.height(height);
hammer.animate({
'left': target.parentNode.offsetLeft + width*3/4,
'top': target.parentNode.offsetTop - height/2
}, 0)
hammer.animate({
'opacity': 1
}, 0)
hammer.animate({
'left': target.parentNode.offsetLeft + width/4,
'top': target.parentNode.offsetTop + height/2
}, 200, 'swing')
hammer.animate({
'opacity': 0
}, 200)
}
function scoreStart(){
$('.mole').click(function(event){
console.log(event.target);
if(!timeUp){
myScore += 1;
//redEffect(event.target);
hammerEffect(event.target);
moleDown(event.target);
updateScore();
}
})
}
function randomTime(min, max){
return Math.random()*(max - min) + min;
}
function randomHoles(){
let id = Math.floor(Math.random() * $('.mole').length);
let hole = $('.mole')[id];
if(hole == lastHole){
return randomHoles();
}
lastHole = hole;
return hole;
}
function finish(){
$('#time').text("Time Up");
if(myScore > highScore){
highScore = myScore;
}
updateScore();
alert('Your score is: '+ myScore);
timeUp = true;
}
function countdown(){
seconds = $('#time').text();
seconds--;
if(seconds>0){
$('#time').text(seconds);
}
else{
finish();
return
}
setTimeout(countdown, 1000);
}
function moleUp(){
let time = randomTime(MIN_TIME, MAX_TIME);
let hole = randomHoles();
//hole.css("transform", "translateY(0px)")
hole.style.transform = "translateY(20px)";
setTimeout(() => {
if (!timeUp){
moleDown(hole);
moleUp();
}
}, time)
}
function moleDown(hole){
hole.style.transform = "translateY(" + height + "px)";
}