o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
create mini game at html code
- User
create mini game at html code
- Assistant
create mini game at html code
Certainly! Here's a simple "Whack-a-Mole" mini-game created using HTML, CSS, and JavaScript. In this game, moles appear randomly on the screen, and you have to click on them to score points within a time limit.
Instructions:
whack-a-mole.html. Ensure the file extension is .html.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Whack-a-Mole Mini Game</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
background-color: #f0f8ff;
user-select: none;
}
h1 {
color: #3e8e41;
}
#game {
position: relative;
width: 600px;
height: 400px;
margin: 20px auto;
background-color: #8FBC8F;
border: 2px solid #556B2F;
overflow: hidden;
}
.mole {
position: absolute;
width: 60px;
height: 60px;
background-image: url('https://i.imgur.com/4bKx1B8.png');
background-size: cover;
cursor: pointer;
}
#scoreboard {
margin: 20px;
font-size: 24px;
}
#startButton {
padding: 10px 20px;
font-size: 18px;
background-color: #3e8e41;
color: white;
border: none;
cursor: pointer;
}
#startButton:disabled {
background-color: #grey;
cursor: not-allowed;
}
</style>
</head>
<body>
<h1>Whack-a-Mole Mini Game</h1>
<div id="scoreboard">Score: 0</div>
<button id="startButton">Start Game</button>
<div id="game"></div>
<script>
const gameArea = document.getElementById('game');
const scoreboard = document.getElementById('scoreboard');
const startButton = document.getElementById('startButton');
let score = 0;
let gameDuration = 30000; // 30 seconds
let moleAppearInterval = 800; // Moles appear every 0.8 seconds
let moleDisplayTime = 600; // Each mole stays for 0.6 seconds
let gameTimer;
let moleTimer;
function randomPosition() {
const x = Math.floor(Math.random() * (gameArea.clientWidth - 60));
const y = Math.floor(Math.random() * (gameArea.clientHeight - 60));
return { x, y };
}
function createMole() {
const mole = document.createElement('div');
mole.classList.add('mole');
const pos = randomPosition();
mole.style.left = pos.x + 'px';
mole.style.top = pos.y + 'px';
mole.addEventListener('click', () => {
score++;
scoreboard.textContent = 'Score: ' + score;
mole.remove();
});
gameArea.appendChild(mole);
setTimeout(() => {
mole.remove();
}, moleDisplayTime);
}
function startGame() {
score = 0;
scoreboard.textContent = 'Score: ' + score;
startButton.disabled = true;
gameArea.innerHTML = ''; // Clear any remaining moles
moleTimer = setInterval(createMole, moleAppearInterval);
gameTimer = setTimeout(() => {
clearInterval(moleTimer);
startButton.disabled = false;
alert('Game Over! Your score is ' + score);
}, gameDuration);
}
startButton.addEventListener('click', startGame);
</script>
</body>
</html>
Feel free to modify and enhance the game to your liking:
background-image: url('your-image-url.png');
gameDuration (in milliseconds) to set how long the game lasts.moleAppearInterval to adjust how frequently moles appear.moleDisplayTime to change how long moles stay on the screen.background-image URL to the image file name.Enjoy your mini-game! If you have any questions or need further assistance, feel free to ask.