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

Allows Background Color to Change #111

Open
wants to merge 1 commit 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
138 changes: 73 additions & 65 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 layout = [
1,
1,
Expand Down Expand Up @@ -800,19 +800,19 @@ 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');
}
}
}
Expand All @@ -826,7 +826,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 +840,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 +850,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 +860,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 +870,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 +884,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 +903,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 +928,44 @@ 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')) {
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,55 +989,55 @@ 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) {
const directions = [-1, +1, width, -width];
let direction = directions[Math.floor(Math.random() * directions.length)];

ghost.timerId = setInterval(function () {
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,16 +1046,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')
) {
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";
setTimeout(function () {
document.getElementById('game-over-screen').style.display = 'flex';
setTimeout(function() {
window.location.reload();
}, 3000);
}
Expand All @@ -1065,12 +1065,12 @@ 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";
setTimeout(function () {
document.getElementById('you-won-screen').style.display = 'flex';
setTimeout(function() {
window.location.reload();
}, 3000);
}
Expand All @@ -1079,16 +1079,24 @@ document.addEventListener("DOMContentLoaded", () => {
//start the game when enter is pressed
function startGame(event) {
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);

//Adjust Background Color
let colorAdjust = document.getElementById('colorChanger');
let bodyAdjust = document.getElementById('pacManBody');

colorAdjust.addEventListener('change', function() {
bodyAdjust.style.backgroundColor = this.value;
});
});
9 changes: 8 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="stylesheet" href="styles.css" />
<script src="app.js" charset="utf-8"></script>
</head>
<body>
<body id="pacManBody" class="bodyColor">
<div class="overlay-screen" id="start-screen">
<h1>PACMAN</h1>
<h2 id="start-msg">Press ENTER to start</h2>
Expand All @@ -15,6 +15,13 @@ <h2>Use arrow keys to move</h2>
<div class="game-container">
<div class="grid"></div>
<h2>Score:<span id="score">0</span></h2>
<label for="colorChanger">Choose a color:</label>
<select id="colorChanger" name="cars">
<option value="black">Black</option>
<option value="#808080">Grey</option>
<option value="#800000">Maroon</option>
<option value="#006400">Green</option>
</select>
</div>
<div class="overlay-screen" id="game-over-screen">
<span>GAME OVER!</span>
Expand Down
Loading