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

Animated the pac-man to open and close mouth #106

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
21 changes: 20 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ document.addEventListener("DOMContentLoaded", () => {
y: 0,
};
const pacmanSpeed = 200;
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 Down Expand Up @@ -885,6 +885,8 @@ document.addEventListener("DOMContentLoaded", () => {
//move pacman
function movePacman() {
squares[pacmanCurrentIndex].classList.remove("pac-man");
let directionClassToAdd = 'right'
let directionClassesToRemove = []
setInterval(() => {
if (pacmanVelocity.x === 1 && pacmanVelocity.y == 0) {
if (
Expand All @@ -895,9 +897,13 @@ document.addEventListener("DOMContentLoaded", () => {
squares[pacmanCurrentIndex].classList.remove("pac-man");

pacmanCurrentIndex -= 1;
directionClassToAdd = 'left'
directionClassesToRemove = ['up', 'down', 'right']
}
if (squares[pacmanCurrentIndex - 1] === squares[363]) {
pacmanCurrentIndex = 391;
directionClassToAdd = 'left'
directionClassesToRemove = ['up', 'down', 'right']
}
}
if (pacmanVelocity.x === -1 && pacmanVelocity.y == 0) {
Expand All @@ -909,6 +915,8 @@ document.addEventListener("DOMContentLoaded", () => {
squares[pacmanCurrentIndex].classList.remove("pac-man");

pacmanCurrentIndex -= width;
directionClassToAdd = 'up'
directionClassesToRemove = ['left', 'down', 'right']
}
}
if (pacmanVelocity.x === 0 && pacmanVelocity.y == 1) {
Expand All @@ -920,9 +928,13 @@ document.addEventListener("DOMContentLoaded", () => {
squares[pacmanCurrentIndex].classList.remove("pac-man");

pacmanCurrentIndex += 1;
directionClassToAdd = 'right'
directionClassesToRemove = ['left', 'down', 'up']
}
if (squares[pacmanCurrentIndex + 1] === squares[392]) {
pacmanCurrentIndex = 364;
directionClassToAdd = 'right'
directionClassesToRemove = ['left', 'down', 'up']
}
}
if (pacmanVelocity.x === 0 && pacmanVelocity.y == -1) {
Expand All @@ -933,10 +945,15 @@ document.addEventListener("DOMContentLoaded", () => {
) {
squares[pacmanCurrentIndex].classList.remove("pac-man");
pacmanCurrentIndex += width;
directionClassToAdd = 'down'
directionClassesToRemove = ['left', 'right', 'up']
}
}

squares[pacmanCurrentIndex].classList.add("pac-man");
// add and remove certain classes once the pac-man changes the facing direction
squares[pacmanCurrentIndex].classList.add(directionClassToAdd)
squares[pacmanCurrentIndex].classList.remove(...directionClassesToRemove)
pacDotEaten();
powerPelletEaten();
}, pacmanSpeed);
Expand Down Expand Up @@ -1079,6 +1096,8 @@ document.addEventListener("DOMContentLoaded", () => {
//start the game when enter is pressed
function startGame(event) {
if (event.keyCode === 13) {
// add the "pac-man" class only when the Board is visible
squares[pacmanCurrentIndex].classList.add("pac-man");
document.removeEventListener("keydown", startGame);
//remove start screen
document.getElementById("start-screen").style.display = "none";
Expand Down
51 changes: 49 additions & 2 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,43 @@ body {
}

.pac-man {
background-color: rgb(247, 232, 22);
border-radius: 10px;
position: relative;
transition: transform 0.1s;
}
/*pac-man will face right*/
.pac-man.right {
transform: rotate(0deg);
}
/*pac-man will face left*/
.pac-man.left {
transform: rotate(180deg);
}
/*pac-man will face up*/
.pac-man.up {
transform: rotate(-90deg);
}
/*pac-man will face down*/
.pac-man.down {
transform: rotate(90deg);
}

.pac-man::before, .pac-man::after {
content: "";
display: block;
height: 0;
width: 0;
position: absolute;
border-radius: 50%;
border: solid 10px yellow;
border-right-color: transparent;
border-bottom-color: transparent;
animation: mouth-top 0.5s ease infinite;
}
.pac-man::after {
border-color: yellow;
border-right-color: transparent;
border-top-color: transparent;
animation: mouth-bottom 0.5s ease infinite;
}
h2 {
color: white;
Expand Down Expand Up @@ -189,3 +224,15 @@ h2 {
opacity: 0;
}
}
/*animation for pac-man's top mouth*/
@keyframes mouth-top {
50% {
transform: rotate(44deg);
}
}
/*animation for pac-man's bottom mouth*/
@keyframes mouth-bottom {
50% {
transform: rotate(-44deg);
}
}