gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
make a cool js code
- User
make a cool js code
- Assistant
make a cool js code
Sure! Here’s an example of a fun JavaScript code that creates a simple interactive game: a clicking game where the player has to click buttons that appear randomly on the screen. The goal is to click as many buttons as possible within a time limit.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clicking Game</title>
<style>
body { display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f0f0f0; }
#gameArea { position: relative; width: 80vw; height: 80vh; border: 2px solid #333; overflow: hidden; }
.clickMe { position: absolute; padding: 10px; background-color: #4CAF50; color: white; border: none; cursor: pointer; }
#score { font-size: 24px; margin-top: 20px; }
</style>
</head>
<body>
<div id="gameArea"></div>
<div id="score">Score: 0</div>
<button id="startBtn">Start Game</button>
<script>
let score = 0;
let gameInterval;
const gameArea = document.getElementById('gameArea');
const scoreDisplay = document.getElementById('score');
const startBtn = document.getElementById('startBtn');
startBtn.addEventListener('click', startGame);
function startGame() {
score = 0;
scoreDisplay.textContent = "Score: " + score;
startBtn.disabled = true;
gameInterval = setInterval(showRandomButton, 1000); // Show button every second
setTimeout(() => {
clearInterval(gameInterval);
alert("Time's up! Your score: " + score);
startBtn.disabled = false;
}, 10000); // Game lasts for 10 seconds
}
function showRandomButton() {
const button = document.createElement('button');
button.textContent = 'Click Me!';
button.className = 'clickMe';
const x = Math.random() * (gameArea.clientWidth - 100); // Random x position
const y = Math.random() * (gameArea.clientHeight - 50); // Random y position
button.style.left = x + 'px';
button.style.top = y + 'px';
button.addEventListener('click', () => {
score++;
scoreDisplay.textContent = "Score: " + score;
gameArea.removeChild(button); // Remove button after click
});
gameArea.appendChild(button);
// Remove button after 1 second if not clicked
setTimeout(() => {
if (button.parentNode) {
gameArea.removeChild(button);
}
}, 1000);
}
</script>
</body>
</html>
.html extension (e.g. clicking_game.html).Feel free to enhance the game with more features or customize its styling!
Not yet included in a published release.