-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4477 from 1911aditi/main
added new game Whack a mole
- Loading branch information
Showing
7 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Whack-a-Mole</title> | ||
<link rel="stylesheet" href="styles.css"> | ||
</head> | ||
<body> | ||
<div id="game-container"> | ||
<div class="hole" id="hole1"></div> | ||
<div class="hole" id="hole2"></div> | ||
<div class="hole" id="hole3"></div> | ||
<div class="hole" id="hole4"></div> | ||
<div class="hole" id="hole5"></div> | ||
<div class="hole" id="hole6"></div> | ||
<div class="hole" id="hole7"></div> | ||
<div class="hole" id="hole8"></div> | ||
<div class="hole" id="hole9"></div> | ||
</div> | ||
<div id="scoreboard"> | ||
<div id="score">Score: 0</div> | ||
<div id="timer">Time: 30</div> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
let score = 0; | ||
let timeLeft = 30; | ||
let timerId; | ||
let moleTimerId; | ||
const scoreDisplay = document.getElementById('score'); | ||
const timerDisplay = document.getElementById('timer'); | ||
const holes = document.querySelectorAll('.hole'); | ||
|
||
function randomHole() { | ||
const index = Math.floor(Math.random() * holes.length); | ||
return holes[index]; | ||
} | ||
|
||
function showMole() { | ||
const hole = randomHole(); | ||
const mole = document.createElement('div'); | ||
mole.classList.add('mole'); | ||
hole.appendChild(mole); | ||
|
||
mole.addEventListener('click', () => { | ||
score++; | ||
scoreDisplay.textContent = `Score: ${score}`; | ||
hole.removeChild(mole); | ||
}); | ||
|
||
setTimeout(() => { | ||
if (hole.contains(mole)) { | ||
hole.removeChild(mole); | ||
} | ||
}, 1000); | ||
} | ||
|
||
function startGame() { | ||
timerId = setInterval(() => { | ||
timeLeft--; | ||
timerDisplay.textContent = `Time: ${timeLeft}`; | ||
if (timeLeft === 0) { | ||
clearInterval(timerId); | ||
clearInterval(moleTimerId); | ||
alert(`Game Over! Your score is ${score}`); | ||
} | ||
}, 1000); | ||
|
||
moleTimerId = setInterval(showMole, 800); | ||
} | ||
|
||
startGame(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
body { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
height: 100vh; | ||
margin: 0; | ||
background-color: #f0f0f0; | ||
font-family: Arial, sans-serif; | ||
} | ||
|
||
#game-container { | ||
display: grid; | ||
grid-template-columns: repeat(3, 100px); | ||
grid-template-rows: repeat(3, 100px); | ||
gap: 10px; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.hole { | ||
width: 100px; | ||
height: 100px; | ||
background-color: #8B4513; | ||
position: relative; | ||
cursor: pointer; | ||
} | ||
|
||
.mole { | ||
width: 60px; | ||
height: 60px; | ||
background-color: #000; | ||
border-radius: 50%; | ||
position: absolute; | ||
top: 20px; | ||
left: 20px; | ||
} | ||
|
||
#scoreboard { | ||
display: flex; | ||
justify-content: space-between; | ||
width: 300px; | ||
font-size: 24px; | ||
} | ||
|
||
#score, #timer { | ||
margin: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.