💩パニック2
Time: 30s | Score: 0
以下は、タイトル「💩パニック2」というゲームの実装例です。制限時間内に💩を押してスコアを稼ぎ、🍦を押すとゲームオーバーとなります。再ゲームするためにはリスタートボタンを押してください。また、ユーザーの要望に合わせ、ジョークを追加しました。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>💩パニック2</title>
<style>
#stage {
width: 350px;
height: 350px;
background-color: white;
position: relative;
margin: 0 auto;
}
.poop {
width: 30px;
height: 30px;
background-color: brown;
position: absolute;
cursor: pointer;
border-radius: 50%;
}
.icecream {
width: 30px;
height: 30px;
background-color: pink;
position: absolute;
cursor: pointer;
border-radius: 50%;
}
#score {
text-align: center;
}
</style>
</head>
<body>
<h1>💩パニック2</h1>
<div id="stage"></div>
<div id="score">Time: <span id="time">30</span>s | Score: <span id="scoreValue">0</span></div>
<button id="restart" style="display: none;">Restart</button>
<script>
// ステージの幅と高さ
const stageWidth = 350;
const stageHeight = 350;
// 制限時間
const timeLimit = 30;
// スコア
let score = 0;
// タイマー
let timer;
// ステージ要素を取得
const stage = document.getElementById('stage');
// スコア表示要素を取得
const scoreValue = document.getElementById('scoreValue');
// タイム表示要素を取得
const time = document.getElementById('time');
// リスタートボタン要素を取得
const restartButton = document.getElementById('restart');
// ポイントが押されたときのイベントハンドラ
function handleClick(event) {
const target = event.target;
// クリックされた要素が💩ならスコアを加算し、要素を削除する
if (target.classList.contains('poop')) {
score++;
target.remove();
scoreValue.textContent = score;
// ゲームクリア判定
if (score === 10) {
clearInterval(timer);
alert('おめでとう!スコア10点達成です!');
}
}
// クリックされた要素が🍦ならゲームオーバー
else if (target.classList.contains('icecream')) {
clearInterval(timer);
alert('ゲームオーバー...アイスクリームを押してしまいました...😭');
}
}
// リスタートボタンが押されたときのイベントハンドラ
function handleRestart() {
score = 0;
scoreValue.textContent = score;
time.textContent = timeLimit;
restartButton.style.display = 'none';
// タイマー再開
startTimer();
// ステージリセット
resetStage();
}
// ステージの要素を再生成する関数
function resetStage() {
stage.innerHTML = '';
}
// タイマーを開始する関数
function startTimer() {
let count = timeLimit;
timer = setInterval(() => {
count--;
time.textContent = count;
// タイムアップ判定
if (count === 0) {
clearInterval(timer);
restartButton.style.display = 'block';
alert('Time Up! リスタートボタンで再挑戦してください!');
}
}, 1000);
}
// 💩と🍦を生成し、クリックイベントリスナーを追加する関数
function createObjects() {
for (let i = 0; i < 10; i++) {
const poop = document.createElement('div');
poop.className = 'poop';
const icecream = document.createElement('div');
icecream.className = 'icecream';
// ステージ上のランダムな位置に要素を配置
const randomX = Math.floor(Math.random() * (stageWidth - 30));
const randomY = Math.floor(Math.random() * (stageHeight - 30));
poop.style.top = `${randomY}px`;
poop.style.left = `${randomX}px`;
icecream.style.top = `${randomY}px`;
icecream.style.left = `${randomX}px`;
stage.appendChild(poop);
stage.appendChild(icecream);
}
// クリックイベントリスナーを追加
stage.addEventListener('click', handleClick);
}
// ゲームをスタートする関数
function startGame() {
createObjects();
startTimer();
}
// ゲームスタート
startGame();
// リスタートボタンのクリックイベントリスナーを追加
restartButton.addEventListener('click', handleRestart);
</script>
</body>
</html>
```
ジョークを追加しました!
- Q: 「💩パニック2」の世界でブレイクダンスをする💩は何て言うでしょう?
- A: 「シャコリンが転がっている!」