Okay, here is a single HTML file that creates a simple, mobile-friendly interactive map game. The game involves finding a hidden "treasure" on a grid by tapping cells.
Game Concept: "Grid Treasure Hunt"
- A grid of cells is displayed.
- One cell contains a hidden "treasure".
- The player taps cells to find the treasure.
- Tapping the correct cell reveals the treasure, awards a point, and ends the round.
- Tapping an incorrect cell provides feedback and costs a point (or just doesn't award one).
- A reset button allows restarting the game.
How to Use:
- Save the code below as an
.html file (e.g., map_game.html).
- Open the file in a web browser on your mobile device (or desktop browser, then resize the window to simulate mobile).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid Treasure Hunt</title>
<style>
/* Basic Reset & Body Styling */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
margin: 0;
text-align: center;
user-select: none; /* Prevent text selection during rapid tapping */
-webkit-user-select: none; /* Safari */
-webkit-touch-callout: none; /* iOS */
}
/* Game Container */
#game-container {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
width: 90%;
max-width: 400px; /* Max width for larger screens */
display: flex;
flex-direction: column;
align-items: center;
}
/* Title */
h1 {
color: #333;
margin-bottom: 15px;
}
/* Score and Message Display */
#score-area,
#message-area {
margin: 10px 0;
font-size: 1.1em;
color: #555;
min-height: 40px; /* Prevent layout shifts */
padding: 5px 10px;
background-color: #e7e7e7;
border-radius: 5px;
width: 100%;
box-sizing: border-box; /* Include padding in width */
}
#score-area {
font-weight: bold;
color: #007bff;
}
/* Map Grid Container */
#map-grid {
display: grid;
/* Columns will be set by JS */
gap: 8px;
width: 100%;
margin-top: 20px;
background-color: #ccc; /* Background for the grid area */
padding: 8px; /* Padding inside the grid area */
border-radius: 5px;
}
/* Individual Map Cells */
.cell {
background-color: #87CEEB; /* Sky blue */
border-radius: 5px;
aspect-ratio: 1 / 1; /* Makes cells square */
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5em; /* Size of the emoji */
cursor: pointer;
transition: background-color 0.3s ease, transform 0.1s ease;
color: transparent; /* Hide default content */
}
/* Cell Hover/Tap Effect */
.cell:hover:not(.found):not(.revealed) {
background-color: #ADD8E6; /* Lighter blue */
transform: scale(1.03);
}
/* Add touch feedback */
.cell:active:not(.found):not(.revealed) {
transform: scale(0.95);
}
/* Cell States */
.cell.revealed {
background-color: #d3d3d3; /* Light gray */
cursor: default;
transform: none;
}
.cell.revealed.wrong {
background-color: #ff7f7f; /* Light red */
color: white; /* Show wrong indicator */
cursor: default;
}
.cell.found {
background-color: #90EE90; /* Light green */
color: #28a745; /* Darker green */
cursor: default;
transform: scale(1.05); /* Slightly larger when found */
}
/* Reset Button */
#reset-button {
background-color: #007bff;
color: white;
border: none;
padding: 12px 25px;
border-radius: 5px;
font-size: 1em;
cursor: pointer;
margin-top: 25px;
transition: background-color 0.3s ease;
}
#reset-button:hover {
background-color: #0056b3;
}
#reset-button:active {
background-color: #004085;
transform: scale(0.98);
}
</style>
</head>
<body>
<div id="game-container">
<h1>Grid Treasure Hunt</h1>
<div id="score-area">Score: 0</div>
<div id="message-area">Tap a cell to find the treasure!</div>
<div id="map-grid">
<!-- Cells will be generated here by JavaScript -->
</div>
<button id="reset-button">New Game</button>
</div>
<script>
// --- Game Configuration ---
const GRID_ROWS = 4;
const GRID_COLS = 4;
const TOTAL_CELLS = GRID_ROWS * GRID_COLS;
const TREASURE_EMOJI = '💰'; // Or 🏆, ⭐, 💎 etc.
const WRONG_EMOJI = '❌';
// --- DOM Elements ---
const mapGrid = document.getElementById('map-grid');
const scoreArea = document.getElementById('score-area');
const messageArea = document.getElementById('message-area');
const resetButton = document.getElementById('reset-button');
// --- Game State ---
let targetCellIndex = -1;
let score = 0;
let gameOver = false;
let revealedCells = []; // Keep track of cells already clicked
// --- Functions ---
/** Initializes or resets the game */
function initGame() {
// Reset state variables
score = 0;
gameOver = false;
revealedCells = [];
messageArea.textContent = "Tap a cell to find the treasure!";
scoreArea.textContent = `Score: ${score}`;
mapGrid.innerHTML = ''; // Clear previous grid
// Set up grid columns dynamically
mapGrid.style.gridTemplateColumns = `repeat(${GRID_COLS}, 1fr)`;
// Choose a random target cell
targetCellIndex = Math.floor(Math.random() * TOTAL_CELLS);
// Create cells and add them to the DOM
for (let i = 0; i < TOTAL_CELLS; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.cellId = i; // Store the index
// Add click listener to each cell
cell.addEventListener('click', handleCellClick);
mapGrid.appendChild(cell);
}
}
/** Handles a click on a map cell */
function handleCellClick(event) {
if (gameOver) return; // Do nothing if game is over
const clickedCell = event.target;
const cellIndex = parseInt(clickedCell.dataset.cellId);
// Check if cell was already revealed
if (revealedCells.includes(cellIndex)) {
messageArea.textContent = "You already checked this cell!";
return;
}
// Add cell to revealed list and mark visually
revealedCells.push(cellIndex);
clickedCell.classList.add('revealed');
// Check if it's the target cell
if (cellIndex === targetCellIndex) {
clickedCell.textContent = TREASURE_EMOJI;
clickedCell.classList.add('found');
score += 1;
messageArea.textContent = "You found the treasure! 🎉";
gameOver = true; // End the game
} else {
clickedCell.textContent = WRONG_EMOJI;
clickedCell.classList.add('wrong');
score -= 1; // Penalty for wrong guess
messageArea.textContent = "Not here! Keep looking...";
// Optional: Check if player has run out of score or guesses if adding limits
}
// Update the score display
scoreArea.textContent = `Score: ${score}`;
// Optional: If score drops below zero, you might want to end the game
// if (score < 0) {
// gameOver = true;
// messageArea.textContent = "Game Over! You ran out of score.";
// // Reveal the treasure location
// const targetCellElement = mapGrid.querySelector(`.cell[data-cell-id="${targetCellIndex}"]`);
// if (targetCellElement) {
// targetCellElement.textContent = TREASURE_EMOJI;
// targetCellElement.classList.add('found');
// }
// }
}
// --- Event Listeners ---
resetButton.addEventListener('click', initGame);
// --- Initial Game Start ---
initGame(); // Start the game when the page loads
</script>
</body>
</html>
Explanation:
- HTML Structure: Sets up the basic page, title, viewport meta tag (essential for mobile responsiveness), and includes a
<style> block for all CSS and a <script> block for all JavaScript.
- CSS Styling:
- Uses flexbox to center the game container.
- Defines styles for the container, score display, message area, the grid itself (
#map-grid), and the individual cells (.cell).
- Uses
display: grid and grid-template-columns (set dynamically by JS) to create the grid layout.
aspect-ratio: 1 / 1 ensures the cells remain square regardless of width.
- Uses classes (
.revealed, .wrong, .found) to visually change the state of clicked cells.
- Includes basic
:hover and :active states for better visual feedback.
- JavaScript Logic:
- Configuration: Constants
GRID_ROWS, GRID_COLS, TREASURE_EMOJI, WRONG_EMOJI make it easy to adjust the game.
- DOM References: Gets references to the necessary HTML elements.
- Game State: Variables track the
targetCellIndex, current score, whether the game is gameOver, and which cells have been revealed.
initGame(): This function sets everything up: clears the board, picks a random target, creates the cell div elements, assigns a data-cell-id to each, attaches the handleCellClick listener to them, and sets the initial grid columns.
handleCellClick(): This is the core interaction function. It checks if the game is over or if the cell was already clicked. If it's a new click, it marks the cell as revealed. It then checks if the clicked cell matches the targetCellIndex. Based on the result, it updates the cell's appearance (emoji, background color), adjusts the score, and updates the message. If the treasure is found, it sets gameOver to true.
- Event Listeners: Attaches the
initGame function to the reset button's click event.
- Initial Call:
initGame() is called once at the end to start the game when the page loads.