-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar-game.html
128 lines (109 loc) · 3.7 KB
/
car-game.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Driving Game</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="600" height="400"></canvas>
<script>
// Get the canvas element and its context
var canvas = document.getElementById("gameCanvas");
var ctx = canvas.getContext("2d");
// Car properties
var carWidth = 50;
var carHeight = 80;
var carX = canvas.width / 2 - carWidth / 2;
var carY = canvas.height - carHeight - 10;
var carSpeed = 5;
// Obstacle properties
var obstacleWidth = 50;
var obstacleHeight = 50;
var obstacleSpeed = 3;
var obstacles = [];
// Game loop
function gameLoop() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the car
drawCar();
// Move and draw obstacles
moveObstacles();
drawObstacles();
// Check for collisions
if (checkCollision()) {
alert("Game Over!");
document.location.reload();
}
// Request the next animation frame
requestAnimationFrame(gameLoop);
}
// Draw the car
function drawCar() {
ctx.fillStyle = "blue";
ctx.fillRect(carX, carY, carWidth, carHeight);
}
// Move and draw obstacles
function moveObstacles() {
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].y += obstacleSpeed;
if (obstacles[i].y > canvas.height) {
// Remove obstacles that have gone off-screen
obstacles.splice(i, 1);
i--;
}
}
// Randomly generate new obstacles
if (Math.random() < 0.02) {
var obstacleX = Math.random() * (canvas.width - obstacleWidth);
obstacles.push({ x: obstacleX, y: 0, width: obstacleWidth, height: obstacleHeight });
}
}
function drawObstacles() {
ctx.fillStyle = "red";
for (var i = 0; i < obstacles.length; i++) {
ctx.fillRect(obstacles[i].x, obstacles[i].y, obstacles[i].width, obstacles[i].height);
}
}
// Check for collisions between the car and obstacles
function checkCollision() {
for (var i = 0; i < obstacles.length; i++) {
if (
carX < obstacles[i].x + obstacleWidth &&
carX + carWidth > obstacles[i].x &&
carY < obstacles[i].y + obstacleHeight &&
carY + carHeight > obstacles[i].y
) {
return true; // Collision detected
}
}
return false; // No collision
}
// Handle keyboard input
window.addEventListener("keydown", function (event) {
switch (event.keyCode) {
case 37: // Left arrow key
carX -= carSpeed;
if (carX < 0) carX = 0;
break;
case 39: // Right arrow key
carX += carSpeed;
if (carX + carWidth > canvas.width) carX = canvas.width - carWidth;
break;
}
});
// Start the game loop
gameLoop();
</script>
</body>
</html>