-
Notifications
You must be signed in to change notification settings - Fork 841
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 #4752 from 1911aditi/main
adding a new game dodge teh blocks
- Loading branch information
Showing
6 changed files
with
169 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,43 @@ | ||
# Dodge the Blocks Game | ||
|
||
"Dodge the Blocks" is a simple and fun web-based game where the player controls a character that must avoid falling blocks. The goal is to survive as long as possible without being hit by the blocks. | ||
|
||
## Table of Contents | ||
|
||
- [Demo](#demo) | ||
- [Installation](#installation) | ||
- [How to Play](#how-to-play) | ||
- [Technologies Used](#technologies-used) | ||
- [Contributing](#contributing) | ||
- [License](#license) | ||
|
||
## Demo | ||
|
||
You can see a live demo of the game [here](#). (Add your live demo link here) | ||
|
||
## Installation | ||
|
||
To run this game locally, follow these steps: | ||
|
||
|
||
## How to Play | ||
|
||
1. Open the game in your web browser. | ||
2. Use the left and right arrow keys to move the player left and right. | ||
3. Avoid the falling blocks. | ||
4. Try to survive as long as possible. Your score increases with time. | ||
5. If a block hits the player, the game is over, and your final score will be displayed. | ||
|
||
## Technologies Used | ||
|
||
- HTML | ||
- CSS | ||
- JavaScript | ||
|
||
## Contributing | ||
|
||
Contributions are welcome! If you have any suggestions or improvements, feel free to submit a pull request or open an issue. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details. |
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,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Dodge the Blocks</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
<div id="gameContainer"> | ||
<div id="player"></div> | ||
</div> | ||
<div id="score">Score: 0</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 @@ | ||
const gameContainer = document.getElementById('gameContainer'); | ||
const player = document.getElementById('player'); | ||
const scoreDisplay = document.getElementById('score'); | ||
|
||
let score = 0; | ||
let gameOver = false; | ||
|
||
// Player movement | ||
document.addEventListener('keydown', (e) => { | ||
const left = parseInt(window.getComputedStyle(player).getPropertyValue('left')); | ||
if (e.key === 'ArrowLeft' && left > 0) { | ||
player.style.left = left - 10 + 'px'; | ||
} else if (e.key === 'ArrowRight' && left < 270) { | ||
player.style.left = left + 10 + 'px'; | ||
} | ||
}); | ||
|
||
// Generate blocks | ||
function createBlock() { | ||
const block = document.createElement('div'); | ||
block.classList.add('block'); | ||
block.style.left = Math.floor(Math.random() * 270) + 'px'; | ||
gameContainer.appendChild(block); | ||
|
||
// Collision detection | ||
const blockInterval = setInterval(() => { | ||
const blockTop = parseInt(window.getComputedStyle(block).getPropertyValue('top')); | ||
const playerLeft = parseInt(window.getComputedStyle(player).getPropertyValue('left')); | ||
const playerRight = playerLeft + 30; | ||
const blockLeft = parseInt(block.style.left); | ||
const blockRight = blockLeft + 30; | ||
|
||
if (blockTop > 470 && blockTop < 500 && playerLeft < blockRight && playerRight > blockLeft) { | ||
gameOver = true; | ||
alert('Game Over! Final Score: ' + score); | ||
clearInterval(blockInterval); | ||
location.reload(); | ||
} | ||
}, 10); | ||
|
||
// Remove block after it falls | ||
setTimeout(() => { | ||
block.remove(); | ||
}, 3000); | ||
} | ||
|
||
// Update score | ||
function updateScore() { | ||
if (!gameOver) { | ||
score++; | ||
scoreDisplay.textContent = 'Score: ' + score; | ||
setTimeout(updateScore, 1000); | ||
} | ||
} | ||
|
||
updateScore(); | ||
setInterval(createBlock, 1500); |
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,49 @@ | ||
body { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
margin: 0; | ||
background-color: #f0f0f0; | ||
font-family: Arial, sans-serif; | ||
} | ||
|
||
#gameContainer { | ||
position: relative; | ||
width: 300px; | ||
height: 500px; | ||
background-color: #fff; | ||
border: 2px solid #000; | ||
overflow: hidden; | ||
} | ||
|
||
#player { | ||
position: absolute; | ||
width: 30px; | ||
height: 30px; | ||
background-color: #3498db; | ||
bottom: 10px; | ||
left: 135px; | ||
} | ||
|
||
.block { | ||
position: absolute; | ||
width: 30px; | ||
height: 30px; | ||
background-color: #e74c3c; | ||
animation: fall 3s linear infinite; | ||
} | ||
|
||
@keyframes fall { | ||
0% { | ||
top: -30px; | ||
} | ||
100% { | ||
top: 500px; | ||
} | ||
} | ||
|
||
#score { | ||
margin-top: 20px; | ||
font-size: 24px; | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.