しりとりゲーム
最初の文字はです。
勝ちました!
負けました!
<!DOCTYPE html>
<html>
<head>
<title>しりとり</title>
<style>
body {
background-color: #f5f5f5;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
margin-top: 50px;
}
#game {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 50px;
}
input[type="text"] {
padding: 10px;
font-size: 20px;
width: 300px;
margin-top: 20px;
border-radius: 5px;
border: none;
box-shadow: 2px 2px 5px #888888;
}
button {
padding: 10px;
font-size: 20px;
width: 100px;
margin-top: 20px;
border-radius: 5px;
border: none;
background-color: #4CAF50;
color: white;
box-shadow: 2px 2px 5px #888888;
cursor: pointer;
}
.win {
background-color: #8A2BE2;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 99999;
}
.lose {
background-color: #FF00FF;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 99999;
}
img {
width: 200px;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>しりとりゲーム</h1>
<div id="game">
<p>最初の文字は<span id="start-letter"></span>です。</p>
<input type="text" id="input">
<button id="submit-btn">送信</button>
</div>
<div id="win" class="win">
<h2>勝ちました!</h2>
<img src="https://i.imgur.com/R50mquJ.png" alt="猫">
</div>
<div id="lose" class="lose">
<h2>負けました!</h2>
<img src="https://i.imgur.com/t4MYcgp.gif" alt="時計">
</div>
<script>
// 最初の文字をランダムに設定
const startLetters = [
'あ', 'い', 'う', 'え', 'お',
'か', 'き', 'く', 'け', 'こ',
'さ', 'し', 'す', 'せ', 'そ',
'た', 'ち', 'つ', 'て', 'と',
'な', 'に', 'ぬ', 'ね', 'の',
'は', 'ひ', 'ふ', 'へ', 'ほ',
'ま', 'み', 'む', 'め', 'も',
'や', 'ゆ', 'よ',
'ら', 'り', 'る', 'れ', 'ろ',
'わ', 'を', 'ん'
];
const startLetter = startLetters[Math.floor(Math.random() * startLetters.length)];
document.getElementById('start-letter').textContent = startLetter;
// ゲームの進行に必要な変数を用意
let currentLetter = startLetter;
let isGameEnd = false;
// 送信ボタンがクリックされたら
document.getElementById('submit-btn').addEventListener('click', function() {
if (!isGameEnd) { // ゲームが終わってなければ
const input = document.getElementById('input').value.trim();
if (input.charAt(0) === currentLetter && input.slice(-1) !== 'ん') { // 正しい回答だった場合
currentLetter = input.slice(-1); // 次の文字を更新
document.getElementById('start-letter').textContent = currentLetter;
document.getElementById('input').value = ''; // テキストボックスを空にする
} else { // 負けた場合
isGameEnd = true;
document.getElementById('lose').style.display = 'flex';
}
}
});
// 勝った場合は背景を紫色(#8A2BE2)にして、画像を表示
// (実際のアプリでは、この部分は別のページに遷移させるなどして、もう少し工夫して作る方が良いです)
if (!isGameEnd) {
isGameEnd = true;
document.getElementById('game').style.display = 'none';
document.getElementById('win').style.display = 'flex';
document.body.style.backgroundColor = '#8A2BE2';
}
</script>
</body>
</html>