8000 Arrow buttons added by bunny8469 · Pull Request #80 · RishabhDevbanshi/Pacman-Game · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Arrow buttons added #80

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

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
26 changes: 19 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,17 @@ document.addEventListener("DOMContentLoaded", () => {

// console.log(getCoordinates(pacmanCurrentIndex))


document.getElementById("btnup").addEventListener('click', function(){setPacmanVelocity('up')})
document.getElementById("btndown").addEventListener("click", function(){setPacmanVelocity('down')})
document.getElementById("btnleft").addEventListener("click", function(){setPacmanVelocity('left')})
document.getElementById("btnright").addEventListener("click", function(){setPacmanVelocity('right')})


// set pacman velocity
function setPacmanVelocity(e) {
switch (e.keyCode) {
case 37:
function setPacmanVelocity(val) {
switch (true) {
case (event.keyCode == 37 || val == 'left'):
if (
pacmanCurrentIndex % width !== 0 &&
!squares[pacmanCurrentIndex - 1].classList.contains("wall") &&
Expand All @@ -847,7 +854,7 @@ document.addEventListener("DOMContentLoaded", () => {
pacmanVelocity.x = 1;
}
break;
case 38:
case (event.keyCode == 38 || val == 'up'):
if (
pacmanCurrentIndex - width >= 0 &&
!squares[pacmanCurrentIndex - width].classList.contains("wall") &&
Expand All @@ -857,7 +864,7 @@ document.addEventListener("DOMContentLoaded", () => {
pacmanVelocity.x = -1;
}
break;
case 39:
case (event.keyCode == 39 || val == 'right'):
if (
pacmanCurrentIndex % width < width - 1 &&
!squares[pacmanCurrentIndex + 1].classList.contains("wall") &&
Expand All @@ -867,7 +874,7 @@ document.addEventListener("DOMContentLoaded", () => {
pacmanVelocity.x = 0;
}
break;
case 40:
case (event.keyCode == 40 || val == 'down'):
if (
pacmanCurrentIndex + width < width * width &&
!squares[pacmanCurrentIndex + width].classList.contains("wall") &&
Expand All @@ -879,7 +886,7 @@ document.addEventListener("DOMContentLoaded", () => {
break;
}
checkForGameOver();
console.log(pacmanVelocity, e.keyCode);
console.log(pacmanVelocity, event.keyCode || val);
}

//move pacman
Expand Down Expand Up @@ -1051,6 +1058,10 @@ document.addEventListener("DOMContentLoaded", () => {
) {
ghosts.forEach((ghost) => clearInterval(ghost.timerId));
document.removeEventListener("keyup", movePacman);
document.getElementById("btnup").removeEventListener('click', function(){setPacmanVelocity('up')})
document.getElementById("btndown").removeEventListener("click", function(){setPacmanVelocity('down')})
document.getElementById("btnleft").removeEventListener("click", function(){setPacmanVelocity('left')})
document.getElementById("btnright").removeEventListener("click", function(){setPacmanVelocity('right')})
pacmanVelocity.x = 0;
pacmanVelocity.y = 0;
//display game over screen and refresh after 3s to rest game
Expand Down Expand Up @@ -1082,6 +1093,7 @@ document.addEventListener("DOMContentLoaded", () => {
document.removeEventListener("keydown", startGame);
//remove start screen
document.getElementById("start-screen").style.display = "none";
document.querySelector('.buttons').style.display = 'flex'
//set pacman velocity and enable movement
document.addEventListener("keyup", setPacmanVelocity);
movePacman();
Expand Down
12 changes: 11 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ <h2 id="start-msg">Press ENTER to start</h2>
<h2>Use arrow keys to move</h2>
</div>
<div class="game-container">
<div class="grid"></div>
<h2>Score:<span id="score">0</span></h2>
<div class="grid"></div>

<div class = 'buttons'>
<button class = "arrow-btn btnup" id="btnup"> &#8593; </button>
<div>
<button class = "arrow-btn btnleft" id="btnleft"> &#8592; </button>
<button class = "arrow-btn btndown" id="btndown"> &#8595; </button>
<button class = "arrow-btn btnright" id="btnright"> &#8594; </button>
</div>

</div>
</div>
<div class="overlay-screen" id="game-over-screen">
<span>GAME OVER!</span>
Expand Down
32 changes: 31 additions & 1 deletion styles.css
< 7493 tr data-hunk="59cf39f7e8414d7b6d0819631edcc3f50b5ac688b766f5655de53cbd565407b8" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ body {

.game-container {
width: 100%;
height: 100vh;
height: 85vh;
display: flex;
flex-direction: column;
justify-content: space-evenly;
Expand Down Expand Up @@ -189,3 +189,33 @@ h2 {
opacity: 0;
}
}

.buttons{
position: absolute;
height: auto;
width:auto;
bottom:1.5%;
left:50%;
transform: translateX(-50%);
display: none;
flex-direction: column;
justify-content: center;
align-items:center;
}
.arrow-btn{
height: 50px;
width: 50px;
cursor:pointer;
font-size:150%;
border:1px solid black;
transition: 0.3s;
border-radius:3px;
margin:0;
}
.arrow-btn:hover{
background-color: #222;
color:white;
}
.btnup{
margin-bottom:10px;
}
0