Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Catch the object game #5035

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Games/Catch_The_Objects/README.md
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>
17 changes: 17 additions & 0 deletions Games/Catch_The_Objects/index.html
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>
57 changes: 57 additions & 0 deletions Games/Catch_The_Objects/script.js
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();
});
41 changes: 41 additions & 0 deletions Games/Catch_The_Objects/styles.css
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;
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ This repository also provides one such platforms where contributers come over an
|[Idle_miner](https://github.com/kunjgit/GameZone/tree/main/Games/Idle_miner)|
|[Five_Nights_at_Freddys](https://github.com/kunjgit/GameZone/tree/main/Games/Five_Nights_at_Freddys)|
|[Snake_Gun_Water](https://github.com/kunjgit/GameZone/tree/main/Games/Snake_Gun_Water)|
|[Catch_The_Object](https://github.com/kunjgit/GameZone/tree/main/Games/Catch_The_Objects)|
</center>

| Game | Game | Game | Game | Game |
Expand Down
Loading