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

Catch the ball #4264

Merged
merged 6 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
34 changes: 34 additions & 0 deletions Games/Catch_The_Ball/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# **CatchTheBall**
<br>

## **Description 📃**
Catch the Ball is an engaging and addictive arcade game that challenges players to catch a bouncing ball with a paddle.

## **features 🎮**
1)Intuitive Gameplay:
Simple controls: Move the paddle left or right to catch the ball.
Easy to understand, making it accessible for players of all skill levels.

2)Responsive Design:
Mobile-friendly: Optimized for touch controls on smartphones and tablets.
Resizable canvas: Automatically adjusts to different screen sizes, providing a consistent gaming experience on any device.

3)Endless Challenge:
Increasing difficulty: The ball's speed increases over time, testing players' reflexes and coordination.
Score tracking: Players can see their score in real-time and aim to beat their high score.

## **How to play? 🕹️**
2. Use the left (`←`) and right (`→`) arrow keys to move the paddle.
3. Try to catch the falling ball with the paddle.
4. If you catch the ball, a new ball will fall from top from a new random position.
5. If you miss the ball, the game will restart.

<br>

## **Screenshots 📸**

<br>
<!-- add your screenshots like this -->
[image](assets\images\catch-the-ball.png)

<br>
22 changes: 22 additions & 0 deletions Games/Catch_The_Ball/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Catch the Ball</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap" rel="stylesheet">
</head>
<body>
<div class="game-container">
<h1 class="game-title">Catch the Ball</h1>
<canvas id="gameCanvas"></canvas>
<div class="controls">
<button id="leftBtn">Left</button>
<button id="rightBtn">Right</button>
<button id="playAgainBtn" style="display: none;">Play Again</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
135 changes: 135 additions & 0 deletions Games/Catch_The_Ball/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth * 0.8;
canvas.height = window.innerHeight * 0.6;

const paddleWidth = 100;
const paddleHeight = 10;
const ballRadius = 10;

let paddleX = (canvas.width - paddleWidth) / 2;
let rightPressed = false;
let leftPressed = false;

let ballX = canvas.width / 2;
let ballY = ballRadius;
let ballDX = 2;
let ballDY = 2;
let score = 0;

document.addEventListener('keydown', keyDownHandler);
document.addEventListener('keyup', keyUpHandler);
document.getElementById('leftBtn').addEventListener('click', () => { leftPressed = true; });
document.getElementById('rightBtn').addEventListener('click', () => { rightPressed = true; });
document.getElementById('playAgainBtn').addEventListener('click', resetGame);

window.addEventListener('resize', () => {
canvas.width = window.innerWidth * 0.8;
canvas.height = window.innerHeight * 0.6;
paddleX = (canvas.width - paddleWidth) / 2;
ballX = canvas.width / 2;
ballY = ballRadius;
});

canvas.addEventListener('touchstart', (e) => {
const touchX = e.touches[0].clientX - canvas.offsetLeft;
if (touchX > paddleX + paddleWidth / 2) {
rightPressed = true;
} else {
leftPressed = true;
}
});

canvas.addEventListener('touchend', (e) => {
rightPressed = false;
leftPressed = false;
});

function keyDownHandler(e) {
if (e.key === 'Right' || e.key === 'ArrowRight') {
rightPressed = true;
} else if (e.key === 'Left' || e.key === 'ArrowLeft') {
leftPressed = true;
}
}

function keyUpHandler(e) {
if (e.key === 'Right' || e.key === 'ArrowRight') {
rightPressed = false;
} else if (e.key === 'Left' || e.key === 'ArrowLeft') {
leftPressed = false;
}
}

function drawPaddle() {
ctx.beginPath();
ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
ctx.fillStyle = '#0095DD';
ctx.fill();
ctx.closePath();
}

function drawBall() {
ctx.beginPath();
ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = '#0095DD';
ctx.fill();
ctx.closePath();
}

function drawScore() {
ctx.font = '16px Arial';
ctx.fillStyle = '#0095DD';
ctx.fillText('Score: ' + score, 8, 20);
}

function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPaddle();
drawBall();
drawScore();

if (rightPressed && paddleX < canvas.width - paddleWidth) {
paddleX += 7;
} else if (leftPressed && paddleX > 0) {
paddleX -= 7;
}

ballX += ballDX;
ballY += ballDY;

if (ballX + ballDX > canvas.width - ballRadius || ballX + ballDX < ballRadius) {
ballDX = -ballDX;
}

if (ballY + ballDY < ballRadius) {
ballDY = -ballDY;
} else if (ballY + ballDY > canvas.height - ballRadius) {
if (ballX > paddleX && ballX < paddleX + paddleWidth) {
ballDY = -ballDY;
score++;
} else {
endGame();
return;
}
}

requestAnimationFrame(draw);
}

function endGame() {
alert('Game Over! Your score is: ' + score);
document.getElementById('playAgainBtn').style.display = 'inline-block';
}

function resetGame() {
score = 0;
ballX = canvas.width / 2;
ballY = ballRadius;
ballDX = 2;
ballDY = 2;
document.getElementById('playAgainBtn').style.display = 'none';
draw();
}

draw();
36 changes: 36 additions & 0 deletions Games/Catch_The_Ball/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: 'Roboto', sans-serif;
}

.game-container {
text-align: center;
}

.game-title {
font-family: 'Roboto', sans-serif;
font-size: 36px;
font-weight: 700;
margin-bottom: 20px;
color: #0095DD;
}

canvas {
background-color: #333;
border: 1px solid #000;
}

.controls {
margin-top: 10px;
}

button {
padding: 10px 20px;
margin: 0 10px;
font-size: 16px;
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ This repository also provides one such platforms where contributers come over an
| [Tic-Tac-Toe Game](https://github.com/kunjgit/GameZone/tree/main/Games/Tic-Tac-Toe) |
| [Rapid_click_frenzy](https://github.com/kunjgit/GameZone/tree/main/Games/Rapid_click_frenzy)

| [Catch_The_Ball](https://github.com/kunjgit/GameZone/tree/main/Games/Catch_The_Ball) |


</center>
Expand Down
Binary file added assets/images/catch-the-ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.