You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Code2C/hangman.html

210 lines
6.8 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hangman Game</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
padding: 50px;
}
h1 {
font-size: 36px;
color: #333;
}
.difficulty {
font-size: 20px;
margin: 20px 0;
}
.btn {
font-size: 18px;
padding: 10px 20px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
margin: 5px;
}
.btn:hover {
background-color: #45a049;
}
.input-box {
margin-top: 20px;
}
.word {
font-size: 40px;
margin: 20px;
}
.result {
font-size: 24px;
margin: 20px;
}
.score {
font-size: 24px;
margin: 10px;
}
.guesses {
font-size: 18px;
margin: 10px;
}
#try-again-btn,
#go-back-btn {
display: none;
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
background-color: #ff6347;
color: white;
border: none;
border-radius: 5px;
margin-top: 20px;
}
#try-again-btn:hover,
#go-back-btn:hover {
background-color: #e55347;
}
</style>
</head>
<body>
<h1>Hangman Game</h1>
<div class="difficulty">
<button class="btn" onclick="startGame('easy')">Easy</button>
<button class="btn" onclick="startGame('medium')">Medium</button>
<button class="btn" onclick="startGame('hard')">Hard</button>
</div>
<div class="game-container">
<div class="word" id="wordDisplay">_ _ _ _ _</div>
<div class="score" id="score">Score: 0</div>
<div class="guesses" id="guessedLetters">Guessed Letters: </div>
<div class="input-box">
<input type="text" id="letterInput" maxlength="1" placeholder="Enter a letter" />
<button class="btn" onclick="guessLetter()">Guess</button>
</div>
<div class="result" id="result"></div>
<!-- Try Again and Go Back buttons -->
<button id="try-again-btn" class="btn" onclick="tryAgain()">Try Again</button>
<button id="go-back-btn" class="btn" onclick="goBack()">Go Back</button>
</div>
<script>
let words = {
easy: ["apple", "banana", "grape"],
medium: ["orange", "strawberry", "pineapple"],
hard: ["watermelon", "pomegranate", "blueberry"]
};
let selectedWord = "";
let guessedLetters = [];
let incorrectGuesses = 0;
let maxIncorrectGuesses = 6;
let score = 0;
let wordDisplay = document.getElementById('wordDisplay');
let scoreDisplay = document.getElementById('score');
let guessedLettersDisplay = document.getElementById('guessedLetters');
let resultDisplay = document.getElementById('result');
let letterInput = document.getElementById('letterInput');
function startGame(difficulty) {
incorrectGuesses = 0;
guessedLetters = [];
score = 0;
resultDisplay.textContent = '';
guessedLettersDisplay.textContent = 'Guessed Letters: ';
let randomIndex = Math.floor(Math.random() * words[difficulty].length);
selectedWord = words[difficulty][randomIndex];
wordDisplay.textContent = "_ ".repeat(selectedWord.length).trim();
scoreDisplay.textContent = "Score: " + score;
letterInput.value = '';
document.getElementById('try-again-btn').style.display = 'none'; // Hide "Try Again"
document.getElementById('go-back-btn').style.display = 'none'; // Hide "Go Back"
}
function guessLetter() {
let guessedLetter = letterInput.value.toLowerCase();
if (!guessedLetter || guessedLetters.includes(guessedLetter)) {
return;
}
guessedLetters.push(guessedLetter);
guessedLettersDisplay.textContent = "Guessed Letters: " + guessedLetters.join(", ");
if (selectedWord.includes(guessedLetter)) {
updateWordDisplay();
score++;
scoreDisplay.textContent = "Score: " + score;
} else {
incorrectGuesses++;
if (incorrectGuesses >= maxIncorrectGuesses) {
resultDisplay.textContent = `Game Over! The word was "${selectedWord}". Your final score is ${score}.`;
resultDisplay.style.color = 'red';
disableGame();
}
}
letterInput.value = '';
checkGameStatus();
}
function updateWordDisplay() {
let displayedWord = selectedWord.split('').map(letter => guessedLetters.includes(letter) ? letter : '_').join(' ');
wordDisplay.textContent = displayedWord;
if (!displayedWord.includes('_')) {
resultDisplay.textContent = `You Win! The word was "${selectedWord}". Your score is ${score}.`;
resultDisplay.style.color = 'green';
disableGame();
}
}
function checkGameStatus() {
if (incorrectGuesses >= maxIncorrectGuesses) {
resultDisplay.textContent = `Game Over! The word was "${selectedWord}". Your score is ${score}.`;
resultDisplay.style.color = 'red';
disableGame();
}
}
function disableGame() {
letterInput.disabled = true;
document.querySelectorAll(".btn").forEach(btn => btn.disabled = true);
document.getElementById('try-again-btn').style.display = 'inline-block'; // Show "Try Again"
document.getElementById('go-back-btn').style.display = 'inline-block'; // Show "Go Back"
}
function tryAgain() {
// Restart the game by calling startGame with the current difficulty
const currentDifficulty = document.querySelector(".difficulty .btn:active").textContent.toLowerCase();
startGame(currentDifficulty);
}
function goBack() {
// Go back to the previous page or homepage
window.history.back(); // Uncomment this for going back to the previous page
// Or you can redirect to a homepage like:
// window.location.href = 'index.html'; // Uncomment and replace with your homepage URL
}
</script>
</body>
</html>