μ΄μ νμ΄μμ μ΄μ΄μ
μ¬μμ λ²νΌμ ν΄λ¦νλ©΄
1) μ΄μ κ²μμ λΉκ·Όκ³Ό λ²λ κ° μμ΄μ ΈμΌ νλ€.
2) replay β μ°½μ΄ μμ΄μ ΈμΌ νλ€.
3) λ€μ2.
κ° μΌμ΄λμΌ νλ€. - (1), (3)λ§
4) νμ΄λ¨Έκ° 'λ€μ μ²μλΆν°' μλν΄μΌ νλ€.
5) bg.mp3 λ°°κ²½μμ΄ 'λ€μ μ²μλΆν°' μ¬μλμ΄μΌ νλ€.
6) κ²μ μμ λ²νΌμ΄ λ€μ 보μ¬μΌ νλ€.
π‘ λμ μΌλ‘ μμ±ν μμμ μ΄λ²€νΈ λ°μΈλ© νλ λ°©λ²
π‘ μ€λμ€ μ²μλΆν° λ€μ μ¬μνλ λ°©λ²
Audio.pause();
Audio.currentTime = 0;
Audio.play();
// field μμ ν΄λ¦νμ λ
field.addEventListener('click', function (event) {
// 8. ν΄λ¦λ κ² μ¬μμ λ²νΌμ΄λ©΄
if (event.target.className === 'fas fa-redo') {
// π 1) μ΄μ κ²μμ λΉκ·Όκ³Ό λ²λ κ° μμ΄μ ΈμΌ νλ€.
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();
}
// π 2) replay β μ°½μ΄ μμ΄μ ΈμΌ νλ€.
const result = document.querySelector('.result');
result.remove();
// π 3) λ€μ 2.κ° μΌμ΄λμΌ νλ€. - (1), (3), (4)λ§
// (1) field μμ λΉκ·Ό 10κ°μ λ²λ 7λ§λ¦¬κ° λλ€ν μμΉμ λ°°μΉλμ΄μΌ νλ€.
const rect = field.getBoundingClientRect();
for (let i = 0; i < 10; i++) {
const carrot = document.createElement('img');
carrot.setAttribute('src', 'img/carrot.png');
carrot.setAttribute('alt', 'carrot');
carrot.style.left = (rect.width - 120) * Math.random() + 'px';
carrot.style.top = (rect.height - 120) * Math.random() + 'px';
carrot.style.width = '120px';
field.appendChild(carrot);
}
for (let i = 0; i < 7; i++) {
const bug = document.createElement('img');
bug.setAttribute('src', 'img/bug.png');
bug.setAttribute('alt', 'bug');
bug.style.left = (rect.width - 80) * Math.random() + 'px';
bug.style.top = (rect.height - 80) * Math.random() + 'px';
bug.style.width = '80px';
field.appendChild(bug);
}
// (3) ν΄λ¦ κ°λ₯ νμκ° 10μΌλ‘ λ°λμ΄μΌ νλ€.
clickLimit.innerHTML = 10;
// (4) bg.mp3 λ°°κ²½μμ΄ μ¬μλμ΄μΌ νλ€.
bg_mp3.currentTime = 0;
bg_mp3.play();
// π 4) κ²μ μμ λ²νΌμ΄ λ€μ 보μ¬μΌ νλ€.
startBtn.style.visibility = 'visible';
// π 5) νμ΄λ¨Έκ° 'λ€μ μ²μλΆν°' μλν΄μΌ νλ€.
timer.innerHTML = `0:10`;
time = 9;
startWorkingTimer();
}
});
π‘ settingμ μ, μλ margin κ°μ μΆκ°νλκΉ μλ―Έ μλ scrollbarκ° μκΈ΄λ€.
β λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ margin λμ topμ μ΄μ©ν΄ settingμ μμΉλ₯Ό μ‘°μ νλ€.
μΌλ¨ λͺ¨λ λ΄μ©μ ꡬν μλ£νκ³ , λͺ¨λ μ€λ₯ μμ΄ μ μ μλμ λλ€
<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>
</body>
</html>
* {
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;
}
button:hover {
transform: scale(1.1);
transition: all 200ms ease;
}
img {
position: absolute;
}
img:hover {
transform: scale(1.2);
transition: all 200ms ease;
}
/* μμ */
body {
background: url("img/background.png") no-repeat center / cover;
font-family: "Gowun Batang", serif;
}
.setting {
height: 450px;
position: relative;
}
.setting-box {
position: absolute;
top: 30px;
left: 50%;
transform: translate(-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;
}
.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;
}
.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;
}
fieldμ λ±λ‘ν΄μ€ ν΄λ¦ μ΄λ²€νΈ 리μ€λλ₯Ό μ 리ν΄μΌ νλλ° μ΄μ°¨νΌ 리ν©ν λ§ νλ©΄μ λ€μ λλμ μΌλ‘ νμ΄μΌ λ κ±° κ°μμ κ·Έλ₯ κ·Έλλ‘ λ . μλμ λλλ° μ λ§ μλλ§ λλ μ½λλ€. 리ν©ν λ§ νμ..
'use strict';
// htmlμμ λ°μμ¨ μμλ€
const startBtn = document.querySelector('.setting-startBtn');
const timer = document.querySelector('.setting-timer');
const clickLimit = document.querySelector('.setting-limit');
const field = document.querySelector('.field');
// μ€λμ€ νμΌμ λ³μμ ν λΉ
const bg_mp3 = new Audio('sound/bg.mp3');
const win_mp3 = new Audio('sound/game_win.mp3');
// λΉκ·Ό, λ²λ λλ€ λ°°μΉ
function placeImgRandomly () {
const rect = field.getBoundingClientRect();
for (let i = 0; i < 10; i++) {
const carrot = document.createElement('img');
carrot.setAttribute('src', 'img/carrot.png');
carrot.setAttribute('alt', 'carrot');
carrot.style.left = (rect.width - 120) * Math.random() + 'px';
carrot.style.top = (rect.height - 120) * Math.random() + 'px';
carrot.style.width = '120px';
field.appendChild(carrot);
}
for (let i = 0; i < 7; i++) {
const bug = document.createElement('img');
bug.setAttribute('src', 'img/bug.png');
bug.setAttribute('alt', 'bug');
bug.style.left = (rect.width - 80) * Math.random() + 'px';
bug.style.top = (rect.height - 80) * Math.random() + 'px';
bug.style.width = '80px';
field.appendChild(bug);
}
}
// λΉκ·Ό, λ²λ μ κ±° & 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();
}
// νμ΄λ¨Έ κ΄λ ¨
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 displayResult (text) {
const gameResult = document.createElement('div');
gameResult.classList.add('result');
gameResult.innerHTML = `
<button class="replayBtn" type="button"><i class="fas fa-redo"></i></button>
<p>${text}</p>
`;
field.appendChild(gameResult);
}
// 2. κ²μ μμ λ²νΌμ ν΄λ¦νλ©΄
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 {
bg_mp3.pause();
const alert_wav = new Audio('sound/alert.wav');
alert_wav.play();
clearInterval(decreNum);
startBtn.style.visibility = 'hidden';
displayResult('REPLAY β');
}
});
// 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 μμ ν΄λ¦νμ λ
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';
}
});
κ°μλ₯Ό μ°Έκ³ ν΄ λ΄ μ½λλ₯Ό 리ν©ν λ§ ν ν, μ€νλμ§ μμ κ²½μ° μ€λ₯ νμ λ° μμΈ λΆμ
HTML λ§ν¬μ
μ νλ©΄μ μμ replay μ°½μ λ§λ€μ΄μ€
ν CSSμμ display: none μ μ μ©ν¨. (κ·Έ ν λ²νΌμ classλ₯Ό 'pop-up pop-up-hide'λΌκ³ μ§μ ν΄μ€.)// λΉκ·Ό, λ²λ λλ€ λ°°μΉ
function placeImgRandomly () {
const rect = field.getBoundingClientRect();
for (let i = 0; i < 10; i++) {
const carrot = document.createElement('img');
carrot.setAttribute('src', 'img/carrot.png');
carrot.setAttribute('alt', 'carrot');
carrot.style.left = (rect.width - 120) * Math.random() + 'px';
carrot.style.top = (rect.height - 120) * Math.random() + 'px';
carrot.style.width = '120px';
field.appendChild(carrot);
}
for (let i = 0; i < 7; i++) {
const bug = document.createElement('img');
bug.setAttribute('src', 'img/bug.png');
bug.setAttribute('alt', 'bug');
bug.style.left = (rect.width - 80) * Math.random() + 'px';
bug.style.top = (rect.height - 80) * Math.random() + 'px';
bug.style.width = '80px';
field.appendChild(bug);
}
}
μλμ κ°μ΄ νμΌλ
coordArray is not defined μ€λ₯ λ°μ
// λΉκ·Ό, λ²λ λλ€ λ°°μΉ
function placeImgRandomly () {
makeImg('carrot', 10, 120);
makeImg('bug', 7, 80);
}
// λΉκ·Ό, λ²λ μ΄λ―Έμ§ μμ±
function makeImg (name, num, size) {
for (let i = 0; i < num; i++) {
randomCoord(size);
const item = document.createElement('img');
item.setAttribute('src', `img/${name}.png`);
item.setAttribute('alt', `${name}`);
item.style.left = coordArray[0] + 'px';
item.style.top = coordArray[1] + 'px';
item.style.width = `${size}px`;
field.appendChild(item);
}
}
// λλ€ν μ’ν μμ±
function randomCoord (size) {
const rect = field.getBoundingClientRect();
const x = (rect.width - size) * Math.random();
const y = (rect.height - size) * Math.random();
const coordArray = [x, y];
return coordArray;
}
makeImg ν¨μμμ coordArray[0]κ³Ό coordArray[1]μ κ°μ μ°κΈ° μν΄μλ, randomCoord ν¨μ
λ₯Ό makeImg ν¨μμμ 'λ³μμ ν λΉ'ν΄μ€μΌ νλ€.
μ¦, randomCoord(size);
β const coord = randomCoord(size);
randomCoord ν¨μμ return κ°μ λ°μ λ³μ coord
λ₯Ό λ°νμΌλ‘ makeImg ν¨μ λ΄μμ randomCoord ν¨μμ coordArray κ°μ μΈ μ μλ€.
coordArray[0] / coordArray[1]
β coord[0] / coord[1]
randomCoord(size) λΌκ³ λ§ μ°λ©΄, makeImg ν¨μμ randomCoord ν¨μκ° μ°κ²°μ΄ μ λΌμ randomCoord ν¨μλ‘λΆν° coordArray κ°, μ¦ return κ°μ λ°μμ¬ μ μλ€!
μ μ μλ νμΈ
μ«μλ κ·Έλ₯ μ°μ§ λ§κ³ , λ³μμ ν λΉν΄μ λ³μλ₯Ό μ°λλ‘ νλ€
// λΉκ·Ό, λ²λ μ΄λ―Έμ§ κ°μ & ν¬κΈ°
const CARROT_COUNT = 10;
const BUG_COUNT = 7;
const CARROT_SIZE = 120;
const BUG_SIZE = 80;
// λΉκ·Ό, λ²λ μ΄λ―Έμ§ λλ€ λ°°μΉ
function placeImgRandomly () {
makeImg('carrot', CARROT_COUNT, CARROT_SIZE);
makeImg('bug', BUG_COUNT, BUG_SIZE);
}
// λΉκ·Ό, λ²λ μ΄λ―Έμ§ μμ±
function makeImg (name, num, size) {
for (let i = 0; i < num; i++) {
const coord = randomCoord(size);
const item = document.createElement('img');
item.setAttribute('src', `img/${name}.png`);
item.setAttribute('alt', `${name}`);
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()];
}