ν€λ³΄λλ‘ νλ μ΄, λ¬Όλ‘ λͺ¨λ°μΌλ‘λ κ°λ₯νλ€.
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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,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μ μΈ μ€ λͺ°λμ΄μ γ γ γ μμ¬μμ.
μ°Έ, λ§ν¬κ° μλͺ» λ¬λ Έμ΄μ~