-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 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,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Simple Racing Game</title> | ||
<link rel="stylesheet" href="styles.css"> | ||
</head> | ||
<body> | ||
<canvas id="gameCanvas"></canvas> | ||
<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,95 @@ | ||
const canvas = document.getElementById('gameCanvas'); | ||
const ctx = canvas.getContext('2d'); | ||
canvas.width = 400; | ||
canvas.height = 600; | ||
|
||
let player = { | ||
x: canvas.width / 2 - 20, | ||
y: canvas.height - 100, | ||
width: 40, | ||
height: 80, | ||
color: 'blue', | ||
speed: 5, | ||
}; | ||
|
||
let obstacles = []; | ||
let score = 0; | ||
let gameInterval; | ||
|
||
// Player movement | ||
document.addEventListener('keydown', (event) => { | ||
if (event.key === 'ArrowLeft' && player.x > 0) { | ||
player.x -= player.speed; | ||
} else if (event.key === 'ArrowRight' && player.x < canvas.width - player.width) { | ||
player.x += player.speed; | ||
} | ||
}); | ||
|
||
// Function to create obstacles | ||
function createObstacle() { | ||
const obstacle = { | ||
x: Math.random() * (canvas.width - 20), | ||
y: -40, | ||
width: 20, | ||
height: 20, | ||
speed: 1 + Math.random() * 0.1, | ||
color: 'red', | ||
}; | ||
obstacles.push(obstacle); | ||
} | ||
|
||
// Function to update the game | ||
function update() { | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
|
||
// Draw player | ||
ctx.fillStyle = player.color; | ||
ctx.fillRect(player.x, player.y, player.width, player.height); | ||
|
||
// Draw obstacles | ||
obstacles.forEach((obstacle, index) => { | ||
obstacle.y += obstacle.speed; | ||
ctx.fillStyle = obstacle.color; | ||
ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height); | ||
|
||
// Check for collision | ||
if (isColliding(player, obstacle)) { | ||
alert("Game Over! Final Score: " + score); | ||
clearInterval(gameInterval); | ||
return; | ||
} | ||
|
||
// Remove off-screen obstacles | ||
if (obstacle.y > canvas.height) { | ||
obstacles.splice(index, 1); | ||
score++; | ||
document.getElementById('score').innerText = `Score: ${score}`; | ||
} | ||
}); | ||
} | ||
|
||
// Function to check for collision | ||
function isColliding(rect1, rect2) { | ||
return rect1.x < rect2.x + rect2.width && | ||
rect1.x + rect1.width > rect2.x && | ||
rect1.y < rect2.y + rect2.height && | ||
rect1.y + rect1.height > rect2.y; | ||
} | ||
|
||
// Start the game | ||
function startGame() { | ||
score = 0; | ||
obstacles = []; | ||
document.getElementById('score').innerText = `Score: ${score}`; | ||
|
||
// Increase the interval between obstacle spawns | ||
gameInterval = setInterval(() => { | ||
if (Math.random() < 0.5) { // Adjust probability to reduce frequency | ||
createObstacle(); | ||
} | ||
update(); | ||
}, 1000 / 30); // 30 FPS | ||
} | ||
|
||
// Begin the game when the page loads | ||
startGame(); |
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,20 @@ | ||
body { | ||
margin: 0; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
height: 100vh; | ||
background-color: #333; | ||
} | ||
|
||
canvas { | ||
border: 1px solid white; | ||
background-color: #aaa; | ||
} | ||
|
||
#score { | ||
margin-top: 10px; | ||
color: white; | ||
font-size: 24px; | ||
} |