Sure! Here's a fun and creative "Emoji Catcher" game where you control a basket to catch falling emojis. The game is implemented entirely in a single HTML file using HTML5 Canvas and JavaScript. Enjoy catching those funny emojis!
How to Play:
Move the Basket: Use your mouse to move the basket left and right along the bottom of the screen.
Catch Emojis: Try to catch as many falling emojis as possible. Each emoji caught increases your score.
Avoid Missing: If you miss too many emojis, the game is over!
Feel free to customize and expand upon this game as you like. Have fun!
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Emoji Catcher Game</title>
<style>
body {
margin: 0;
padding: 0;
background: #f0f8ff;
overflow: hidden;
font-family: Arial, sans-serif;
}
#gameCanvas {
display: block;
margin: 0 auto;
background: linear-gradient(to bottom, #87cefa, #f0f8ff);
border: 2px solid #000;
}
#scoreBoard {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
font-size: 24px;
font-weight: bold;
color: #333;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 36px;
font-weight: bold;
color: red;
display: none;
text-align: center;
}
#restartButton {
margin-top: 20px;
padding: 10px 20px;
font-size: 20px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
#restartButton:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div id="scoreBoard">Score: 0</div>
<div id="gameOver">
Game Over!<br>
Your Score: <span id="finalScore">0</span><br>
<button id="restartButton">Play Again</button>
</div>
<canvas id="gameCanvas" width="600" height="800"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreBoard = document.getElementById('scoreBoard');
const gameOverDiv = document.getElementById('gameOver');
const finalScoreSpan = document.getElementById('finalScore');
const restartButton = document.getElementById('restartButton');
const basketImgSrc = 'https://i.imgur.com/6X5Z4wV.png'; // Basket image URL
const emojiImages = [
'https://twemoji.maxcdn.com/v/latest/72x72/1f600.png', // 😀
'https://twemoji.maxcdn.com/v/latest/72x72/1f923.png', // 😂
'https://twemoji.maxcdn.com/v/latest/72x72/1f604.png', // 😄
'https://twemoji.maxcdn.com/v/latest/72x72/1f60d.png', // 😍
'https://twemoji.maxcdn.com/v/latest/72x72/1f47b.png', // 👻
'https://twemoji.maxcdn.com/v/latest/72x72/1f929.png', // 🤩
'https://twemoji.maxcdn.com/v/latest/72x72/1f60e.png', // 😎
'https://twemoji.maxcdn.com/v/latest/72x72/1f632.png' // 😲
];
let basket = {
x: canvas.width / 2 - 50,
y: canvas.height - 100,
width: 100,
height: 50,
image: new Image(),
speed: 0
};
basket.image.src = basketImgSrc;
let emojis = [];
let emojiSpeed = 2;
let emojiFrequency = 60; // Frames between emoji drops
let frameCount = 0;
let score = 0;
let missed = 0;
const maxMissed = 10;
let gameOver = false;
// Load basket image
basket.image.onload = function() {
draw();
};
// Handle mouse movement
canvas.addEventListener('mousemove', function(e) {
const rect = canvas.getBoundingClientRect();
const mouseX = e.clientX - rect.left;
basket.x = mouseX - basket.width / 2;
// Prevent basket from going out of bounds
if (basket.x < 0) basket.x = 0;
if (basket.x + basket.width > canvas.width) basket.x = canvas.width - basket.width;
});
// Emoji class
class Emoji {
constructor() {
this.size = 50;
this.x = Math.random() * (canvas.width - this.size);
this.y = -this.size;
this.speed = emojiSpeed + Math.random(); // Slight variation in speed
this.image = new Image();
this.image.src = emojiImages[Math.floor(Math.random() * emojiImages.length)];
this.caught = false;
}
update() {
this.y += this.speed;
}
draw() {
ctx.drawImage(this.image, this.x, this.y, this.size, this.size);
}
}
// Spawn a new emoji
function spawnEmoji() {
emojis.push(new Emoji());
}
// Update game objects
function update() {
if (gameOver) return;
frameCount++;
if (frameCount % emojiFrequency === 0) {
spawnEmoji();
}
emojis.forEach((emoji, index) => {
emoji.update();
// Check for collision with basket
if (
emoji.x < basket.x + basket.width &&
emoji.x + emoji.size > basket.x &&
emoji.y < basket.y + basket.height &&
emoji.y + emoji.size > basket.y
) {
score++;
emojis.splice(index, 1);
}
// Remove emojis that are out of bounds
else if (emoji.y > canvas.height) {
missed++;
emojis.splice(index, 1);
if (missed >= maxMissed) {
endGame();
}
}
});
}
// Draw game objects
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw basket
ctx.drawImage(basket.image, basket.x, basket.y, basket.width, basket.height);
// Draw emojis
emojis.forEach(emoji => {
emoji.draw();
});
// Update score
scoreBoard.textContent = `Score: ${score}`;
// Continue the game loop
if (!gameOver) {
requestAnimationFrame(draw);
}
}
// Game loop
function gameLoop() {
update();
draw();
if (!gameOver) {
requestAnimationFrame(gameLoop);
}
}
// End the game
function endGame() {
gameOver = true;
finalScoreSpan.textContent = score;
gameOverDiv.style.display = 'block';
}
// Restart the game
restartButton.addEventListener('click', function() {
// Reset game variables
emojis = [];
score = 0;
missed = 0;
frameCount = 0;
gameOver = false;
gameOverDiv.style.display = 'none';
// Start the game loop again
gameLoop();
});
// Start the game when basket image is loaded
basket.image.onload = function() {
gameLoop();
};
</script>
</body>
</html>
Explanation of the Code:
HTML Structure:
The <canvas> element is used for rendering the game.
#scoreBoard displays the current score at the top center.
#gameOver is a hidden div that appears when the game ends, showing the final score and a restart button.
CSS Styling:
Styles ensure the canvas is centered and the background is visually appealing.
The game over screen is styled to overlay the canvas when the game ends.
JavaScript Functionality:
Basket: An object representing the player's basket, controlled by mouse movement.
Emojis: A class representing falling emojis with random types and speeds.
Game Loop: Uses requestAnimationFrame to continuously update and render the game state.
Collision Detection: Checks if an emoji overlaps with the basket to increase the score.
Game Over Condition: If the player misses more than a set number of emojis, the game ends.
Restart Functionality: Allows the player to restart the game after it ends.
Assets:
The basket image and various emoji images are loaded from external URLs. You can replace these URLs with your own images if desired.
Enjoy the Game!
Open the HTML file in your web browser, and start catching those funny emojis! Customize the game by adding more emojis, increasing difficulty levels, or enhancing the graphics to make it even more enjoyable.