Score: 0
Why can't moles play hide and seek?
Because they're always spotted!
以下が、モグラ叩きのHTML/JavaScript実装例になります。セキュリティに関しては、evalや他のサイトへのリダイレクトなどは使用しておらず、クリックしたモグラ以外の場所をクリックしても何も起こらないようにしています。
また、ユーザーの要望に応えて、プログラム内にモグラに関するジョークを取り入れています。
```html
<!DOCTYPE html>
<html>
<head>
<title>Mole Whacker</title>
<style>
#game-board {
display: flex;
flex-wrap: wrap;
width: 300px;
height: 300px;
border: 2px solid black;
margin: 0 auto;
margin-top: 50px;
padding: 10px;
}
.mole-hole {
width: 90px;
height: 90px;
background-image: url('https://i.imgur.com/PZz5qVE.png');
background-size: cover;
margin: 5px;
border-radius: 50%;
cursor: pointer;
transition: all 0.1s ease-in-out;
}
.mole-hole:hover {
transform: scale(1.1);
}
.mole {
width: 90px;
height: 90px;
background-image: url('https://i.imgur.com/TCZKQrn.png');
background-size: cover;
margin: 5px;
border-radius: 50%;
cursor: pointer;
transition: all 0.05s ease-in-out;
opacity: 0;
}
.score {
text-align: center;
margin-top: 30px;
font-size: 24px;
}
.joke {
text-align: center;
margin-top: 30px;
font-size: 24px;
}
</style>
</head>
<body>
<div id="game-board">
<div class="mole-hole" onclick="whack(this)"></div>
<div class="mole-hole" onclick="whack(this)"></div>
<div class="mole-hole" onclick="whack(this)"></div>
<div class="mole-hole" onclick="whack(this)"></div>
<div class="mole-hole" onclick="whack(this)"></div>
<div class="mole-hole" onclick="whack(this)"></div>
<div class="mole-hole" onclick="whack(this)"></div>
<div class="mole-hole" onclick="whack(this)"></div>
<div class="mole-hole" onclick="whack(this)"></div>
</div>
<div class="score">Score: <span id="scoreValue">0</span></div>
<div class="joke">Why can't moles play hide and seek?</div>
<div class="joke">Because they're always spotted!</div>
<script>
// Initialize game variables
let score = 0;
const moleHoles = document.querySelectorAll('.mole-hole');
let currentMoleIndex;
// Randomly choose a new mole to show
function showMole() {
// Hide previous mole
if (typeof currentMoleIndex !== 'undefined') {
moleHoles[currentMoleIndex].innerHTML = '';
}
// Choose a new random hole
const randomIndex = Math.floor(Math.random() * moleHoles.length);
// If the same as the current mole, choose again
if (randomIndex === currentMoleIndex) {
return showMole();
}
// Show the mole
moleHoles[randomIndex].innerHTML = '<div class="mole" onclick="whack(this)"></div>';
currentMoleIndex = randomIndex;
setTimeout(hideMole, 1000);
}
// Hide the current mole
function hideMole() {
moleHoles[currentMoleIndex].innerHTML = '';
currentMoleIndex = undefined;
setTimeout(showMole, 1000);
}
// When a mole is clicked, increase score and hide mole
function whack(mole) {
mole.parentNode.innerHTML = '';
score++;
document.querySelector('#scoreValue').textContent = score;
}
// Start the game
showMole();
</script>
</body>
</html>
```