ํค๋ณด๋๋ก ํ๋ ์ด, ๋ฌผ๋ก ๋ชจ๋ฐ์ผ๋ก๋ ๊ฐ๋ฅํ๋ค.
Vanilla Javascript, CSS, HTML ์ ์ฌ์ฉํด์ ๋ง๋ ํฉ๋งจ.
ํฌ๋ฆฌ์ค๋ง์ค๊ฐ ๋ค๊ฐ์ฌ๋์ฏค ๋ง๋ค์ด์ ํฉ๋งจ ๋ก๊ณ ์ ํฌ๋ฆฌ์ค๋ง์ค ๋ชจ์๋ฅผ ์์๊ณ
๋ก๊ณ ๋ฅผ ๋๋ฅด๋ฉด ์์ ์ฌ์, arrow key๋ก ํ๋ ์ด, ๊ฒ์์์ ๋ฒํผ, ๊ฒ์, ์ต๊ณ ๊ธฐ๋ก, ๊ฒ์์ค๋ฒ, ์ฑ๊ณต, ๋จน์ด ๋จน์ผ๋ฉด ๋ฌด์ ์ผ๋ก ๋ณํ๋ ๊ฒ์ ๊ตฌํํ๋ค.
After 2 months of learning JS, CSS and HTML(before knowing the existance of REACT) I wanted to push myself to see if I can Javascript games. Here I present my most proud achievement of all :)
์ฃผ์ : https://emilyoun.github.io/pacman-christmas/
Github ๋ ํฌ : https://github.com/emilyoun/pacman-christmas
์ด๊ฑฐ ํ๋ฒ ๋ง๋ค์ด๋๊ณ ์ด๊ณณ์ ๊ณณ ์ ์ฐ๊ณ ์๋ค css ๊ผญ ๋ฃ์ด์ค์ผ ๊ท์ฌ์
<audio id="audio" style="display:none;" src="Fun Christmas - AShamaluevMusic.mp3" controls autoplay loop onloadeddata="setHalfVolume()">
let on_off = document.querySelector(".musicOn");
on_off.onclick = function () {
if (audio.paused) {
audio.play();
}
else {
audio.pause();
}
}
#logo{
width: 700px;
}
#logo:active{
transform: scale(1, 0.9);
transition: transform 0.2s color 0.2s;
}
**0 = ๋จน์ด, 1 = ๋ฒฝ, 2 = ๊ณ ์คํธ ๋ถํ ์ฅ์, 3 = ๋ถ์คํธ, 4 = ๋น๊ณต๊ฐ **์ ์ก๊ณ array ํ๋ ๋ง๋ค์ด์(layout) ํ๋ํ๋ ๊ทธ๋ ค์ค๋ค
document.getElementById("play").addEventListener("click", function game() {
const scoreDisplay = document.getElementById("score");
const width = 28; // 28*28 = 784 squares
const grid = document.querySelector(".grid")
const layout = [
1,1,1,1,1,1,1,1,4,4,1,1,1,4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,4,4,1,3,1,4,4,1,0,0,0,0,0,0,0,0,0,0,3,1,
1,0,1,1,1,1,0,1,1,1,1,0,1,4,4,1,1,1,1,0,1,0,1,1,1,0,1,1,
1,3,1,4,4,1,0,0,0,0,0,0,1,4,4,4,4,4,1,0,0,0,1,4,1,0,1,4,
1,0,1,4,4,1,0,1,1,0,1,0,1,4,4,4,4,4,1,0,1,0,1,4,1,0,1,4,
1,0,1,1,1,1,0,0,0,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,0,1,1,
1,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,
1,0,1,1,1,1,0,3,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,
4,4,4,4,4,1,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,1,4,4,4,4,4,
4,4,4,4,4,1,0,0,0,0,0,1,1,4,4,1,1,0,0,0,0,0,1,4,4,4,4,4,
1,1,1,1,1,1,0,1,1,1,0,1,2,2,2,2,1,0,1,1,1,0,1,1,1,1,1,1,
4,0,0,0,0,0,0,1,4,1,0,2,2,2,2,2,2,0,1,4,1,0,0,0,0,0,0,4,
1,1,1,1,1,1,0,1,1,1,0,1,2,2,2,2,1,0,1,1,1,0,1,1,1,1,1,1,
4,4,4,4,4,1,0,0,0,0,0,1,1,4,4,1,1,0,0,0,0,0,1,4,4,4,4,4,
4,4,4,4,4,1,0,1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,1,4,4,4,4,4,
1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,1,3,1,0,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,3,1,
1,0,1,1,1,1,0,3,1,0,0,1,1,1,1,1,1,0,0,0,0,0,1,0,1,1,1,1,
1,0,0,0,0,0,0,0,0,0,1,1,4,4,4,4,1,1,0,1,1,0,1,0,1,4,4,4,
1,1,1,0,1,1,1,1,1,0,1,4,4,4,4,4,4,1,0,0,0,0,1,0,1,4,4,4,
4,4,1,0,1,4,4,4,1,0,1,4,4,4,4,4,4,1,0,1,1,1,1,0,1,1,1,1,
1,1,1,0,1,1,1,1,1,0,1,1,1,4,4,1,1,1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,1,4,4,1,0,0,0,1,1,1,1,0,1,1,0,1,
1,3,1,1,1,1,1,1,1,1,1,0,1,4,4,1,0,1,0,1,4,4,1,0,1,3,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,1,4,4,1,0,0,0,1,4,4,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,1,1,1,1,1,4,4,1,1,1,1,1,1];
const squares = [];
and traverse a for loop to place each element in the right place.
// Draw the grid
function createBoard() {
for (let i = 0; i < layout.length ; i++){
const square = document.createElement("div");
grid.appendChild(square);
squares.push(square);
// Add layout to the board
if (layout[i] === 0) {
squares[i].classList.add("pac-dot");
} else if (layout[i] === 1) {
squares[i].classList.add("wall");
} else if (layout[i] === 2) {
squares[i].classList.add("ghost-lair");
} else if (layout[i] === 3) {
squares[i].classList.add("power-pellet");
} else if (layout[i] === 4) {
squares[i].classList.add("empty");
}
}
}
createBoard();
function bestScoreCount () {
let higher;
let bestScore = window.localStorage.getItem(higher);
window.localStorage.setItem(bestScore, higher);
const bestScoreDisplay = document.getElementById("bestScore");
if (bestScore == null) {
bestScore = score;
window.localStorage.setItem(higher, bestScore);
} else if (score < Number(window.localStorage.getItem(higher))) {
bestScore = score;
} else if (score > Number(window.localStorage.getItem(higher))) {
window.localStorage.clear();
bestScore = score;
window.localStorage.setItem(higher, bestScore);
}
bestScoreDisplay.innerHTML = window.localStorage.getItem(higher);
}
bestScoreCount();
let pacmanCurrentIndex = 518;
squares[pacmanCurrentIndex].classList.add("pac-man");
function movePacman (e) {
squares[pacmanCurrentIndex].classList.remove("pac-man");
switch(e.keyCode){
case 37:
if (pacmanCurrentIndex % width !== 0 && !squares[pacmanCurrentIndex -1].classList.contains("wall") && !squares[pacmanCurrentIndex -1].classList.contains("ghost-lair")) {
pacmanCurrentIndex -=1;
squares[pacmanCurrentIndex].style.transform = "scaleX(-1) rotate(95deg)";
// If Pac-Man is on the left exit
if (pacmanCurrentIndex - 1 === 363) {
pacmanCurrentIndex = 391;
}}
break;
case 38:
if (pacmanCurrentIndex - width >= 0 && !squares[pacmanCurrentIndex - width].classList.contains("wall") && !squares[pacmanCurrentIndex - width].classList.contains("ghost-lair")) { pacmanCurrentIndex -= width;
squares[pacmanCurrentIndex].style.transform = "scaleY(1)";
}
break;
case 39:
if (pacmanCurrentIndex % width < width - 1 && !squares[pacmanCurrentIndex +1].classList.contains("wall") && !squares[pacmanCurrentIndex +1].classList.contains("ghost-lair")){ pacmanCurrentIndex += 1;
squares[pacmanCurrentIndex].style.transform = "scaleY(1) rotate(95deg)";
// If Pac-Man is on the right exit
if(pacmanCurrentIndex + 1 === 392) {
pacmanCurrentIndex = 364;
}}
break;
case 40:
if (pacmanCurrentIndex + width < width * width && !squares[pacmanCurrentIndex + width].classList.contains("wall") && !squares[pacmanCurrentIndex + width].classList.contains("ghost-lair")) {
pacmanCurrentIndex += width;
squares[pacmanCurrentIndex].style.transform = "rotate(190deg)";
}
break;
}
squares[pacmanCurrentIndex].classList.add("pac-man");
pacDotEaten();
powerPelletEaten();
checkForGameOver();
checkForWin();
}
document.addEventListener("keydown", movePacman);
e.touches ๋ฅผ ์ฃผ์๋๋ฐ ์์ธํ๊ฑด ๊นํ ๋ ํฌ์ app.js์์ ํ์ธ ๊ฐ๋ฅํ๋ค.
์ ๋
์ ์์ํด์ ์๊ธฐ์ ์ ๋๋ด๋ ค ํ๋๋ฐ ์์ฌ์ด ์๊ฒจ์ ์คํ 8์์ฏค ์์ํด์ ์๋ฒฝ 4์์ฏค ๋๋ฌ๋ค. ๋๋ต 8์๊ฐ์ ๋ ๊ฑธ๋ ธ์ง๋ง ๊ฒฐ๊ณผ๋ฌผ์ด ๋ง์์ ๋ค์๋ ํ๋ก์ ํธ. ํ ๋๋ง๋ค ๋ฟ๋ฏํด์ ์์ง๋ ๊ฐ๋ ํ๋ ์ดํ๋ค ๐
์ด๋ ๊ฒ ํด๋์์ ์ ์๊ณ ์๋ Side Project๋ค์ด ๋ง์๋ฐ(blogging materials) ์ฐจ๊ทผ์ฐจ๊ทผ ์
๋ฐ์ดํธ ํ ์์ .
๋ํ๊ต์์ ๊ณผ์ ๋ก JS๋ก ํฉ๋งจ ๋ง๋ค์๋ ๊ฒฝํ์ด ์๋๋ฐ.. ์ถ์ต์ด๋ค์.
์ ๋ ๊ทธ๋ Canvas๋ฅผ ์ฌ์ฉํ์์ด์.
๊ทธ๋ ์์ฑํ๋ ์ฝ๋, ๋ค์ ํ๋ฒ ๋ณด๊ณ ์ถ์๋ฐ ์ฐพ์์๊ฐ ์๊ตฐ์ ใ ๊ทธ๋๋ GitHub์ ์ธ ์ค ๋ชฐ๋์ด์ ใ ใ ใ ์์ฌ์์.
์ฐธ, ๋งํฌ๊ฐ ์๋ชป ๋ฌ๋ ธ์ด์~