Skip to content

Commit

Permalink
Added Car Game
Browse files Browse the repository at this point in the history
  • Loading branch information
VanshGarg06 committed Nov 10, 2024
1 parent c1916f8 commit 3c8fa88
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 29 deletions.
161 changes: 161 additions & 0 deletions Car game1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Car Game</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
height: 100vh;
background: #333;
overflow: hidden;
}
#gameArea {
position: relative;
width: 400px;
height: 600px;
background: url('https://i.imgur.com/4d4SHVi.jpg') center/cover;
overflow: hidden;
border: 2px solid #fff;
}
.car {
position: absolute;
width: 50px;
height: 100px;
bottom: 20px;
left: 175px;
background: url('playerCar.png') center/cover; /* Player's car image */
}
.opponent {
position: absolute;
width: 50px;
height: 100px;
background: url('opponentCar.png') center/cover; /* Opponent car image */
}
#scoreboard {
color: #fff;
font-size: 1.5em;
position: absolute;
top: 10px;
left: 10px;
}
#gameOver {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: red;
font-size: 2em;
text-align: center;
background-color: #333;
padding: 20px;
border: 2px solid #fff;
}
</style>
</head>
<body>
<div id="gameArea">
<div id="scoreboard">Score: 0</div>
<div id="gameOver">Game Over<br><span id="finalScore"></span></div>
<div class="car" id="playerCar"></div>
</div>

<script>
const gameArea = document.getElementById("gameArea");
const playerCar = document.getElementById("playerCar");
const scoreboard = document.getElementById("scoreboard");
const gameOverDisplay = document.getElementById("gameOver");
const finalScore = document.getElementById("finalScore");

let score = 0;
let gameOver = false;
let playerPosition = { x: 175, y: 500 }; // Starting position

function updateScore() {
score++;
scoreboard.textContent = "Score: " + score;
}

function createOpponent() {
const opponent = document.createElement("div");
opponent.classList.add("opponent");
opponent.style.left = Math.floor(Math.random() * 350) + "px";
opponent.style.top = "-150px"; // Starts off-screen
gameArea.appendChild(opponent);
moveOpponent(opponent);
}

function moveOpponent(opponent) {
let opponentInterval = setInterval(() => {
if (gameOver) {
clearInterval(opponentInterval);
return;
}
const opponentRect = opponent.getBoundingClientRect();
const playerRect = playerCar.getBoundingClientRect();

if (
opponentRect.left < playerRect.right &&
opponentRect.right > playerRect.left &&
opponentRect.top < playerRect.bottom &&
opponentRect.bottom > playerRect.top
) {
endGame();
clearInterval(opponentInterval);
}

if (parseInt(opponent.style.top) > 600) {
opponent.remove();
updateScore();
clearInterval(opponentInterval);
} else {
opponent.style.top = parseInt(opponent.style.top) + 5 + "px";
}
}, 30);
}

function endGame() {
gameOver = true;
gameOverDisplay.style.display = "block";
finalScore.textContent = "Final Score: " + score;
}

function movePlayer(e) {
if (gameOver) return;

if (e.key === "ArrowLeft" && playerPosition.x > 0) {
playerPosition.x -= 15;
} else if (e.key === "ArrowRight" && playerPosition.x < 350) {
playerPosition.x += 15;
} else if (e.key === "ArrowUp" && playerPosition.y > 0) {
playerPosition.y -= 15;
} else if (e.key === "ArrowDown" && playerPosition.y < 500) {
playerPosition.y += 15;
}
playerCar.style.left = playerPosition.x + "px";
playerCar.style.top = playerPosition.y + "px";
}

document.addEventListener("keydown", movePlayer);

function gameLoop() {
if (!gameOver) {
createOpponent();
setTimeout(gameLoop, 1000);
}
}

gameLoop();
</script>
</body>
</html>
Binary file added Car game1/opponentCar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Car game1/playerCar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/car game.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 12 additions & 29 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,18 @@ <h3 class="card-heading">Flames Game Calculator</h3>
</div>
</a>

<a href="./Car game1/index.html" class="card Mathematical " target="_blank">
<div class="card-cover counter-cover-colour">
<img src="./assets/image/car game.png" alt="Car game">
</div>
<div class="card-content">
<h3 class="card-heading">Car Racing Game</h3>
<p class="card-description">
Maximize speed score more and careful for oppenents coming towards you
</p>
</div>
</a>



<a href="./projects/Magic Square/index.html" class="card Mathematical " target="_blank">
Expand Down Expand Up @@ -2518,20 +2530,6 @@ <h3 class="card-heading">Dictionary</h3>
</a>


<a href="./projects/FlashCard Quiz App/index.html" class="card Games " target="_blank">
<div class="card-cover">
<img src="./projects/Flashcard Quiz App/image/image.png" alt="Flashcard Quiz App logo">
</div>
<div class="card-content">
<h3 class="card-heading">Flashcard Quiz App</h3>
<p class="card-description">
Create and review flashcards for effective learning.
</p>
</div>
</a>



<a href="./projects/weather/index.html" class="card Miscellaneous " target="_blank">
<div class="card-cover counter-cover-colour">
<img src="./assets/image/weather.jpg" class="weather-img" alt="">
Expand Down Expand Up @@ -3237,21 +3235,6 @@ <h3 class="card-heading">Othello Game</h3>
</p>
</div>
</a>
<!-- =========================== Card Validator ================================= -->
<a href="projects/Card-Validator/index.html" class="card Security " target="_blank">

<div class="card-cover counter-cover-colour">
<img src="./assets/image/credit_card_validator.jpg" alt="Credit Card Validator"
style="width: 10rem; height: auto;">
</div>
<div class="card-content">
<h3 class="card-heading">Credit Card Validator</h3>
<p class="card-description">
Validate your credit card number with ease.
</p>
</div>
</a>


<!-- ============================ Sudoku Solver ================================== -->
<a href="projects/Sudoku_Solver/index.html" class="card Games " target="_blank">
Expand Down

0 comments on commit 3c8fa88

Please sign in to comment.