Skip to content

Commit e043a8c

Browse files
authored
Merge pull request #200 from Apoorva57/Apoorva57
Added a Catch the Ball game
2 parents 551c4df + a25cbb2 commit e043a8c

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

web/pages/catchgame.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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>Catch the Falling Object</title>
7+
<link rel="stylesheet" href="catchgame.css">
8+
</head>
9+
<body>
10+
<div id="gameContainer">
11+
<div id="basket"></div>
12+
<div id="object"></div>
13+
</div>
14+
<h1 id="score">Score: 0</h1>
15+
<script src="catchgame.js"></script>
16+
</body>
17+
</html>

web/scripts/catchgame.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
let basket = document.getElementById('basket');
2+
let object = document.getElementById('object');
3+
let scoreDisplay = document.getElementById('score');
4+
let gameContainer = document.getElementById('gameContainer');
5+
let score = 0;
6+
let objectFallingSpeed = 2;
7+
let gameWidth = gameContainer.offsetWidth;
8+
let basketWidth = basket.offsetWidth;
9+
10+
window.addEventListener('keydown', function(event) {
11+
let basketLeft = basket.offsetLeft;
12+
if (event.key === 'ArrowLeft' && basketLeft > 0) {
13+
basket.style.left = (basketLeft - 20) + 'px';
14+
} else if (event.key === 'ArrowRight' && basketLeft < (gameWidth - basketWidth)) {
15+
basket.style.left = (basketLeft + 20) + 'px';
16+
}
17+
});
18+
19+
function dropObject() {
20+
let objectLeft = Math.floor(Math.random() * (gameWidth - object.offsetWidth));
21+
object.style.left = objectLeft + 'px';
22+
object.style.top = '-30px';
23+
let fallingInterval = setInterval(function() {
24+
let objectTop = object.offsetTop;
25+
object.style.top = objectTop + objectFallingSpeed + 'px';
26+
let basketLeft = basket.offsetLeft;
27+
let basketTop = basket.offsetTop;
28+
if (objectTop + object.offsetHeight >= basketTop &&
29+
objectTop + object.offsetHeight <= basketTop + basket.offsetHeight &&
30+
objectLeft + object.offsetWidth >= basketLeft &&
31+
objectLeft <= basketLeft + basketWidth) {
32+
clearInterval(fallingInterval);
33+
score += 1;
34+
scoreDisplay.innerText = 'Score: ' + score;
35+
if (score % 10 === 0) {
36+
changeColors();
37+
}
38+
if (score % 10 === 0) {
39+
increaseSpeed();
40+
}
41+
dropObject();
42+
}
43+
if (objectTop > gameContainer.offsetHeight) {
44+
clearInterval(fallingInterval);
45+
alert('Game Over! Final Score: ' + score);
46+
resetGame();
47+
}
48+
}, 20);
49+
}
50+
51+
function getRandomColor() {
52+
const letters = '0123456789ABCDEF';
53+
let color = '#';
54+
for (let i = 0; i < 6; i++) {
55+
color += letters[Math.floor(Math.random() * 16)];
56+
}
57+
return color;
58+
}
59+
60+
function changeColors() {
61+
const randomBasketColor = getRandomColor();
62+
const randomObjectColor = getRandomColor();
63+
const randomBorderColor = getRandomColor();
64+
basket.style.backgroundColor = randomBasketColor;
65+
object.style.backgroundColor = randomObjectColor;
66+
gameContainer.style.borderColor = randomBorderColor;
67+
}
68+
69+
function increaseSpeed() {
70+
objectFallingSpeed += 0.5;
71+
}
72+
73+
function resetGame() {
74+
score = 0;
75+
objectFallingSpeed = 3;
76+
scoreDisplay.innerText = 'Score: ' + score;
77+
dropObject();
78+
}
79+
80+
dropObject();

web/stylesheet/catchgame.css

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
box-sizing: border-box;
5+
}
6+
7+
body {
8+
font-family: Arial, sans-serif;
9+
background-color: #081108;
10+
color: #fff;
11+
text-align: center;
12+
}
13+
14+
#gameContainer {
15+
position: relative;
16+
width: 100%;
17+
height: 500px;
18+
background-color: #222;
19+
border: 2px solid #6BFF53;
20+
overflow: hidden;
21+
margin: 0 auto;
22+
}
23+
24+
#basket {
25+
position: absolute;
26+
bottom: 10px;
27+
width: 100px;
28+
height: 50px;
29+
background-color: #6BFF53;
30+
border-radius: 10px;
31+
left: 50%;
32+
transform: translateX(-50%);
33+
}
34+
35+
#object {
36+
position: absolute;
37+
width: 30px;
38+
height: 30px;
39+
background-color: red;
40+
border-radius: 50%;
41+
top: -30px;
42+
}
43+
44+
#score {
45+
margin-top: 20px;
46+
font-size: 24px;
47+
}

0 commit comments

Comments
 (0)