-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
144 lines (122 loc) · 3.39 KB
/
main.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
const space = document.querySelector('.game-space');
const cal = space.getBoundingClientRect();
const start = document.querySelector('.start');
const stop = document.querySelector('.stop');
const time = document.querySelector('.time');
const pop = document.querySelector('.pop-up');
const replay = document.querySelector('.replay');
const score = document.querySelector('.game-score');
const fishes = document.querySelectorAll('.fish');
const message = document.querySelector('.message');
const lastTime = 4;
let clock;
let points = 5;
//character info
const char1 = "fish";
const char2 = "shark";
const char1_num = 5;
const char2_num = 5;
const char1_imgPath = 'img/fish.png';
const char2_imgPath = 'img/shark.png';
const char1_width = 50;
const char2_width = 80;
const rule = document.querySelector(".rule");
const ruleClose = document.querySelector(".close");
const ruleOpen = document.querySelector(".ruleBtn");
ruleClose.addEventListener("click", () => {
rule.style.display = "none";
})
ruleOpen.addEventListener("click", () => {
rule.style.display = "block";
})
// 게임시작
start.addEventListener('click', () => {
start.style.display = 'none';
stop.classList.remove('stop-hide');
gameStart();
});
// 게임정지
stop.addEventListener('click', () => {
gameStop();
});
// 게임재시작
replay.addEventListener('click', () => {
pop.classList.add('hide');
gameStart();
});
// 점수내기
space.addEventListener('click', (e) => {
if (e.target.className === char1) {
e.target.remove();
points--;
score.innerHTML = points;
if (points === 0) {
gameStop();
message.innerHTML = 'You Won!';
}
} else if (e.target.className == char2) {
e.target.remove();
gameStop();
message.innerHTML = 'You Lost!';
}
});
// 게임준비
function spacing() {
objects(char1, char1_num, char1_imgPath);
objects(char2, char2_num, char2_imgPath);
}
function objects(name, number, src) {
const width = cal.width - 80;
const height = cal.height - 80;
for (let i = 0; i < number; i++) {
const object = document.createElement('img');
object.classList.add(name);
object.setAttribute('src', src);
object.style.position = 'absolute';
const x = random(0, width);
const y = random(20, height);
object.style.left = `${x}px`;
object.style.top = `${y}px`;
space.appendChild(object);
}
}
function random(min, max) {
return Math.random() * (max - min) + min;
}
// 타이머
function timer() {
let last = lastTime;
let min = parseInt(last / 60);
let sec = last % 60;
time.innerHTML = min + ':' + sec;
clock = setInterval(function () {
--last;
min = parseInt(last / 60);
sec = last % 60;
time.innerHTML = min + ':' + sec;
if (last <= 0) {
clearInterval(clock);
// time.innerHTML = 'OVER';
message.innerHTML = 'You Lost!';
gameStop();
}
}, 1000);
}
// 게임시작
function gameStart() {
space.classList.add("active");
time.innerHTML = 'Ready';
timer();
spacing();
score.innerHTML = points;
}
// 게임중지
function gameStop() {
space.innerHTML = '';
space.classList.remove("active");
pop.classList.remove('hide');
clearInterval(clock);
time.innerHTML = 'over';
points = 5;
}
// game rule