Create a game of chicken crossing road
Browse files- chicken-game.html +87 -0
- chicken-game.js +151 -0
- script.js +4 -3
chicken-game.html
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Chicken Crossing Road</title>
|
| 7 |
+
<link rel="stylesheet" href="style.css">
|
| 8 |
+
<script src="components/navbar.js"></script>
|
| 9 |
+
<style>
|
| 10 |
+
#game-container {
|
| 11 |
+
width: 800px;
|
| 12 |
+
height: 600px;
|
| 13 |
+
margin: 20px auto;
|
| 14 |
+
position: relative;
|
| 15 |
+
overflow: hidden;
|
| 16 |
+
background: #87CEEB;
|
| 17 |
+
border: 4px solid #333;
|
| 18 |
+
}
|
| 19 |
+
#road {
|
| 20 |
+
position: absolute;
|
| 21 |
+
bottom: 100px;
|
| 22 |
+
width: 100%;
|
| 23 |
+
height: 200px;
|
| 24 |
+
background: #555;
|
| 25 |
+
}
|
| 26 |
+
#chicken {
|
| 27 |
+
position: absolute;
|
| 28 |
+
width: 50px;
|
| 29 |
+
height: 50px;
|
| 30 |
+
bottom: 150px;
|
| 31 |
+
left: 50px;
|
| 32 |
+
background-image: url('http://static.photos/animals/50x50/1');
|
| 33 |
+
background-size: contain;
|
| 34 |
+
z-index: 10;
|
| 35 |
+
}
|
| 36 |
+
.car {
|
| 37 |
+
position: absolute;
|
| 38 |
+
width: 80px;
|
| 39 |
+
height: 40px;
|
| 40 |
+
background-size: contain;
|
| 41 |
+
background-repeat: no-repeat;
|
| 42 |
+
}
|
| 43 |
+
#score {
|
| 44 |
+
text-align: center;
|
| 45 |
+
font-size: 24px;
|
| 46 |
+
margin-top: 10px;
|
| 47 |
+
color: #333;
|
| 48 |
+
}
|
| 49 |
+
#start-screen {
|
| 50 |
+
position: absolute;
|
| 51 |
+
top: 0;
|
| 52 |
+
left: 0;
|
| 53 |
+
width: 100%;
|
| 54 |
+
height: 100%;
|
| 55 |
+
background: rgba(0,0,0,0.7);
|
| 56 |
+
display: flex;
|
| 57 |
+
flex-direction: column;
|
| 58 |
+
justify-content: center;
|
| 59 |
+
align-items: center;
|
| 60 |
+
z-index: 100;
|
| 61 |
+
}
|
| 62 |
+
#start-button {
|
| 63 |
+
padding: 15px 30px;
|
| 64 |
+
font-size: 24px;
|
| 65 |
+
background: #4CAF50;
|
| 66 |
+
color: white;
|
| 67 |
+
border: none;
|
| 68 |
+
border-radius: 5px;
|
| 69 |
+
cursor: pointer;
|
| 70 |
+
}
|
| 71 |
+
</style>
|
| 72 |
+
</head>
|
| 73 |
+
<body>
|
| 74 |
+
<custom-navbar></custom-navbar>
|
| 75 |
+
<div id="game-container">
|
| 76 |
+
<div id="road"></div>
|
| 77 |
+
<div id="chicken"></div>
|
| 78 |
+
<div id="score">Score: 0</div>
|
| 79 |
+
<div id="start-screen">
|
| 80 |
+
<h1 style="color: white; margin-bottom: 20px;">Chicken Crossing Road</h1>
|
| 81 |
+
<button id="start-button">Start Game</button>
|
| 82 |
+
</div>
|
| 83 |
+
</div>
|
| 84 |
+
<script src="chicken-game.js"></script>
|
| 85 |
+
<script src="script.js"></script>
|
| 86 |
+
</body>
|
| 87 |
+
</html>
|
chicken-game.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 2 |
+
const gameContainer = document.getElementById('game-container');
|
| 3 |
+
const chicken = document.getElementById('chicken');
|
| 4 |
+
const road = document.getElementById('road');
|
| 5 |
+
const scoreDisplay = document.getElementById('score');
|
| 6 |
+
const startScreen = document.getElementById('start-screen');
|
| 7 |
+
const startButton = document.getElementById('start-button');
|
| 8 |
+
|
| 9 |
+
let score = 0;
|
| 10 |
+
let gameSpeed = 2;
|
| 11 |
+
let gameRunning = false;
|
| 12 |
+
let cars = [];
|
| 13 |
+
let gameInterval;
|
| 14 |
+
let carInterval;
|
| 15 |
+
|
| 16 |
+
// Chicken movement
|
| 17 |
+
document.addEventListener('keydown', (e) => {
|
| 18 |
+
if (!gameRunning) return;
|
| 19 |
+
|
| 20 |
+
const chickenRect = chicken.getBoundingClientRect();
|
| 21 |
+
const containerRect = gameContainer.getBoundingClientRect();
|
| 22 |
+
|
| 23 |
+
switch(e.key) {
|
| 24 |
+
case 'ArrowUp':
|
| 25 |
+
if (chickenRect.top > containerRect.top + 10) {
|
| 26 |
+
chicken.style.bottom = (parseInt(chicken.style.bottom) + 10) + 'px';
|
| 27 |
+
}
|
| 28 |
+
break;
|
| 29 |
+
case 'ArrowDown':
|
| 30 |
+
if (chickenRect.bottom < road.getBoundingClientRect().top - 10) {
|
| 31 |
+
chicken.style.bottom = (parseInt(chicken.style.bottom) - 10) + 'px';
|
| 32 |
+
}
|
| 33 |
+
break;
|
| 34 |
+
case 'ArrowLeft':
|
| 35 |
+
if (chickenRect.left > containerRect.left + 10) {
|
| 36 |
+
chicken.style.left = (parseInt(chicken.style.left) - 10) + 'px';
|
| 37 |
+
}
|
| 38 |
+
break;
|
| 39 |
+
case 'ArrowRight':
|
| 40 |
+
if (chickenRect.right < containerRect.right - 10) {
|
| 41 |
+
chicken.style.left = (parseInt(chicken.style.left) + 10) + 'px';
|
| 42 |
+
}
|
| 43 |
+
break;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
checkCollision();
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
// Create cars
|
| 50 |
+
function createCar() {
|
| 51 |
+
if (!gameRunning) return;
|
| 52 |
+
|
| 53 |
+
const car = document.createElement('div');
|
| 54 |
+
car.className = 'car';
|
| 55 |
+
const carTypes = ['red', 'blue', 'yellow', 'green'];
|
| 56 |
+
const randomType = carTypes[Math.floor(Math.random() * carTypes.length)];
|
| 57 |
+
car.style.backgroundImage = `url('http://static.photos/automotive/80x40/${Math.floor(Math.random() * 10) + 1}')`;
|
| 58 |
+
|
| 59 |
+
const direction = Math.random() > 0.5 ? 'left' : 'right';
|
| 60 |
+
const startX = direction === 'right' ? -80 : gameContainer.offsetWidth;
|
| 61 |
+
|
| 62 |
+
car.style.left = startX + 'px';
|
| 63 |
+
car.style.top = (road.offsetTop + 20) + 'px';
|
| 64 |
+
|
| 65 |
+
if (direction === 'right') {
|
| 66 |
+
car.style.transform = 'scaleX(-1)';
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
gameContainer.appendChild(car);
|
| 70 |
+
cars.push({ element: car, direction: direction, speed: Math.random() * 2 + gameSpeed });
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
// Move cars
|
| 74 |
+
function moveCars() {
|
| 75 |
+
cars.forEach((car, index) => {
|
| 76 |
+
const currentLeft = parseInt(car.element.style.left);
|
| 77 |
+
|
| 78 |
+
if (car.direction === 'right') {
|
| 79 |
+
car.element.style.left = (currentLeft + car.speed) + 'px';
|
| 80 |
+
if (currentLeft > gameContainer.offsetWidth) {
|
| 81 |
+
gameContainer.removeChild(car.element);
|
| 82 |
+
cars.splice(index, 1);
|
| 83 |
+
}
|
| 84 |
+
} else {
|
| 85 |
+
car.element.style.left = (currentLeft - car.speed) + 'px';
|
| 86 |
+
if (currentLeft < -80) {
|
| 87 |
+
gameContainer.removeChild(car.element);
|
| 88 |
+
cars.splice(index, 1);
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
});
|
| 92 |
+
|
| 93 |
+
checkCollision();
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// Check collision
|
| 97 |
+
function checkCollision() {
|
| 98 |
+
const chickenRect = chicken.getBoundingClientRect();
|
| 99 |
+
|
| 100 |
+
cars.forEach(car => {
|
| 101 |
+
const carRect = car.element.getBoundingClientRect();
|
| 102 |
+
|
| 103 |
+
if (
|
| 104 |
+
chickenRect.left < carRect.right &&
|
| 105 |
+
chickenRect.right > carRect.left &&
|
| 106 |
+
chickenRect.top < carRect.bottom &&
|
| 107 |
+
chickenRect.bottom > carRect.top
|
| 108 |
+
) {
|
| 109 |
+
gameOver();
|
| 110 |
+
}
|
| 111 |
+
});
|
| 112 |
+
|
| 113 |
+
// Check if chicken reached top
|
| 114 |
+
if (chickenRect.top <= gameContainer.getBoundingClientRect().top + 10) {
|
| 115 |
+
score++;
|
| 116 |
+
scoreDisplay.textContent = `Score: ${score}`;
|
| 117 |
+
gameSpeed += 0.2;
|
| 118 |
+
chicken.style.bottom = '150px';
|
| 119 |
+
chicken.style.left = '50px';
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// Game over
|
| 124 |
+
function gameOver() {
|
| 125 |
+
gameRunning = false;
|
| 126 |
+
clearInterval(gameInterval);
|
| 127 |
+
clearInterval(carInterval);
|
| 128 |
+
|
| 129 |
+
cars.forEach(car => {
|
| 130 |
+
gameContainer.removeChild(car.element);
|
| 131 |
+
});
|
| 132 |
+
cars = [];
|
| 133 |
+
|
| 134 |
+
alert(`Game Over! Your score: ${score}`);
|
| 135 |
+
startScreen.style.display = 'flex';
|
| 136 |
+
score = 0;
|
| 137 |
+
gameSpeed = 2;
|
| 138 |
+
scoreDisplay.textContent = `Score: ${score}`;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
// Start game
|
| 142 |
+
startButton.addEventListener('click', () => {
|
| 143 |
+
startScreen.style.display = 'none';
|
| 144 |
+
gameRunning = true;
|
| 145 |
+
chicken.style.bottom = '150px';
|
| 146 |
+
chicken.style.left = '50px';
|
| 147 |
+
|
| 148 |
+
gameInterval = setInterval(moveCars, 20);
|
| 149 |
+
carInterval = setInterval(createCar, 2000);
|
| 150 |
+
});
|
| 151 |
+
});
|
script.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
| 2 |
// Main app functionality
|
| 3 |
document.addEventListener('DOMContentLoaded', () => {
|
| 4 |
// Initialize any global functionality here
|
| 5 |
-
console.log('
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
if (e.target.closest('.video-card')) {
|
| 9 |
const card = e.target.closest('.video-card');
|
| 10 |
const title = card.getAttribute('data-title');
|
|
|
|
| 2 |
// Main app functionality
|
| 3 |
document.addEventListener('DOMContentLoaded', () => {
|
| 4 |
// Initialize any global functionality here
|
| 5 |
+
console.log('Rizz Reels Royale loaded!');
|
| 6 |
+
|
| 7 |
+
// Example: Video card click handler
|
| 8 |
+
document.addEventListener('click', (e) => {
|
| 9 |
if (e.target.closest('.video-card')) {
|
| 10 |
const card = e.target.closest('.video-card');
|
| 11 |
const title = card.getAttribute('data-title');
|