-
Notifications
You must be signed in to change notification settings - Fork 638
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 Game CatchTheBall #1660
Merged
swapnilsparsh
merged 8 commits into
swapnilsparsh:master
from
shruti110503:CatchTheBall
Jun 21, 2024
Merged
Added Game CatchTheBall #1660
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7d5341
Added Game CatchTheBall
shruti110503 fea918a
updated the index file
shruti110503 9ee969f
Update index.html
shruti110503 09c6b37
Update index.html
shruti110503 511ed5b
Merge branch 'master' into CatchTheBall
shruti110503 c33aa9c
Update index.html
shruti110503 051c354
Update index.html
shruti110503 f9a8f00
Merge branch 'master' into CatchTheBall
shruti110503 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,24 @@ | ||
# **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. |
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,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> |
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,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(); |
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,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; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not be you localhost url, update this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swapnilsparsh can you please help me as to how can i add my own localhost url
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shruti110503 Take reference from the other url which are there in the index.htm and make the required changes