μ²μ μμ± μ½λ
μ΄μ νμ΄μμ μ΄μ΄μ
κ²μ μμ λ²νΌμ ν΄λ¦νλ©΄
1) field λ΄λΆκ° 리μ λ ν, νλ μμ λΉκ·Ό 10κ°κ³Ό λ²λ 7λ§λ¦¬κ° λλ€ν μμΉμ λ°°μΉλμ΄μΌ νλ€.
2) νμ΄λ¨Έκ° μλν΄μΌ νλ€. (10μ΄λΆν° μμ)
3) ν΄λ¦ κ°λ₯ νμκ° 10μΌλ‘ λ°λμ΄μΌ νλ€.
4) κ²μ μμ λ²νΌ μμ μμ΄μ½μ΄ μ€μ§ μμ΄μ½μΌλ‘ λ°λμ΄μΌ νλ€.
λ°λ‘ μμλ€. μλμ κ°μ΄ κ·Έλ₯ λ°λ‘ startBtn μ΄λ²€νΈ 리μ€λ μμ if ꡬ문μ λ§λ€μ΄μ μ μ¨μ€¬μ.
startBtn.addEventListener('click', function startTimer () {
if (document.querySelector('i[class$="stop"]') === null) { // κ²μ μμ μ , κ²μ μμ
placeImgRandomly();
timer.innerHTML = `0:10`;
startWorkingTimer();
clickLimit.innerHTML = 10;
bg_mp3.play();
startBtn.innerHTML = '<i class="fas fa-stop"></i>';
} else { // κ²μ μμ ν, κ²μ μ μ§
/* μ½λ μ€λ΅ */
}
});
κ²μ μνλ₯Ό κΈ°μ΅νλ μ μ λ³μ
λ₯Ό μΆκ°νλ€. (started, timer, limit) κ²μ μ§ν μνμ λ°λΌ κ·Έ κ°μ λ°κΏμ€μΌ νλ λ³μμ΄λ―λ‘ letμ μ¬μ©ν΄ global scopeμ μ μΈν΄μΌ νλ€.
startBtnμ μ΄λ²€νΈ 리μ€λ λ³Έλ¬Έμ μμ νλ€.
if ꡬ문μ 쑰건μμ, started λ³μλ₯Ό μ΄μ©ν΄ λ°κΏ¨λ€.
if (document.querySelector('[class$="stop"]')
β if (started)
μ¬κΈ°μ κΈ° μ¨λμ μ½λλ€μ μμ λ° μ λ¦¬ν΄ global scopeμ startGame ν¨μμ stopGame ν¨μλ₯Ό λ§λ€μλ€.
fieldλ₯Ό 리μ
νκΈ° μν΄ μ μλ μ½λλ₯Ό κ°κ²°νκ² λ°κΏ¨λ€. μλ μ½λλ₯Ό field.innerHTML = ''
λ‘ λ°κΏμ 'λΉκ·Ό, λ²λ μ΄λ―Έμ§ λλ€ λ°°μΉ ν¨μ μ'μ μ½μ
ν¨. μ΅κ³ λ‘ λΉν¨μ¨μ μ΄μ
// λΉκ·Ό, λ²λ μ κ±° & replay μ°½ μ κ±°
function init () {
const carrots = document.querySelectorAll('img[alt="carrot"]');
const bugs = document.querySelectorAll('img[alt="bug"]');
for (let i = 0; i < carrots.length; i++) {
carrots[i].remove();
}
for (let i = 0; i < bugs.length; i++) {
bugs[i].remove();
}
const result = document.querySelector('.result');
result.remove();
}
[TIL] 211001 μ°Έκ³
π λ΄κ° μ²μ μμ±ν μ½λ
// κ° μ΄λ²€νΈ 리μ€λ ν¨μμ μ μ΄μ€
timer.innerHTML = `0:10`;
// νμ΄λ¨Έ κ΄λ ¨
let time = 9;
let decreNum;
function startWorkingTimer () {
decreNum = setInterval(function () {
const second = time % 60;
timer.innerHTML = `0:${second}`;
time--;
if (time < 0) {
clearInterval(decreNum);
bg_mp3.pause();
startBtn.style.visibility = 'hidden';
displayResult('YOU LOST π©');
}
}, 1000);
}
π κ°μλ₯Ό μ°Έκ³ ν΄ μ²μ μμ ν μ½λ
// νμ΄λ¨Έ μμ ν¨μ
function startTimer() {
let remainingSec = 10;
const decNum = setInterval(function () {
let minute = Math.floor(remainingSec / 60);
let second = remainingSec % 60;
timer.innerHTML = `${minute}:${second}`;
remainingSec--;
}, 1000);
}
π μμ λ° μμ±
displayTime ν¨μλ₯Ό setInterval λ©μλλ³΄λ€ λ¨Όμ μ¨μ€μΌ, κ²μ μμ ν 1μ΄λ₯Ό κΈ°λ€λ¦¬μ§ μκ³ λ 10μ΄κ° νλ©΄μ νκΈ°λλ€.
κ·Έ ν setInterval λ©μλ μμ diplayTime(--reaminingTime)
μ ν΅ν΄ 1μ΄μ© μ€μ΄λ μκ°μ νκΈ°ν μ μλ€.
// κ²μ μνλ₯Ό κΈ°μ΅νλ μ μ λ³μλ€
let timer = undefined;
// μ«μ κ΄λ ¨ μ μ λ³μλ€
const GAME_DURATION_TIME = 10; // μ΄ λ¨μ
// νμ΄λ¨Έ μμ ν¨μ
function startTimer() {
let remainingSec = GAME_DURATION_TIME;
displayTime(remainingSec);
timer = setInterval(() => {
if (remainingSec <= 0) {
clearInterval(timer);
return;
}
displayTime(--remainingSec);
}, 1000);
}
// λ¨μ μκ° νκΈ° ν¨μ
function displayTime (time) {
let minutes = Math.floor(time / 60);
let seconds = time % 60;
gameTimer.innerHTML = `${minutes}:${seconds}`;
}
π λ΄κ° μ²μ μμ±ν μ½λ
startBtn.addEventListener('click', function () {
clickLimit.innerHTML = 10;
});
π κ°μλ₯Ό μ°Έκ³ ν΄ μμ λ° μμ±
// κ²μ μ΄κΈ°ν (λΉκ·Όκ³Ό λ²λ μ΄λ―Έμ§ λλ€ λ°°μΉ / ν΄λ¦ μ ν νμ 10μΌλ‘ μ€μ )
function initGame () {
field.innerHTML = '';
makeImg('carrot', CARROT_COUNT, 'img/carrot.png', CARROT_SIZE);
makeImg('bug', BUG_COUNT, 'img/bug.png', BUG_SIZE);
gameLimit.innerHTML = CARROT_COUNT;
bg_mp3.play();
}
π λ΄κ° μ²μ μμ±ν μ½λ
startBtn.addEventListener('click', function () {
startBtn.innerHTML = '<i class="fas fa-stop"></i>';
});
π κ°μλ₯Ό μ°Έκ³ ν΄ μμ λ° μμ±
// κ²μ μ€μ§ μμ΄μ½ νκΈ° ν¨μ
function showStopIcon () {
const icon = document.querySelector('.fa-play');
icon.classList.add('fa-stop');
icon.classList.remove('fa-play');
}
'use strict';
// htmlμμ λ°μμ¨ μμλ€
const startBtn = document.querySelector('.setting-startBtn');
const gameTimer = document.querySelector('.setting-timer');
const gameLimit = document.querySelector('.setting-limit');
const field = document.querySelector('.field');
// κ²μ μνλ₯Ό κΈ°μ΅νλ μ μ λ³μλ€
let started = false;
let timer = undefined;
let limit = 0;
// μ«μ κ΄λ ¨ μ μ λ³μλ€
const CARROT_COUNT = 10;
const BUG_COUNT = 7;
const CARROT_SIZE = 120;
const BUG_SIZE = 80;
const GAME_DURATION_TIME = 10;
// κ²μ μμ λ²νΌ ν΄λ¦ μ
startBtn.addEventListener('click', () => {
if (started) {
stopGame();
} else {
startGame();
}
started = !started;
});
// κ²μ μμ ν¨μ
function startGame () {
// κ²μ μ΄κΈ°ν
initGame();
// νμ΄λ¨Έ μμ
startTimer();
// κ²μ μμ μμ΄μ½ β κ²μ μ€μ§ μμ΄μ½
showStopIcon();
}
// κ²μ μ΄κΈ°ν (λΉκ·Όκ³Ό λ²λ μ΄λ―Έμ§ λλ€ λ°°μΉ / ν΄λ¦ μ ν νμ 10μΌλ‘ μ€μ )
function initGame () {
makeImg('carrot', CARROT_COUNT, 'img/carrot.png', CARROT_SIZE);
makeImg('bug', BUG_COUNT, 'img/bug.png', BUG_SIZE);
gameLimit.innerHTML = CARROT_COUNT;
}
// μ΄λ―Έμ§ μμ± ν¨μ
function makeImg (name, count, src, size) {
for (let i = 0; i < count; i++) {
const item = document.createElement('img');
item.setAttribute('class', `${name}`);
item.setAttribute('src', `${src}`);
const coord = randomCoord(size);
item.style.left = coord[0] + 'px';
item.style.top = coord[1] + 'px';
item.style.width = size + 'px';
field.appendChild(item);
}
}
// λλ€ μ’ν μμ± ν¨μ
function randomCoord (size) {
const fieldRect = field.getBoundingClientRect();
return [ ( fieldRect.width - size ) * Math.random(),
( fieldRect.height - size ) * Math.random() ];
}
// νμ΄λ¨Έ μμ ν¨μ
function startTimer() {
let remainingSec = GAME_DURATION_TIME;
displayTime(remainingSec);
timer = setInterval(() => {
if (remainingSec <= 0) {
clearInterval(timer);
return;
}
displayTime(--remainingSec);
}, 1000);
}
// λ¨μ μκ° νκΈ° ν¨μ
function displayTime (time) {
let minutes = Math.floor(time / 60);
let seconds = time % 60;
gameTimer.innerHTML = `${minutes}:${seconds}`;
}
// κ²μ μ€μ§ μμ΄μ½ νκΈ° ν¨μ
function showStopIcon () {
const icon = document.querySelector('.fa-play');
icon.classList.add('fa-stop');
icon.classList.remove('fa-play');
}
κ²μ μ μ§ λ²νΌμ ν΄λ¦νλ©΄
1) νμ΄λ¨Έκ° λ©μΆ°μΌ νλ€.
2) κ²μ μμ λ²νΌμ΄ μμ΄μ ΈμΌ νλ€.
3) REPLAY 문ꡬμ ν¨κ» μ¬μμ λ²νΌμ΄ λ μΌ νλ€.
startBtn.addEventListener('click', function startTimer () {
if (document.querySelector('i[class$="stop"]') === null) { // κ²μ μμ μ , κ²μ μμ
/* μ½λ μ€λ΅ */
} else { // κ²μ μμ ν, κ²μ μ μ§
bg_mp3.pause();
const alert_wav = new Audio('sound/alert.wav');
alert_wav.play();
clearInterval(decreNum);
startBtn.style.visibility = 'hidden';
displayResult('REPLAY β');
}
});
β HTML
<html>
<body>
<div class="setting">
<div class="setting-box">
<button class="setting-startBtn" type="button"><i class="fas fa-play"></i></button>
<div class="setting-timer">00:00</div>
<div class="setting-limit">0</div>
</div>
</div>
<div class="field"></div>
<div class="result">
<button class="replayBtn" type="button"><i class="fas fa-redo"></i></button>
<p class="notice"></p>
</div>
</body>
</html>
β CSS
.field {
height: calc(100vh - 450px);
position: relative;
}
.result {
width: 50%;
margin: 0 auto;
padding: 10px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
border-radius: 20px;
background-color: rgba(0, 0, 0, 0.5);
text-align: center;
font-size: 3em;
color: white;
z-index: 1;
display: none;
}
β JavaScript
const field = document.querySelector('.field');
const gameResult = document.querySelector('.result');
const replayBtn = document.querySelector('.replayBtn');
const notice = document.querySelector('.notice');
// κ²μ μμ λ²νΌ ν΄λ¦ μ
startBtn.addEventListener('click', () => {
if (started) {
stopGame();
} else {
startGame();
}
started = !started;
});
// κ²μ μ€μ§ ν¨μ (started κ°μ΄ trueμΈ μνμμ startBtnμ λ€μ ν΄λ¦νλ©΄)
function stopGame () {
// κ²μ μμ λ²νΌ μ¨κΉ
hideStartBtn();
// νμ΄λ¨Έ μ μ§
stopTimer();
// REPLAY 문ꡬ, μ¬μμ λ²νΌ νκΈ°
showGameResult('Replay β');
}
// νμ΄λ¨Έ μ μ§ ν¨μ
function stopTimer () {
clearInterval(timer);
}
// κ²μ μμ λ²νΌ μ¨κΉ ν¨μ
function hideStartBtn () {
startBtn.style.visibility = 'hidden';
}
// κ²°κ³Ό 문ꡬ, μ¬μμ λ²νΌ νκΈ° ν¨μ
function showGameResult (text) {
gameResult.style.display = 'block';
notice.innerHTML = text;
}
μ¬μμ λ²νΌμ ν΄λ¦νλ©΄
1) μ΄μ κ²μμ λΉκ·Όκ³Ό λ²λ κ° μμ΄μ ΈμΌ νλ€.
2) replay β μ°½μ΄ μμ΄μ ΈμΌ νλ€.
3) λ€μ 2.κ° μΌμ΄λμΌ νλ€. - (1), (3)λ§
4) νμ΄λ¨Έκ° 'λ€μ μ²μλΆν°' μλν΄μΌ νλ€.
5) κ²μ μμ λ²νΌμ΄ λ€μ 보μ¬μΌ νλ€.
// field μμ ν΄λ¦νμ λ
field.addEventListener('click', function (event) {
// 8. ν΄λ¦λ κ² μ¬μμ λ²νΌμ΄λ©΄
if (event.target.className === 'fas fa-redo') {
init();
placeImgRandomly();
clickLimit.innerHTML = 10;
timer.innerHTML = `0:10`;
time = 9;
startWorkingTimer();
bg_mp3.currentTime = 0;
bg_mp3.play();
startBtn.style.visibility = 'visible';
}
});
// μ¬μμ λ²νΌ ν΄λ¦
replayBtn.addEventListener('click', () => {
started = true;
// μ΄μ λΉκ·Όκ³Ό λ²λ μ΄λ―Έμ§ μμ κ³ , μλ‘κ² λΉκ·Ό λ²λ μ΄λ―Έμ§ λλ€ λ°°μΉ & ν΄λ¦ μ ν νμ 10μΌλ‘ μ€μ
initGame();
// νμ΄λ¨Έ λ€μ μ²μλΆν° μλ
startTimer ();
// κ²°κ³Ό 문ꡬ, μ¬μμ λ²νΌ μ¨κΉ
hideGameResult();
});
// μ¬μμ λ²νΌ ν΄λ¦
replayBtn.addEventListener('click', () => {
started = true;
// κ²μ μμ
startGame();
// κ²°κ³Ό 문ꡬ, μ¬μμ λ²νΌ μ¨κΉ
hideGameResult();
});
// field μμ ν΄λ¦νμ λ
field.addEventListener('click', function (event) {
// 3. ν΄λ¦λ κ² λΉκ·Όμ΄λ©΄
if (event.target.alt === 'carrot') {
const carrot_mp3 = new Audio('sound/carrot_pull.mp3');
carrot_mp3.play();
clickLimit.innerHTML = clickLimit.innerHTML - 1;
event.target.remove();
// 5. μ ν μκ° λ΄μ λͺ¨λ λΉκ·Όμ ν΄λ¦νλ©΄
if (time > 0) {
if (document.querySelectorAll('img[alt="carrot"]').length === 0) {
bg_mp3.pause();
win_mp3.play();
clearInterval(decreNum);
startBtn.style.visibility = 'hidden';
displayResult('YOU WON π');
}
}
}
});
// field μμ ν΄λ¦νμ λ
field.addEventListener('click', function (event) {
// 4. ν΄λ¦λ κ² λ²λ λΌλ©΄
if (event.target.alt === 'bug') {
bg_mp3.pause();
const bug_mp3 = new Audio('sound/bug_pull.mp3');
bug_mp3.play();
clearInterval(decreNum);
startBtn.style.visibility = 'hidden';
displayResult('YOU LOST π©');
}
});
// νλ ν΄λ¦
field.addEventListener('click', event => {
if (!started) {
return;
} else {
if (event.target.className === 'carrot') {
clickCarrot(event);
} else if (event.target.className === 'bug') {
clickBug();
}
}
});
// λΉκ·Ό ν΄λ¦
function clickCarrot (event) {
// λΉκ·Ό μ κ±°
event.target.remove();
// ν΄λ¦ μ ν νμ 1 κ°μ
let clickLimit = CARROT_COUNT;
clickLimit = clickLimit - 1;
gameLimit.innerHTML = clickLimit;
}
// λ²λ ν΄λ¦
function clickBug () {
started = false;
// κ²μ μμ λ²νΌ μ¨κΉ
hideStartBtn();
// νμ΄λ¨Έ μ μ§
stopTimer();
// YOU LOST 문ꡬ, μ¬μμ λ²νΌ νκΈ°
showGameResult('YOU LOST');
}
κ³μ limitλ₯Ό μ¬μ©ν΄μ μμ±ν΄ λ΄. μλν λλ‘ μ€νλλ κ±Έ νμΈνλ€.
κ²μμ μΉν¨μ λ°λΌ νλ©΄μ λ°κΎΈκΈ° μν΄ finishGame ν¨μλ₯Ό μΆκ°νλ€. μ‘°κ±΄λΆ μΌν μ°μ°μλ₯Ό μ΄μ©νλ€. νμ΄λ¨Έ ν¨μμλ finishGame ν¨μλ₯Ό μΆκ°νλ€.
// μ«μ κ΄λ ¨ μ μ λ³μλ€
const CARROT_COUNT = 10;
const GAME_DURATION_TIME = 10;
// κ²μ μνλ₯Ό κΈ°μ΅νλ μ μ λ³μλ€
let started = false;
let timer = undefined;
let limit = CARROT_COUNT;
// νμ΄λ¨Έ μμ ν¨μ
function startTimer () {
let remainingSec = GAME_DURATION_TIME;
displayTime(remainingSec);
timer = setInterval(() => {
if (remainingSec <= 0) {
clearInterval(timer);
finishGame(limit === 0); // κ²μ μ’
λ£
return;
}
displayTime(--remainingSec);
}, 1000);
}
// field ν΄λ¦
field.addEventListener('click', onFieldClick);
function onFieldClick (event) {
if (!started) { // start κ°μ΄ falseμ΄λ©΄ ν΄λ¦μ΄ λ¨Ήμ§ μμ
return;
}
const target = event.target;
if (target.matches('.carrot')) {
// λΉκ·Ό μ κ±°
target.remove();
// ν΄λ¦ μ ν νμ κ°μ
showGameLimit(--limit);
// κ²μ μ’
λ£ (WON)
if (limit === 0) {
finishGame(true);
}
} else if (target.matches('.bug')) {
// κ²μ μ’
λ£ (LOST)
finishGame(false);
}
}
// ν΄λ¦ μ ν νμ νκΈ° ν¨μ
function showGameLimit (num) {
gameLimit.innerHTML = num;
}
// κ²μ μ’
λ£ ν¨μ (WON λλ LOST)
function finishGame (win) {
// κ²μ μμ λ²νΌ μ¨κΉ
hideStartBtn();
// νμ΄λ¨Έ μ μ§
stopTimer();
// ~ 문ꡬ, μ¬μμ λ²νΌ νκΈ°
showGameResult(win? 'YOU WON π' :'YOU LOST π©');
}
// μμ
μ¬μ
function playSound (sound) {
sound.currentTime = 0;
sound.play();
}
// μμ
μ μ§
function stopSound (sound) {
sound.pause();
}
π HTML
<html>
<body>
<div class="setting">
<div class="setting-box">
<button class="setting-startBtn" type="button"><i class="fas fa-play"></i></button>
<div class="setting-timer">00:00</div>
<div class="setting-limit">0</div>
</div>
</div>
<div class="field"></div>
<div class="result">
<button class="replayBtn" type="button"><i class="fas fa-redo"></i></button>
<p class="notice"></p>
</div>
</body>
</html>
π CSS
μΆκ°μ μΌλ‘ νμ΄λ¨Έμ ν΄λ¦ μ ν νμ μμ 컀μ μ¬λ Έμ λ ν
μ€νΈκ° μ νλμ§ μλλ‘ CSS μμ νλ€. user-select: none
μ μ¬μ©ν¨.
div.settingκ³Ό div.fieldμ heightλ₯Ό 450px κ³ μ κ°μμ % κ°μΌλ‘ μμ νλ€. htmlκ³Ό bodyμλ height: 100% λ₯Ό μΆκ°ν¨.
.setting {
height: 50%;
position: relative;
}
.field {
height: calc(100vh - 50%);
position: relative;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
button {
border: 5px solid black;
border-radius: 20px;
background-color: wheat;
width: 100px;
height: 100px;
cursor: pointer;
display: block;
margin: 0 auto;
transition: all 200ms ease;
}
button:hover {
transform: scale(1.1);
}
img {
position: absolute;
transition: all 200ms ease;
}
img:hover {
transform: scale(1.2);
}
html {
height: 100%;
}
body {
height: 100%;
background: url("img/background.png") no-repeat center / cover;
font-family: "Gowun Batang", serif;
}
.setting {
height: 50%;
position: relative;
}
.setting-box {
position: absolute;
top: 30px;
left: 50%;
transform: translateX(-50%);
}
.setting-timer {
border: 5px solid black;
border-radius: 20px;
background-color: white;
width: 260px;
height: 80px;
margin: 15px auto;
text-align: center;
font-size: 2.5em;
line-height: 1.3;
user-select: none;
}
.fas {
font-size: 40px;
}
.setting-limit {
border: 4px solid black;
border-radius: 50%;
background-color: orange;
color: white;
width: 100px;
height: 100px;
margin: 0 auto;
text-align: center;
line-height: 1.5;
font-size: 3em;
font-weight: bold;
user-select: none;
}
.field {
height: calc(100vh - 50%);
position: relative;
}
.result {
width: 50%;
margin: 0 auto;
padding: 40px;
position: absolute;
left: 50%;
top: 70%;
transform: translate(-50%, -50%);
border-radius: 20px;
background-color: rgba(0, 0, 0, 0.5);
text-align: center;
font-size: 3em;
color: white;
z-index: 1;
display: none;
}
π JavaScript
'use strict';
const startBtn = document.querySelector('.setting-startBtn');
const gameTimer = document.querySelector('.setting-timer');
const gameLimit = document.querySelector('.setting-limit');
const field = document.querySelector('.field');
const gameResult = document.querySelector('.result');
const replayBtn = document.querySelector('.replayBtn');
const notice = document.querySelector('.notice');
const CARROT_COUNT = 10;
const BUG_COUNT = 7;
const CARROT_SIZE = 120;
const BUG_SIZE = 80;
const GAME_DURATION_TIME = 10;
let started = false;
let timer = undefined;
let limit = CARROT_COUNT;
const bgSound = new Audio('sound/bg.mp3');
const alertSound = new Audio('sound/alert.wav');
const carrotSound = new Audio('sound/carrot_pull.mp3');
const bugSound = new Audio('sound/bug_pull.mp3');
const winSound = new Audio('sound/game_win.mp3');
startBtn.addEventListener('click', () => {
if (started) {
stopGame();
} else {
startGame();
}
});
replayBtn.addEventListener('click', () => {
started = true;
startGame();
hideGameResult();
playSound(bgSound);
});
function stopGame () {
started = false;
hideStartBtn();
stopTimer();
showGameResult('Replay β');
stopSound(bgSound);
playSound(alertSound);
}
function startGame () {
started = true;
initGame();
startTimer();
showStopIcon();
playSound(bgSound);
}
field.addEventListener('click', onFieldClick);
function onFieldClick (event) {
if (!started) {
return;
}
const target = event.target;
if (target.matches('.carrot')) {
target.remove();
showGameLimit(--limit);
playSound(carrotSound);
if (limit === 0) {
finishGame(true);
}
} else if (target.matches('.bug')) {
finishGame(false);
}
}
function showGameLimit (num) {
gameLimit.innerHTML = num;
}
function finishGame (win) {
hideStartBtn();
stopTimer();
showGameResult(win? 'YOU WON π' :'YOU LOST π©');
if (win) {
stopSound(bgSound);
playSound(winSound);
} else {
stopSound(bgSound);
playSound(bugSound);
}
}
function initGame () {
limit = CARROT_COUNT;
field.innerHTML = '';
makeImg('carrot', CARROT_COUNT, 'img/carrot.png', CARROT_SIZE);
makeImg('bug', BUG_COUNT, 'img/bug.png', BUG_SIZE);
gameLimit.innerHTML = CARROT_COUNT;
}
function makeImg (name, count, src, size) {
for (let i = 0; i < count; i++) {
const item = document.createElement('img');
item.setAttribute('class', `${name}`);
item.setAttribute('src', `${src}`);
const coord = randomCoord(size);
item.style.left = coord[0] + 'px';
item.style.top = coord[1] + 'px';
item.style.width = size + 'px';
field.appendChild(item);
}
}
function randomCoord (size) {
const fieldRect = field.getBoundingClientRect();
return [ ( fieldRect.width - size ) * Math.random(),
( fieldRect.height - size ) * Math.random() ];
}
function startTimer () {
let remainingSec = GAME_DURATION_TIME;
displayTime(remainingSec);
timer = setInterval(() => {
if (remainingSec <= 0) {
clearInterval(timer);
finishGame(limit === 0);
return;
}
displayTime(--remainingSec);
}, 1000);
}
function stopTimer () {
clearInterval(timer);
}
function displayTime (time) {
let minutes = Math.floor(time / 60);
let seconds = time % 60;
gameTimer.innerHTML = `${minutes}:${seconds}`;
}
function showStopIcon () {
const icon = document.querySelector('.fas');
icon.classList.add('fa-stop');
icon.classList.remove('fa-play');
startBtn.style.visibility = 'visible';
}
function hideStartBtn () {
startBtn.style.visibility = 'hidden';
}
function showGameResult (text) {
gameResult.style.display = 'block';
notice.innerHTML = text;
}
function hideGameResult () {
gameResult.style.display = 'none';
}
function playSound (sound) {
sound.currentTime = 0;
sound.play();
}
function stopSound (sound) {
sound.pause();
}
κ²μ μ§ν μνλ₯Ό λνλ΄λ μ μ λ³μλ₯Ό νμ©ν κ².
μ«μλ λλ¬Έμμ μΈλλ°λ₯Ό μ΄μ©ν΄ λ³μλ‘ μ μ΄μ€ κ².
μλ μΈ μ€μ λͺ¨λ κ°μ μ½λμ΄λ€.
field.addEventListener('click', onFieldClick);
field.addEventListener('click', (event) => onFieldClick(event));
field.addEventListener('click', function (event) {
onFieldClick(event);
})
setInterval λ©μλ
μ΄μ©ν΄μ νμ΄λ¨Έ λ§λ ν μκ° νκΈ°νκΈ°.
ν μ€μ§λ¦¬ 짧μ μ½λλΌλ λ΄μ©μ λ§κ² ν¨μλ‘ λ§λ€μ΄ μ°λλ‘ νλ€. ꡬꡬμ μ if ꡬ문μ λμ΄λμ§ λ§κ³ ν¨μλ₯Ό νμ©ν κ².
if ꡬ문 쑰건μμμ λ³μ μ체μ ture/false κ° νμ©νκΈ°. μ‘°κ±΄λΆ μΌν μ°μ°μ
λ€μ ν λ² μ 리.
ν¨μλ₯Ό μ μΈν λ κ·Έ ν¨μκ° μ€νλμ΄μ μ λλ κ²½μ°κ° μλ€λ©΄, μ²μμ if ꡬ문과 return
μ μ΄μ©νμ¬ κ·Έ λ°μ μ½λλ₯Ό μ½μ§ μκ³ λ°λ‘ κ·Έ ν¨μλ₯Ό νμΆνλλ‘ νλ κ² μ’λ€.
κ°μμμλ ꡬνν΄μΌ ν λ΄μ©μ μͺΌκ° ν, κ·Έ λ΄μ©μ λ΄κ³ μλ ν¨μμ μ΄λ¦μ λ¨Όμ μ μ΄μ νΈμΆν΄μ£Όκ³ , κ·Έ λ°μ κ·Έ ν¨μλ₯Ό μ μΈνλ€.
κ·Έλμ ν¨μλ μμ μ for ꡬ문μ²λΌ μ μ§ λͺ¨λ₯΄κ² κΈ°νΌνκ³ μΆμ κ² μ€ νλμλλ° μ΄λ² μ€μ΅μ ν΅ν΄ μ 보λ€λ μ‘°κΈ μ΅μν΄μ§ κ±° κ°λ€. μ΄λ² μ€μ΅μ κ°μ₯ ν° μν κ°λ€. μ΄μ 리ν©ν λ§ ννΈκ° λ¨μλ€.