Certainly! Below is a simple implementation of the game using HTML, CSS, and JavaScript. The game features randomly generated rooms with doors, items, clues, and an unfolding story that allows the player to eventually retrace their steps.
HTML (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mysterious Maze Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="game-container">
<div id="game-screen">
<h1 id="room-title">Room</h1>
<p id="room-description"></p>
<div id="doors-container"></div>
<button id="go-back-button" style="display: none;">Go Back</button>
</div>
<div id="inventory">
<h2>Inventory</h2>
<ul id="items-list"></ul>
<h2>Clues</h2>
<ul id="clues-list"></ul>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css):
body {
margin: 0;
font-family: Arial, sans-serif;
background-color: #000;
color: #fff;
}
#game-container {
display: flex;
}
#game-screen {
flex: 2;
padding: 20px;
}
#inventory {
flex: 1;
padding: 20px;
background-color: #111;
border-left: 1px solid #444;
}
#doors-container {
margin-top: 20px;
}
.door-button {
padding: 10px 20px;
margin: 5px;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
}
.door-button:hover {
background-color: #555;
}
#go-back-button {
margin-top: 20px;
padding: 10px 20px;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
}
#go-back-button:hover {
background-color: #555;
}
h1, h2, p {
color: #fff;
}
JavaScript (script.js):
// Define the game state
const gameState = {
rooms: [],
currentRoomIndex: -1,
inventory: [],
clues: [],
canGoBack: false,
hasTorch: false
};
// List of possible items and clues
const items = ['Torch'];
const clues = ['A mysterious symbol on the wall', 'Faint whispering sounds', 'An old, torn note'];
// Initialize the game
function initGame() {
// Start with the first room
enterNewRoom();
}
// Generate a new room
function generateRoom() {
// Randomly decide the number of doors (2 to 4)
const numDoors = Math.floor(Math.random() * 3) + 2;
const room = {
doors: numDoors,
item: null,
clue: null,
description: 'You are in a dark room. There are ' + numDoors + ' doors in front of you.',
previousRoomIndex: gameState.currentRoomIndex
};
// Randomly decide if the room has an item or clue
if (!gameState.hasTorch && Math.random() < 0.3) {
room.item = 'Torch';
} else if (gameState.hasTorch && Math.random() < 0.3) {
room.clue = clues[Math.floor(Math.random() * clues.length)];
}
return room;
}
// Enter a new room
function enterNewRoom() {
const room = generateRoom();
gameState.rooms.push(room);
gameState.currentRoomIndex++;
updateRoomDisplay();
}
// Go back to the previous room
function goBack() {
if (gameState.canGoBack && gameState.currentRoomIndex > 0) {
gameState.currentRoomIndex--;
updateRoomDisplay();
}
}
// Update the display based on the current room
function updateRoomDisplay() {
const room = gameState.rooms[gameState.currentRoomIndex];
document.getElementById('room-title').innerText = `Room ${gameState.currentRoomIndex + 1}`;
document.getElementById('room-description').innerText = room.description;
// Update doors
const doorsContainer = document.getElementById('doors-container');
doorsContainer.innerHTML = '';
for (let i = 0; i < room.doors; i++) {
const doorButton = document.createElement('button');
doorButton.innerText = `Door ${i + 1}`;
doorButton.classList.add('door-button');
doorButton.onclick = () => {
checkForVoices();
enterNewRoom();
};
doorsContainer.appendChild(doorButton);
}
// Check for item
if (room.item && !gameState.inventory.includes(room.item)) {
gameState.inventory.push(room.item);
updateInventoryDisplay();
if (room.item === 'Torch') {
gameState.hasTorch = true;
alert('You found a Torch! You can now find clues.');
}
}
// Check for clue
if (room.clue) {
gameState.clues.push(room.clue);
updateCluesDisplay();
alert(`You found a clue: "${room.clue}"`);
}
// Update Go Back button
const goBackButton = document.getElementById('go-back-button');
if (gameState.canGoBack && gameState.currentRoomIndex > 0) {
goBackButton.style.display = 'block';
} else {
goBackButton.style.display = 'none';
}
goBackButton.onclick = () => {
checkForExit();
goBack();
};
}
// Update inventory display
function updateInventoryDisplay() {
const itemsList = document.getElementById('items-list');
itemsList.innerHTML = '';
gameState.inventory.forEach(item => {
const li = document.createElement('li');
li.innerText = item;
itemsList.appendChild(li);
});
}
// Update clues display
function updateCluesDisplay() {
const cluesList = document.getElementById('clues-list');
cluesList.innerHTML = '';
gameState.clues.forEach(clue => {
const li = document.createElement('li');
li.innerText = clue;
cluesList.appendChild(li);
});
}
// Randomly trigger voices talking to the player
function checkForVoices() {
if (!gameState.canGoBack && Math.random() < 0.2) {
alert('A mysterious voice whispers: "Sometimes the only way forward is back..."');
gameState.canGoBack = true;
}
}
// Check if the player returns to the start
function checkForExit() {
if (gameState.currentRoomIndex === 1) {
alert('You realize the exit was behind you all along. You step through the door and escape!');
resetGame();
}
}
// Reset the game
function resetGame() {
gameState.rooms = [];
gameState.currentRoomIndex = -1;
gameState.inventory = [];
gameState.clues = [];
gameState.canGoBack = false;
gameState.hasTorch = false;
updateInventoryDisplay();
updateCluesDisplay();
initGame();
}
// Start the game when the page loads
window.onload = initGame;
Explanation:
-
HTML Structure:
- The game is divided into two main sections:
- Game Screen: Displays the room title, description, and door options.
- Inventory: Shows the items and clues collected by the player.
-
CSS Styling:
- A dark theme is applied to match the game's atmosphere.
- Buttons and text are styled for better visibility and user experience.
-
JavaScript Logic:
- Game State Object (
gameState): Maintains the current state of the game, including rooms, inventory, clues, and flags for abilities like going back or having a torch.
- Initialization (
initGame): Starts the game by entering the first room.
- Room Generation (
generateRoom): Creates a new room with a random number of doors and randomly assigns items or clues based on whether the player has the torch.
- Entering Rooms (
enterNewRoom): Updates the game state when the player enters a new room and updates the display.
- Going Back (
goBack): Allows the player to return to the previous room if they have unlocked the ability.
- Updating Display Functions: Refresh the room description, inventory list, and clues list on the page.
- Voice Trigger (
checkForVoices): Randomly shows messages to the player, hinting that they can go back.
- Exit Check (
checkForExit): Checks if the player has returned to the starting room after unlocking the ability to go back. If so, the game resets.
Good Coding Practices Used:
- Separation of Concerns:
- HTML, CSS, and JavaScript are separated into different files for clarity and maintainability.
- Readable Code:
- Variable and function names are descriptive.
- Modular Functions:
- Code is divided into modular functions for specific tasks (e.g.,
generateRoom, updateRoomDisplay).
- Comments:
- Comments are added in the JavaScript to explain key parts of the code.
How to Run the Game:
-
Create the Files:
- Save the provided HTML code into a file named
index.html.
- Save the CSS code into a file named
styles.css.
- Save the JavaScript code into a file named
script.js.
-
Folder Structure:
- Ensure all files are in the same directory, or adjust the paths in the HTML file accordingly.
-
Open in Browser:
- Open
index.html in a modern web browser (e.g., Chrome, Firefox).
-
Play the Game:
- Click on the doors to navigate through rooms.
- Collect the Torch to start finding clues.
- Pay attention to the messages; once you can go back, try retracing your steps to the beginning to find the exit.
Potential Enhancements:
- Improved Storytelling:
- Add more variety to room descriptions and clues to enrich the backstory.
- Graphics and Animations:
- Include images and animations for doors, items, and rooms to enhance visual appeal.
- Sound Effects:
- Introduce background music and sound effects for a more immersive experience.
- Persistent State:
- Use
localStorage to save the game state so the player can return later.
- Mobile Responsiveness:
- Update CSS to ensure the game displays well on mobile devices.
Feel free to customize and expand upon this code to create a more complex and engaging game. Happy coding!