-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5035 from Ayushmaanagarwal1211/new-game
Added Catch the object game
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Catch the Falling Objects | ||
|
||
## Overview | ||
|
||
"Catch the Falling Objects" is a simple HTML, CSS, and JavaScript game where players control a basket to catch falling objects. The objective is to catch as many falling objects as possible to increase your score. | ||
|
||
### Features | ||
|
||
- **Responsive Design**: The game adjusts to various screen sizes. | ||
- **Real-Time Scoring**: The score updates in real-time as you catch objects. | ||
- **Keyboard Controls**: Use the left and right arrow keys to move the basket. | ||
|
||
## Installation | ||
|
||
To set up and run the game locally, follow these steps: | ||
|
||
### Prerequisites | ||
|
||
Ensure you have a modern web browser (Chrome, Firefox, Safari, etc.) for testing the game. | ||
|
||
### Setup | ||
|
||
1. **Clone or Download the Repository** | ||
|
||
```bash | ||
git clone <repository-url> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Catch the Falling Objects</title> | ||
<link rel="stylesheet" href="styles.css"> | ||
</head> | ||
<body> | ||
<div class="game-container"> | ||
<div id="basket"></div> | ||
<div id="object"></div> | ||
<div id="score">Score: 0</div> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
document.addEventListener("DOMContentLoaded", () => { | ||
const basket = document.getElementById("basket"); | ||
const object = document.getElementById("object"); | ||
const scoreDisplay = document.getElementById("score"); | ||
|
||
let score = 0; | ||
let basketX = window.innerWidth / 2 - basket.offsetWidth / 2; | ||
let objectX = Math.random() * (window.innerWidth - object.offsetWidth); | ||
let objectY = -object.offsetHeight; | ||
|
||
const moveBasket = (e) => { | ||
if (e.key === "ArrowLeft") { | ||
basketX -= 20; | ||
} else if (e.key === "ArrowRight") { | ||
basketX += 20; | ||
} | ||
basketX = Math.max(0, Math.min(window.innerWidth - basket.offsetWidth, basketX)); | ||
basket.style.left = `${basketX}px`; | ||
}; | ||
|
||
const updateObject = () => { | ||
objectY += 5; | ||
if (objectY > window.innerHeight) { | ||
objectY = -object.offsetHeight; | ||
objectX = Math.random() * (window.innerWidth - object.offsetWidth); | ||
} | ||
object.style.top = `${objectY}px`; | ||
object.style.left = `${objectX}px`; | ||
}; | ||
|
||
const checkCollision = () => { | ||
const basketRect = basket.getBoundingClientRect(); | ||
const objectRect = object.getBoundingClientRect(); | ||
|
||
if ( | ||
objectRect.left < basketRect.right && | ||
objectRect.right > basketRect.left && | ||
objectRect.top < basketRect.bottom && | ||
objectRect.bottom > basketRect.top | ||
) { | ||
score++; | ||
scoreDisplay.textContent = `Score: ${score}`; | ||
objectY = -object.offsetHeight; | ||
objectX = Math.random() * (window.innerWidth - object.offsetWidth); | ||
} | ||
}; | ||
|
||
document.addEventListener("keydown", moveBasket); | ||
|
||
const gameLoop = () => { | ||
updateObject(); | ||
checkCollision(); | ||
requestAnimationFrame(gameLoop); | ||
}; | ||
|
||
gameLoop(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
body { | ||
margin: 0; | ||
font-family: Arial, sans-serif; | ||
background-color: #f0f0f0; | ||
} | ||
|
||
.game-container { | ||
position: relative; | ||
width: 100vw; | ||
height: 100vh; | ||
overflow: hidden; | ||
background-color: #87ceeb; | ||
} | ||
|
||
#basket { | ||
position: absolute; | ||
bottom: 0; | ||
left: 50%; | ||
width: 100px; | ||
height: 50px; | ||
background-color: #ff4500; | ||
border-radius: 10px; | ||
transform: translateX(-50%); | ||
} | ||
|
||
#object { | ||
position: absolute; | ||
top: 0; | ||
width: 30px; | ||
height: 30px; | ||
background-color: #ff6347; | ||
border-radius: 50%; | ||
} | ||
|
||
#score { | ||
position: absolute; | ||
top: 10px; | ||
left: 10px; | ||
font-size: 24px; | ||
color: #ffffff; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters