Sure! Let's create a simple, fun, and creative web-based game called "Crazy Chicken Chase". In this game, you control a chicken that moves left and right at the bottom of the screen to catch falling eggs while avoiding mischievous foxes. The goal is to catch as many eggs as possible without letting any fall past you. The game includes humorous graphics and sounds to enhance the fun experience.
Game Features:
Player Control: Move the chicken left and right using arrow keys or on-screen buttons.
Falling Objects: Eggs fall from the top, and catching them increases your score. Foxes also fall, and avoiding them is crucial.
Score Tracking: Display your current score and high score.
Game Over Condition: If an egg or fox reaches the bottom without being caught or avoided, the game ends.
Responsive Design: Works on both desktop and mobile browsers.
Technologies Used:
HTML5 for structuring the game.
CSS3 for styling and animations.
JavaScript for game logic and interactions.
Below is the complete code for the game. You can save it as an .html file and open it in any modern web browser to play.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Crazy Chicken Chase</title>
<style>
body {
margin: 0;
padding: 0;
background: #87CEEB;
overflow: hidden;
font-family: Arial, sans-serif;
}
#gameCanvas {
background: linear-gradient(to bottom, #87CEEB 0%, #ffffff 100%);
display: block;
margin: 0 auto;
}
#scoreBoard {
position: absolute;
top: 10px;
left: 10px;
color: #333;
font-size: 20px;
background: rgba(255, 255, 255, 0.7);
padding: 5px 10px;
border-radius: 5px;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #ff0000;
font-size: 40px;
display: none;
text-align: center;
}
#restartBtn {
margin-top: 20px;
padding: 10px 20px;
font-size: 20px;
cursor: pointer;
background-color: #ffcc00;
border: none;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="scoreBoard">Score: 0</div>
<div id="gameOver">
<div>Game Over!</div>
<button id="restartBtn">Restart</button>
</div>
<canvas id="gameCanvas" width="480" height="640"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreBoard = document.getElementById('scoreBoard');
const gameOverDiv = document.getElementById('gameOver');
const restartBtn = document.getElementById('restartBtn');
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
let score = 0;
let highScore = 0;
let gameOver = false;
// Load Images
const chickenImg = new Image();
chickenImg.src = 'https://i.imgur.com/OdL0XPt.png'; // Chicken image
const eggImg = new Image();
eggImg.src = 'https://i.imgur.com/4AI6YQh.png'; // Egg image
const foxImg = new Image();
foxImg.src = 'https://i.imgur.com/ZXBt7Kk.png'; // Fox image
// Sounds
const catchSound = new Audio('https://www.soundjay.com/buttons/sounds/button-10.mp3');
const missSound = new Audio('https://www.soundjay.com/buttons/sounds/button-3.mp3');
const gameOverSound = new Audio('https://www.soundjay.com/misc/sounds/fail-trombone-01.mp3');
// Player
const player = {
x: WIDTH / 2 - 25,
y: HEIGHT - 80,
width: 50,
height: 50,
speed: 5,
dx: 0
};
// Falling Objects
const objects = [];
const objectTypes = ['egg', 'fox'];
function createObject() {
const type = objectTypes[Math.floor(Math.random() * objectTypes.length)];
const size = 40;
const x = Math.random() * (WIDTH - size);
const y = -size;
const speed = type === 'egg' ? 3 : 4;
objects.push({ type, x, y, width: size, height: size, speed });
}
// Control
const keys = {};
document.addEventListener('keydown', (e) => {
keys[e.code] = true;
});
document.addEventListener('keyup', (e) => {
keys[e.code] = false;
});
// Touch Controls for Mobile
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchend', handleTouchEnd, false);
let touchStartX = null;
function handleTouchStart(evt) {
touchStartX = evt.touches[0].clientX;
}
function handleTouchEnd(evt) {
if (!touchStartX) return;
const touchEndX = evt.changedTouches[0].clientX;
const diff = touchEndX - touchStartX;
if (diff > 50) {
player.dx = player.speed;
} else if (diff < -50) {
player.dx = -player.speed;
}
// Reset after movement
setTimeout(() => {
player.dx = 0;
}, 100);
touchStartX = null;
}
// Update Player Position
function updatePlayer() {
if (keys['ArrowRight'] || keys['KeyD']) {
player.dx = player.speed;
} else if (keys['ArrowLeft'] || keys['KeyA']) {
player.dx = -player.speed;
} else {
player.dx = 0;
}
player.x += player.dx;
// Boundary Check
if (player.x < 0) player.x = 0;
if (player.x + player.width > WIDTH) player.x = WIDTH - player.width;
}
// Update Objects
function updateObjects() {
for (let i = 0; i < objects.length; i++) {
const obj = objects[i];
obj.y += obj.speed;
// Collision Detection
if (obj.type === 'egg') {
if (isColliding(obj, player)) {
score++;
catchSound.play();
objects.splice(i, 1);
i--;
continue;
}
} else if (obj.type === 'fox') {
if (isColliding(obj, player)) {
gameOver = true;
gameOverSound.play();
return;
}
}
// Missed Objects
if (obj.y > HEIGHT) {
if (obj.type === 'egg') {
missSound.play();
gameOver = true;
gameOverSound.play();
return;
}
objects.splice(i, 1);
i--;
}
}
}
// Collision Detection Helper
function isColliding(a, b) {
return (
a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y
);
}
// Draw Everything
function draw() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
// Draw Player
ctx.drawImage(chickenImg, player.x, player.y, player.width, player.height);
// Draw Objects
for (const obj of objects) {
if (obj.type === 'egg') {
ctx.drawImage(eggImg, obj.x, obj.y, obj.width, obj.height);
} else if (obj.type === 'fox') {
ctx.drawImage(foxImg, obj.x, obj.y, obj.width, obj.height);
}
}
}
// Game Loop
function gameLoop() {
if (gameOver) {
endGame();
return;
}
updatePlayer();
updateObjects();
// Spawn objects every 60 frames (~1 second at 60fps)
if (Math.random() < 0.02) {
createObject();
}
draw();
scoreBoard.textContent = `Score: ${score} | High Score: ${highScore}`;
requestAnimationFrame(gameLoop);
}
// Start Game
function startGame() {
score = 0;
gameOver = false;
objects.length = 0;
gameOverDiv.style.display = 'none';
gameLoop();
}
// End Game
function endGame() {
if (score > highScore) {
highScore = score;
}
gameOverDiv.style.display = 'block';
}
// Restart Button
restartBtn.addEventListener('click', startGame);
// Start the game when images are loaded
window.onload = () => {
// Ensure images are loaded
let imagesLoaded = 0;
const totalImages = 3;
const checkAllImagesLoaded = () => {
imagesLoaded++;
if (imagesLoaded === totalImages) {
startGame();
}
};
chickenImg.onload = checkAllImagesLoaded;
eggImg.onload = checkAllImagesLoaded;
foxImg.onload = checkAllImagesLoaded;
};
</script>
</body>
</html>
How to Play:
Run the Game: Save the above code in a file named crazy_chicken_chase.html and open it in your web browser.
Controls:
Desktop: Use the Left and Right arrow keys or A and D keys to move the chicken.
Mobile/Touch Devices: Swipe left or right on the screen to move the chicken.
Objective: Catch as many falling eggs as possible while avoiding the foxes. Each egg caught increases your score by one. If a fox collides with the chicken or an egg falls past without being caught, the game ends.
Restarting: After the game is over, click the "Restart" button to play again.
Customization:
Images: The game uses external images for the chicken, egg, and fox. You can replace the image URLs with your own images by uploading them to an image hosting service and updating the src attributes in the JavaScript section.
Sounds: Similarly, you can customize the sound effects by replacing the URLs in the catchSound, missSound, and gameOverSound variables.
Difficulty: Adjust the falling speed or the rate at which objects are created to make the game easier or harder.
Enjoy playing Crazy Chicken Chase! Feel free to enhance and modify the game to add more features and make it even more entertaining.