Okay, this is a fun challenge! Creating an interactive map game in a single HTML file means all CSS and JavaScript must be inline. We'll make a simple "Find the Hidden Treasure" game on a generic island map.
Game Concept:
- A simple island map image.
- Several clickable "spots" on the map.
- A certain number of these spots will be designated as "treasures" at random.
- The player clicks spots to find the treasures.
- Messages guide the player (e.g., "Found a treasure!", "Nothing here...").
- Score tracks found treasures.
- Win condition when all treasures are found.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Treasure Island Hunt</title>
<style>
/* Basic Resets & Body Styles */
body {
font-family: 'Arial', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
min-height: 100vh;
margin: 0;
background-color: #87CEEB; /* Sky blue */
color: #333;
padding: 20px;
box-sizing: border-box; /* Include padding in element's total width and height */
}
h1 {
color: #4CAF50; /* Green */
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
margin-bottom: 20px;
font-size: 1.8em; /* Adjust for mobile */
}
/* Game Container */
#game-container {
background-color: #fff;
border: 5px solid #A0522D; /* Brown border for a treasure map feel */
border-radius: 15px;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
width: 100%;
max-width: 600px; /* Limit max width for larger screens */
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
padding: 15px;
box-sizing: border-box;
}
/* Map Area - Uses padding-bottom trick for aspect ratio */
#map-area {
position: relative;
width: 100%;
padding-bottom: 75%; /* 4:3 aspect ratio (e.g., 600px width / 450px height) */
background-image: url('https://i.imgur.com/your-island-map-image.png'); /* REPLACE WITH YOUR IMAGE URL */
/* For now, using a placeholder green background if no image is available */
background-color: #7CFC00; /* Placeholder green for island */
background-size: cover;
background-position: center;
border-radius: 10px;
overflow: hidden; /* Ensures treasure spots don't overflow map boundary */
margin-bottom: 20px;
box-shadow: inset 0 0 10px rgba(0,0,0,0.3);
}
/* Placeholder image if the external URL doesn't load */
#map-area[style*="background-color: #7CFC00"]::before {
content: "🌴 Placeholder Island Map 🌴";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 1.5em;
color: rgba(0,0,0,0.5);
text-align: center;
pointer-events: none; /* Allows clicks to pass through */
}
/* Treasure Spots */
.treasure-spot {
position: absolute;
width: 8%; /* Size relative to map area */
height: 8%;
background-color: rgba(0, 0, 0, 0.4); /* Hidden state: semi-transparent */
border-radius: 50%; /* Make them circles */
cursor: pointer;
border: 2px solid rgba(255, 255, 255, 0.6);
box-shadow: 0 0 5px rgba(0,0,0,0.5);
transition: background-color 0.3s ease, transform 0.1s ease;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.8em;
color: white;
font-weight: bold;
opacity: 0; /* Hidden by default */
transform: scale(0.8); /* Start slightly smaller */
pointer-events: none; /* Disabled until game starts */
}
.treasure-spot:active {
transform: scale(0.95); /* Slight squash on tap */
}
/* States for treasure spots */
.treasure-spot.active {
opacity: 1; /* Visible when game starts */
transform: scale(1);
pointer-events: auto; /* Enable clicks */
}
.treasure-spot.found {
background-color: gold; /* Found state: gold! */
border-color: #DAA520; /* Darker gold border */
cursor: default;
animation: pulse 0.5s ease-in-out;
}
.treasure-spot.found::after {
content: '⭐'; /* Add a star for found treasures */
font-size: 1.5em;
}
.treasure-spot.miss {
background-color: rgba(255, 0, 0, 0.5); /* Missed state: semi-transparent red */
border-color: rgba(255, 0, 0, 0.8);
cursor: default;
animation: fadeOut 0.8s forwards; /* Fade out after a miss */
}
/* Animation for found treasure */
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
/* Animation for missed spot */
@keyframes fadeOut {
0% { opacity: 1; transform: scale(1); }
100% { opacity: 0; transform: scale(0.5); }
}
/* Specific positions for treasure spots - these are crucial for your map layout */
/* You will need to adjust these percentages based on your actual map image */
/* These are just arbitrary examples */
.spot-1 { top: 15%; left: 20%; }
.spot-2 { top: 30%; left: 70%; }
.spot-3 { top: 60%; left: 40%; }
.spot-4 { top: 80%; left: 10%; }
.spot-5 { top: 5%; left: 55%; }
.spot-6 { top: 45%; left: 10%; }
.spot-7 { top: 70%; left: 80%; }
.spot-8 { top: 25%; left: 45%; }
.spot-9 { top: 50%; left: 25%; }
.spot-10 { top: 10%; left: 85%; }
/* Controls Area */
#controls {
width: 100%;
text-align: center;
margin-top: 15px;
}
#message {
font-size: 1.1em;
color: #0056b3;
min-height: 2em; /* Prevent layout shift */
margin-bottom: 10px;
}
#score {
font-size: 1.2em;
font-weight: bold;
color: #D2691E; /* Chocolate */
margin-bottom: 15px;
}
/* Buttons */
.game-button {
background-color: #4CAF50; /* Green */
color: white;
padding: 12px 25px;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 1.1em;
margin: 5px;
transition: background-color 0.3s ease, transform 0.1s ease;
box-shadow: 0 4px #367c39; /* Darker green shadow */
}
.game-button:hover {
background-color: #45a049;
transform: translateY(-2px); /* Lift effect on hover */
}
.game-button:active {
background-color: #367c39;
transform: translateY(2px); /* Press effect */
box-shadow: 0 2px #367c39;
}
/* Hide elements initially or based on game state */
.hidden {
display: none !important;
}
/* Mobile specific adjustments */
@media (max-width: 480px) {
h1 {
font-size: 1.5em;
}
#game-container {
padding: 10px;
}
.game-button {
padding: 10px 20px;
font-size: 1em;
}
.treasure-spot {
width: 10%;
height: 10%;
}
.treasure-spot.found::after {
font-size: 1.2em;
}
}
/* Optional: Add a simple compass rose or other map elements */
#compass {
position: absolute;
width: 15%;
height: auto;
top: 5%;
right: 5%;
z-index: 10;
opacity: 0.7;
/* Using a simple image for compass if available */
/* background-image: url('https://i.imgur.com/your-compass-image.png'); */
/* background-size: contain;
background-repeat: no-repeat;
background-position: center; */
/* Placeholder for compass */
background-color: rgba(255,255,255,0.7);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.8em;
text-align: center;
padding-top: 15%; /* For aspect ratio of circle */
box-sizing: border-box;
color: #333;
font-weight: bold;
}
#compass::before {
content: "N\nW"; /* Unicode for North, South, East, West */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
line-height: 1;
white-space: pre-wrap;
}
#compass::after {
content: "E\nS";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
line-height: 1;
white-space: pre-wrap;
}
/* Make only the letters visible in the compass placeholder */
#compass { font-size: 0.8em; }
#compass::before { content: "N"; top: 10%; left: 50%; transform: translateX(-50%); }
#compass::after { content: "S"; bottom: 10%; left: 50%; transform: translateX(-50%); }
#compass span { position: absolute; }
#compass span:nth-child(1) { left: 10%; top: 50%; transform: translateY(-50%); content: "W"; }
#compass span:nth-child(2) { right: 10%; top: 50%; transform: translateY(-50%); content: "E"; }
</style>
</head>
<body>
<h1>Treasure Island Hunt!</h1>
<div id="game-container">
<div id="map-area">
<!-- Treasure Spots - These are the clickable areas -->
<!-- Adjust top/left percentages to match your actual map image -->
<div class="treasure-spot spot-1" data-spot-id="1"></div>
<div class="treasure-spot spot-2" data-spot-id="2"></div>
<div class="treasure-spot spot-3" data-spot-id="3"></div>
<div class="treasure-spot spot-4" data-spot-id="4"></div>
<div class="treasure-spot spot-5" data-spot-id="5"></div>
<div class="treasure-spot spot-6" data-spot-id="6"></div>
<div class="treasure-spot spot-7" data-spot-id="7"></div>
<div class="treasure-spot spot-8" data-spot-id="8"></div>
<div class="treasure-spot spot-9" data-spot-id="9"></div>
<div class="treasure-spot spot-10" data-spot-id="10"></div>
<!-- Optional Compass Rose -->
<div id="compass">
<span>W</span>
<span>E</span>
</div>
</div>
<div id="controls">
<p id="message">Find all the hidden treasures!</p>
<p id="score">Treasures Found: 0 / 0</p>
<button id="startButton" class="game-button">Start Game</button>
<button id="resetButton" class="game-button hidden">Play Again?</button>
</div>
</div>
<script>
const messageDisplay = document.getElementById('message');
const scoreDisplay = document.getElementById('score');
const startButton = document.getElementById('startButton');
const resetButton = document.getElementById('resetButton');
const treasureSpots = document.querySelectorAll('.treasure-spot');
let foundTreasures = 0;
let totalTreasuresToFind = 0;
let gameActive = false;
let actualTreasureSpots = []; // Stores the indices of the actual treasures
// Helper function to shuffle an array (Fisher-Yates algorithm)
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function initGame() {
gameActive = false;
foundTreasures = 0;
totalTreasuresToFind = Math.floor(treasureSpots.length / 3) + 1; // Example: 1/3 of spots are treasures, min 1
if (totalTreasuresToFind > treasureSpots.length) totalTreasuresToFind = treasureSpots.length;
// Reset all spots
treasureSpots.forEach(spot => {
spot.classList.remove('found', 'miss', 'active');
spot.style.opacity = '0'; // Ensure opacity reset for fadeOut animation
spot.style.transform = 'scale(0.8)'; // Ensure transform reset
spot.onclick = null; // Remove previous click handlers
spot.removeAttribute('data-is-treasure');
});
// Determine which spots are treasures randomly
const allSpotIndices = Array.from({ length: treasureSpots.length }, (_, i) => i);
shuffleArray(allSpotIndices);
actualTreasureSpots = allSpotIndices.slice(0, totalTreasuresToFind);
actualTreasureSpots.forEach(index => {
treasureSpots[index].setAttribute('data-is-treasure', 'true');
});
messageDisplay.textContent = 'Click "Start Game" to begin your hunt!';
scoreDisplay.textContent = `Treasures Found: ${foundTreasures} / ${totalTreasuresToFind}`;
startButton.classList.remove('hidden');
resetButton.classList.add('hidden');
}
function startGame() {
gameActive = true;
messageDisplay.textContent = 'Begin your search! Click the island spots.';
startButton.classList.add('hidden');
resetButton.classList.remove('hidden');
treasureSpots.forEach(spot => {
spot.classList.add('active'); // Make spots visible and clickable
spot.onclick = handleSpotClick; // Add click handler
});
}
function handleSpotClick(event) {
if (!gameActive) return;
const clickedSpot = event.currentTarget; // Use currentTarget as handler is on the element itself
// If already found or processed, ignore
if (clickedSpot.classList.contains('found') || clickedSpot.classList.contains('miss')) {
return;
}
// Check if this spot is an actual treasure
const isTreasure = clickedSpot.getAttribute('data-is-treasure') === 'true';
if (isTreasure) {
clickedSpot.classList.add('found');
foundTreasures++;
messageDisplay.textContent = '🌟 You found a treasure! Keep going!';
} else {
clickedSpot.classList.add('miss');
messageDisplay.textContent = 'Nada! Nothing here... try another spot.';
// Briefly keep the 'miss' class and then remove it visually after animation
setTimeout(() => {
// This spot effectively disappears after animation for a clean UI
clickedSpot.style.pointerEvents = 'none'; // Disable further clicks on this spot
}, 800); // Match fadeOut animation duration
}
scoreDisplay.textContent = `Treasures Found: ${foundTreasures} / ${totalTreasuresToFind}`;
if (foundTreasures === totalTreasuresToFind) {
endGame(true); // Player won
}
}
function endGame(won) {
gameActive = false;
if (won) {
messageDisplay.textContent = '🎉 Congratulations! You found all the treasures!';
} else {
messageDisplay.textContent = 'Game Over. Try again!'; // (Not currently used in this game, but good to have)
}
resetButton.textContent = 'Play Again?'; // Update button text
resetButton.classList.remove('hidden'); // Ensure reset button is visible
// Disable clicks on all spots
treasureSpots.forEach(spot => {
spot.onclick = null;
spot.classList.remove('active'); // Remove active state
if (!spot.classList.contains('found')) {
// For unfound non-treasures, make them fully transparent, or reveal true treasures
if (spot.getAttribute('data-is-treasure') === 'true') {
spot.classList.add('found'); // Reveal remaining treasures
spot.style.opacity = '1';
spot.style.transform = 'scale(1)';
} else {
spot.style.opacity = '0'; // Hide missed spots or non-treasures
}
}
});
}
// Event Listeners
startButton.addEventListener('click', startGame);
resetButton.addEventListener('click', initGame);
// Initialize the game when the page loads
document.addEventListener('DOMContentLoaded', initGame);
// IMPORTANT: Replace the placeholder image URL in the CSS!
// background-image: url('https://i.imgur.com/your-island-map-image.png');
// You can find free map images on sites like Pixabay, Unsplash, or create your own.
// For example: https://i.imgur.com/p14w20G.png (A simple green blob, not an actual island)
// Or search for "cartoon island map" or "treasure map" on royalty-free image sites.
// The current placeholder in CSS is a basic green background.
</script>
</body>
</html>
How to Use and Customize:
-
Save the Code: Save the entire block of code above as an .html file (e.g., treasure_hunt.html).
-
Open in Browser: Open this HTML file with any web browser on your computer or mobile device.
-
Critical Customization: The Map Image:
-
Customize Treasure Spot Positions:
-
Number of Treasures:
- In the JavaScript
initGame() function, this line determines how many treasures will be hidden:
Enjoy your mobile-friendly interactive map game!