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

Add Winner Notification and Play Again Functionality to Rock Paper Scissors Game #286

Merged
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
14 changes: 7 additions & 7 deletions Rock Paper Scissors/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<!DOCTYPE html>
<!-- Coding By PrasadChavan - prasadchavan.me -->
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Game | Rock Paper Scissors</title>
<link rel="stylesheet" href="src/style.css" />
</head>

<body>
<section class="container">
<div class="result_field">
Expand Down Expand Up @@ -45,11 +43,13 @@
<button class="reset">Reset</button>
</section>

<audio id="click-sound" src="sounds/click.mp3"></audio>
<audio id="win-sound" src="sounds/win.mp3"></audio>
<audio id="lose-sound" src="sounds/lose.mp3"></audio>
<div class="modal" id="winnerModal">
<div class="modal-content">
<p id="winnerMessage"></p>
<button id="playAgainButton">Play Again</button>
</div>
</div>

<script src="scripts/script.js" defer></script>
</body>

</html>
</html>
40 changes: 34 additions & 6 deletions Rock Paper Scissors/scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,43 @@ const gameContainer = document.querySelector(".container"),
result = document.querySelector(".result"),
userScoreDisplay = document.querySelector(".user_score"),
cpuScoreDisplay = document.querySelector(".cpu_score"),
resetButton = document.querySelector(".reset");
resetButton = document.querySelector(".reset"),
winnerModal = document.getElementById("winnerModal"),
winnerMessage = document.getElementById("winnerMessage"),
playAgainButton = document.getElementById("playAgainButton");

let userScore = 0,
cpuScore = 0;

const optionImages = document.querySelectorAll(".option_image");

function checkWinner() {
if (userScore === 5) {
showWinner("User");
} else if (cpuScore === 5) {
showWinner("CPU");
}
}

function showWinner(winner) {
winnerMessage.textContent = `${winner} Won 5 Times!`;
winnerModal.style.display = "block";
}

function resetGame() {
userScore = 0;
cpuScore = 0;
userScoreDisplay.textContent = `User: ${userScore}`;
cpuScoreDisplay.textContent = `CPU: ${cpuScore}`;
result.textContent = "Let's Play!!";
optionImages.forEach(image => image.classList.remove("active"));
}

playAgainButton.addEventListener("click", () => {
winnerModal.style.display = "none";
resetGame();
});

optionImages.forEach((image, index) => {
image.addEventListener("click", (e) => {
image.classList.add("active");
Expand Down Expand Up @@ -61,14 +91,12 @@ optionImages.forEach((image, index) => {

userScoreDisplay.textContent = `User: ${userScore}`;
cpuScoreDisplay.textContent = `CPU: ${cpuScore}`;

checkWinner();
}, 2500);
});
});

resetButton.addEventListener("click", () => {
userScore = 0;
cpuScore = 0;
userScoreDisplay.textContent = `User: ${userScore}`;
cpuScoreDisplay.textContent = `CPU: ${cpuScore}`;
result.textContent = "Let's Play!!";
resetGame();
});
48 changes: 47 additions & 1 deletion Rock Paper Scissors/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ body {
font-size: 1.2rem;
color: #333;
position: absolute;
top: 100px;
top: 20px;
}

.user_score {
Expand Down Expand Up @@ -137,3 +137,49 @@ body {
.reset:hover {
background-color: #6c1ccf;
}

/* Existing styles... */

/* Modal styles */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
background-color: #fff;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 400px;
text-align: center;
border-radius: 10px;
}

.modal-content p {
font-size: 1.5rem;
color: #7d2ae8;
}

.modal-content button {
padding: 10px 20px;
background-color: #7d2ae8;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1rem;
margin-top: 20px;
}

.modal-content button:hover {
background-color: #5c1bb1;
}
Loading