Sure! Below is a complete HTML code for a simple text-based hacking-themed game. The website has a black background with green text to emulate a hacker's computer aesthetic. The game is a basic "Guess the Password" game where the player has to guess the correct password within a limited number of attempts.
Instructions to Run:
- Copy the entire code below into a text editor.
- Save the file with a
.html extension, for example, hacker_game.html.
- Open the saved file in your web browser to play the game.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hacker's Challenge</title>
<style>
body {
background-color: #000;
color: #0f0;
font-family: 'Courier New', Courier, monospace;
margin: 0;
padding: 20px;
}
#gameContainer {
max-width: 600px;
margin: auto;
}
.inputField {
background-color: #000;
color: #0f0;
border: 1px solid #0f0;
padding: 10px;
font-size: 16px;
width: 100%;
box-sizing: border-box;
margin-top: 10px;
}
.button {
background-color: #0f0;
color: #000;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
margin-top: 10px;
}
.button:hover {
background-color: #0c0;
}
#output {
margin-top: 20px;
white-space: pre-wrap;
}
</style>
</head>
<body>
<div id="gameContainer">
<h1>Hacker's Challenge</h1>
<p>Welcome, Agent. Your mission is to infiltrate the secure server by guessing the correct password.</p>
<p>You have <span id="attempts">5</span> attempts remaining.</p>
<input type="text" id="passwordInput" class="inputField" placeholder="Enter password here" autofocus>
<button class="button" onclick="submitGuess()">Submit</button>
<div id="output"></div>
</div>
<script>
const correctPassword = "decrypt";
let attemptsLeft = 5;
function submitGuess() {
const userInput = document.getElementById('passwordInput').value.trim().toLowerCase();
const outputDiv = document.getElementById('output');
const attemptsSpan = document.getElementById('attempts');
if (userInput === "") {
outputDiv.textContent += "\n[ * ] Please enter a password.";
return;
}
if (userInput === correctPassword) {
outputDiv.textContent += "\n[ ✔ ] Access Granted! You've successfully infiltrated the server.";
endGame();
return;
} else {
attemptsLeft--;
attemptsSpan.textContent = attemptsLeft;
outputDiv.textContent += `\n[ ✖ ] Incorrect password. Attempts left: ${attemptsLeft}`;
if (attemptsLeft === 0) {
outputDiv.textContent += "\n[ !!! ] Access Denied! You've been locked out.";
endGame();
}
}
document.getElementById('passwordInput').value = '';
document.getElementById('passwordInput').focus();
}
function endGame() {
document.getElementById('passwordInput').disabled = true;
const buttons = document.getElementsByClassName('button');
for(let btn of buttons){
btn.disabled = true;
}
}
// Optional: Handle Enter key press
document.getElementById('passwordInput').addEventListener('keyup', function(event) {
if (event.key === 'Enter') {
submitGuess();
}
});
</script>
</body>
</html>
Features:
- Hacker Aesthetic: Black background and green monospaced text to mimic a terminal interface.
- Interactive Game: Users have to guess the correct password within 5 attempts.
- Responsive Feedback: The game provides immediate feedback on each guess.
- Accessibility: Input field is auto-focused, and pressing the Enter key submits the guess.
Enhancements:
You can further enhance the game by adding:
- Multiple Levels: Introduce different levels with varying difficulty.
- Random Passwords: Generate random passwords for each game session.
- Sound Effects: Add typing or success/failure sounds for immersion.
- Visual Effects: Include blinking cursor or ASCII art to enrich the hacker theme.
Feel free to modify and expand the game as per your requirements!