Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added background themes and eyes for ghost #87

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 92 additions & 62 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
document.addEventListener("DOMContentLoaded", () => {
const scoreDisplay = document.getElementById("score");
document.addEventListener('DOMContentLoaded', () => {
const scoreDisplay = document.getElementById('score');
const width = 28;
let score = 0;
const grid = document.querySelector(".grid");
const grid = document.querySelector('.grid');
const gameStartsAudio = new Audio('./assets/audio/gameStartsAudio.mp3');
const gameOverAudio = new Audio('./assets/audio/gameEndsAudio.mp3');
const pacmanEatsAudio = new Audio('./assets/audio/pacmanEatsAudio.mp3');
const layout = [
1,
1,
Expand Down Expand Up @@ -800,24 +803,46 @@ document.addEventListener("DOMContentLoaded", () => {
//create your board
function createBoard() {
for (let i = 0; i < layout.length; i++) {
const square = document.createElement("div");
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");
squares[i].classList.add('pac-dot');
} else if (layout[i] === 1) {
squares[i].classList.add("wall");
squares[i].classList.add('wall');
} else if (layout[i] === 2) {
squares[i].classList.add("ghost-lair");
squares[i].classList.add('ghost-lair');
} else if (layout[i] === 3) {
squares[i].classList.add("power-pellet");
squares[i].classList.add('power-pellet');
}
}
}
createBoard();

// play audio when game starts
function playGameStartsAudio() {
gameStartsAudio.currentTime = 0;
gameStartsAudio.play();
}

//play audio when game ends
function playGameEndAudio() {
gameOverAudio.currentTime = 0;
gameOverAudio.play();
}

function playPacManEatAudio() {
pacmanEatsAudio.currentTime = 0;
pacmanEatsAudio.play();
}

// play background audio during DOM onload
window.onload = (e) => {
playGameStartsAudio();
};

//create Characters
//draw pacman onto the board
let pacmanCurrentIndex = 490;
Expand All @@ -826,7 +851,7 @@ document.addEventListener("DOMContentLoaded", () => {
y: 0,
};
const pacmanSpeed = 200;
squares[pacmanCurrentIndex].classList.add("pac-man");
squares[pacmanCurrentIndex].classList.add('pac-man');
//get the coordinates of pacman on the grid with X and Y axis
// function getCoordinates(index) {
// return [index % width, Math.floor(index / width)]
Expand All @@ -840,8 +865,8 @@ document.addEventListener("DOMContentLoaded", () => {
case 37:
if (
pacmanCurrentIndex % width !== 0 &&
!squares[pacmanCurrentIndex - 1].classList.contains("wall") &&
!squares[pacmanCurrentIndex - 1].classList.contains("ghost-lair")
!squares[pacmanCurrentIndex - 1].classList.contains('wall') &&
!squares[pacmanCurrentIndex - 1].classList.contains('ghost-lair')
) {
pacmanVelocity.y = 0;
pacmanVelocity.x = 1;
Expand All @@ -850,8 +875,8 @@ document.addEventListener("DOMContentLoaded", () => {
case 38:
if (
pacmanCurrentIndex - width >= 0 &&
!squares[pacmanCurrentIndex - width].classList.contains("wall") &&
!squares[pacmanCurrentIndex - width].classList.contains("ghost-lair")
!squares[pacmanCurrentIndex - width].classList.contains('wall') &&
!squares[pacmanCurrentIndex - width].classList.contains('ghost-lair')
) {
pacmanVelocity.y = 0;
pacmanVelocity.x = -1;
Expand All @@ -860,8 +885,8 @@ document.addEventListener("DOMContentLoaded", () => {
case 39:
if (
pacmanCurrentIndex % width < width - 1 &&
!squares[pacmanCurrentIndex + 1].classList.contains("wall") &&
!squares[pacmanCurrentIndex + 1].classList.contains("ghost-lair")
!squares[pacmanCurrentIndex + 1].classList.contains('wall') &&
!squares[pacmanCurrentIndex + 1].classList.contains('ghost-lair')
) {
pacmanVelocity.y = 1;
pacmanVelocity.x = 0;
Expand All @@ -870,8 +895,8 @@ document.addEventListener("DOMContentLoaded", () => {
case 40:
if (
pacmanCurrentIndex + width < width * width &&
!squares[pacmanCurrentIndex + width].classList.contains("wall") &&
!squares[pacmanCurrentIndex + width].classList.contains("ghost-lair")
!squares[pacmanCurrentIndex + width].classList.contains('wall') &&
!squares[pacmanCurrentIndex + width].classList.contains('ghost-lair')
) {
pacmanVelocity.y = -1;
pacmanVelocity.x = 0;
Expand All @@ -884,15 +909,15 @@ document.addEventListener("DOMContentLoaded", () => {

//move pacman
function movePacman() {
squares[pacmanCurrentIndex].classList.remove("pac-man");
squares[pacmanCurrentIndex].classList.remove('pac-man');
setInterval(() => {
if (pacmanVelocity.x === 1 && pacmanVelocity.y == 0) {
if (
pacmanCurrentIndex % width !== 0 &&
!squares[pacmanCurrentIndex - 1].classList.contains("wall") &&
!squares[pacmanCurrentIndex - 1].classList.contains("ghost-lair")
!squares[pacmanCurrentIndex - 1].classList.contains('wall') &&
!squares[pacmanCurrentIndex - 1].classList.contains('ghost-lair')
) {
squares[pacmanCurrentIndex].classList.remove("pac-man");
squares[pacmanCurrentIndex].classList.remove('pac-man');

pacmanCurrentIndex -= 1;
}
Expand All @@ -903,21 +928,21 @@ document.addEventListener("DOMContentLoaded", () => {
if (pacmanVelocity.x === -1 && pacmanVelocity.y == 0) {
if (
pacmanCurrentIndex - width >= 0 &&
!squares[pacmanCurrentIndex - width].classList.contains("wall") &&
!squares[pacmanCurrentIndex - width].classList.contains("ghost-lair")
!squares[pacmanCurrentIndex - width].classList.contains('wall') &&
!squares[pacmanCurrentIndex - width].classList.contains('ghost-lair')
) {
squares[pacmanCurrentIndex].classList.remove("pac-man");
squares[pacmanCurrentIndex].classList.remove('pac-man');

pacmanCurrentIndex -= width;
}
}
if (pacmanVelocity.x === 0 && pacmanVelocity.y == 1) {
if (
pacmanCurrentIndex % width < width - 1 &&
!squares[pacmanCurrentIndex + 1].classList.contains("wall") &&
!squares[pacmanCurrentIndex + 1].classList.contains("ghost-lair")
!squares[pacmanCurrentIndex + 1].classList.contains('wall') &&
!squares[pacmanCurrentIndex + 1].classList.contains('ghost-lair')
) {
squares[pacmanCurrentIndex].classList.remove("pac-man");
squares[pacmanCurrentIndex].classList.remove('pac-man');

pacmanCurrentIndex += 1;
}
Expand All @@ -928,44 +953,45 @@ document.addEventListener("DOMContentLoaded", () => {
if (pacmanVelocity.x === 0 && pacmanVelocity.y == -1) {
if (
pacmanCurrentIndex + width < width * width &&
!squares[pacmanCurrentIndex + width].classList.contains("wall") &&
!squares[pacmanCurrentIndex + width].classList.contains("ghost-lair")
!squares[pacmanCurrentIndex + width].classList.contains('wall') &&
!squares[pacmanCurrentIndex + width].classList.contains('ghost-lair')
) {
squares[pacmanCurrentIndex].classList.remove("pac-man");
squares[pacmanCurrentIndex].classList.remove('pac-man');
pacmanCurrentIndex += width;
}
}

squares[pacmanCurrentIndex].classList.add("pac-man");
squares[pacmanCurrentIndex].classList.add('pac-man');
pacDotEaten();
powerPelletEaten();
}, pacmanSpeed);
}

// what happens when you eat a pac-dot
function pacDotEaten() {
if (squares[pacmanCurrentIndex].classList.contains("pac-dot")) {
if (squares[pacmanCurrentIndex].classList.contains('pac-dot')) {
score++;
if (score < 50) {
document.getElementById("score").classList.add("low-score");
document.getElementById('score').classList.add('low-score');
} else if (score > 100) {
document.getElementById("score").classList.add("mid-score");
document.getElementById('score').classList.add('mid-score');
} else if (score > 200) {
document.getElementById("score").classList.add("high-score");
document.getElementById('score').classList.add('high-score');
}
scoreDisplay.innerHTML = score;
squares[pacmanCurrentIndex].classList.remove("pac-dot");
squares[pacmanCurrentIndex].classList.remove('pac-dot');
checkForWin();
}
}

//what happens when you eat a power-pellet
function powerPelletEaten() {
if (squares[pacmanCurrentIndex].classList.contains("power-pellet")) {
if (squares[pacmanCurrentIndex].classList.contains('power-pellet')) {
playPacManEatAudio();
score += 10;
ghosts.forEach((ghost) => (ghost.isScared = true));
setTimeout(unScareGhosts, 10000);
squares[pacmanCurrentIndex].classList.remove("power-pellet");
squares[pacmanCurrentIndex].classList.remove('power-pellet');
checkForWin();
}
}
Expand All @@ -989,16 +1015,16 @@ document.addEventListener("DOMContentLoaded", () => {

//all my ghosts
ghosts = [
new Ghost("blinky", 348, 100),
new Ghost("stinky", 376, 400),
new Ghost("inky", 351, 300),
new Ghost("clyde", 379, 200),
new Ghost('blinky', 348, 100),
new Ghost('stinky', 376, 400),
new Ghost('inky', 351, 300),
new Ghost('clyde', 379, 200),
];

//draw my ghosts onto the grid
ghosts.forEach((ghost) => {
squares[ghost.currentIndex].classList.add(ghost.className);
squares[ghost.currentIndex].classList.add("ghost");
squares[ghost.currentIndex].classList.add('ghost');
});

function moveGhost(ghost) {
Expand All @@ -1008,36 +1034,36 @@ document.addEventListener("DOMContentLoaded", () => {
ghost.timerId = setInterval(function () {
//if the next square your ghost is going to go to does not have a ghost and does not have a wall
if (
!squares[ghost.currentIndex + direction].classList.contains("ghost") &&
!squares[ghost.currentIndex + direction].classList.contains("wall")
!squares[ghost.currentIndex + direction].classList.contains('ghost') &&
!squares[ghost.currentIndex + direction].classList.contains('wall')
) {
//remove the ghosts classes
squares[ghost.currentIndex].classList.remove(ghost.className);
squares[ghost.currentIndex].classList.remove("ghost", "scared-ghost");
squares[ghost.currentIndex].classList.remove('ghost', 'scared-ghost');
//move into that space
ghost.currentIndex += direction;
squares[ghost.currentIndex].classList.add(ghost.className, "ghost");
squares[ghost.currentIndex].classList.add(ghost.className, 'ghost');
//else find a new random direction to go in
} else direction = directions[Math.floor(Math.random() * directions.length)];

//if the ghost is currently scared
if (ghost.isScared) {
squares[ghost.currentIndex].classList.add("scared-ghost");
squares[ghost.currentIndex].classList.add('scared-ghost');
}

//if the ghost is currently scared and pacman is on it
if (
ghost.isScared &&
squares[ghost.currentIndex].classList.contains("pac-man")
squares[ghost.currentIndex].classList.contains('pac-man')
) {
squares[ghost.currentIndex].classList.remove(
ghost.className,
"ghost",
"scared-ghost"
'ghost',
'scared-ghost'
);
ghost.currentIndex = ghost.startIndex;
score += 100;
squares[ghost.currentIndex].classList.add(ghost.className, "ghost");
squares[ghost.currentIndex].classList.add(ghost.className, 'ghost');
}
checkForGameOver();
}, ghost.speed);
Expand All @@ -1046,15 +1072,16 @@ document.addEventListener("DOMContentLoaded", () => {
//check for a game over
function checkForGameOver() {
if (
squares[pacmanCurrentIndex].classList.contains("ghost") &&
!squares[pacmanCurrentIndex].classList.contains("scared-ghost")
squares[pacmanCurrentIndex].classList.contains('ghost') &&
!squares[pacmanCurrentIndex].classList.contains('scared-ghost')
) {
playGameEndAudio();
ghosts.forEach((ghost) => clearInterval(ghost.timerId));
document.removeEventListener("keyup", movePacman);
document.removeEventListener('keyup', movePacman);
pacmanVelocity.x = 0;
pacmanVelocity.y = 0;
//display game over screen and refresh after 3s to rest game
document.getElementById("game-over-screen").style.display = "flex";
document.getElementById('game-over-screen').style.display = 'flex';
setTimeout(function () {
window.location.reload();
}, 3000);
Expand All @@ -1065,11 +1092,11 @@ document.addEventListener("DOMContentLoaded", () => {
function checkForWin() {
if (score === 274) {
ghosts.forEach((ghost) => clearInterval(ghost.timerId));
document.removeEventListener("keyup", movePacman);
document.removeEventListener('keyup', movePacman);
pacmanVelocity.x = 0;
pacmanVelocity.y = 0;
//display you won screen and refresh after 3s to rest game
document.getElementById("you-won-screen").style.display = "flex";
document.getElementById('you-won-screen').style.display = 'flex';
setTimeout(function () {
window.location.reload();
}, 3000);
Expand All @@ -1078,17 +1105,20 @@ document.addEventListener("DOMContentLoaded", () => {

//start the game when enter is pressed
function startGame(event) {
console.log('game started');
gameStartsAudio.pause();
gameStartsAudio.currentTime = 0;
if (event.keyCode === 13) {
document.removeEventListener("keydown", startGame);
document.removeEventListener('keydown', startGame);
//remove start screen
document.getElementById("start-screen").style.display = "none";
document.getElementById('start-screen').style.display = 'none';
//set pacman velocity and enable movement
document.addEventListener("keyup", setPacmanVelocity);
document.addEventListener('keyup', setPacmanVelocity);
movePacman();
// move the Ghosts randomly
ghosts.forEach((ghost) => moveGhost(ghost));
}
}

document.addEventListener("keydown", startGame);
document.addEventListener('keydown', startGame);
});
Binary file added assets/audio/gameEndsAudio.mp3
Binary file not shown.
Binary file added assets/audio/gamePlayingAudio.mp3
Binary file not shown.
Binary file added assets/audio/gameStartsAudio.mp3
Binary file not shown.
Binary file added assets/audio/pacmanEatsAudio.mp3
Binary file not shown.
Binary file added assets/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading