<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>I Spy Game</title>
	<link rel="stylesheet" href="style.css">
</head>
<body>
	<h1>I Spy Game</h1>
	<div class="game-container">
		<div class="game-board">
			<!-- Game board with hidden objects -->
		</div>
		<div class="game-items">
			<!-- List of items to find -->
		</div>
	</div>
	<script src="app.js"></script>
</body>
</html>
<pre><code class="language-css">/* Reset all margin and padding */
*
{
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}

/* Style for the game title */
h1
{
	text-align: center;
	margin-top: 20px;
	font-size: 36px;
}

/* Style for the game container */
.game-container
{
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	align-items: center;
	margin-top: 20px;
}

/* Style for the game board */
.game-board
{
	width: 400px;
	height: 400px;
	border: 5px solid black;
	background-color: #ccc;
	position: relative;
	margin-right: 20px;
	margin-bottom: 20px;
}

/* Style for the game items list */
.game-items
{
	width: 200px;
	height: 400px;
	border: 5px solid black;
	background-color: #eee;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
}
</code></pre>
<pre><code class="language-javascript">// List of objects to find
const items = [
	'Car',
	'Tree',
	'Dog',
	'Cat',
	'House',
	'Sun',
	'Flower',
	'Star',
	'Chair',
	'Book',
	'Clock',
	'Shoe'
];

// Shuffle items array to randomize the order of items
items.sort(() =&gt; Math.random() - 0.5);

// Add items to game items list
for (let i = 0; i &lt; items.length; i++) {
	// Create a new div for each item
	const item = document.createElement('div');
	item.classList.add('item');
	item.innerText = items[i];
	// Add the item to the game items list
	document.querySelector('.game-items').appendChild(item);
}

// Add hidden objects to game board
for (let i = 0; i &lt; 10; i++) {
	// Create a new div for each hidden object
	const object = document.createElement('div');
	object.classList.add('object');
	// Randomly position the object within the game board
	object.style.top = Math.floor(Math.random() * 350) + 'px';
	object.style.left = Math.floor(Math.random() * 350) + 'px';
	// Add the object to the game board
	document.querySelector('.game-board').appendChild(object);
}

// Click event to find objects
let found = 0;
document.querySelectorAll('.object').forEach(object =&gt; {
	object.addEventListener('click', () =&gt; {
		// When an object is clicked, hide it
		object.style.visibility = 'hidden';
		// Increase the count of found objects
		found++;
		// If all objects have been found, show a congratulatory message
		if (found === 10) {
			alert('Congratulations! You found all the objects!');
		}
	});
});
</code></pre>
I Spy Game: Find Hidden Objects - HTML, CSS, JavaScript

原文地址: https://www.cveoy.top/t/topic/njsK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录