-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.html
276 lines (239 loc) · 6.78 KB
/
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Road Killers</title>
<style>
/* CSS Styling */
body {
margin: 0;
overflow: hidden;
background-color: #333;
}
canvas {
display: block;
margin: 0 auto;
background-color: #7ec850; /* Grass background */
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
// JavaScript Logic
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game variables
let lives = 3;
let gameOver = false;
// Player properties
const player = {
x: canvas.width / 2 - 20,
y: canvas.height - 70,
width: 40,
height: 40,
speed: 4,
dx: 0,
dy: 0,
color: '#00ff00', // Lizard color
};
// Vehicle properties
const vehicles = [];
const vehicleWidth = 80;
const vehicleHeight = 50;
const lanes = 4;
const roadStartY = 50;
const roadEndY = canvas.height - 50;
const laneHeight = (roadEndY - roadStartY) / lanes;
// Create vehicles in each lane
function createVehicles() {
vehicles.length = 0;
for (let i = 0; i < lanes; i++) {
const vehicle = {
x: Math.random() * canvas.width,
y: roadStartY + i * laneHeight + (laneHeight - vehicleHeight) / 2,
width: vehicleWidth,
height: vehicleHeight,
speed: 1 + Math.random() * 2,
direction: i % 2 === 0 ? 1 : -1, // Alternate direction per lane
color: '#0000FF', // Vehicle color
};
vehicles.push(vehicle);
}
}
// Draw start and end lines
function drawRoadLines() {
// Draw the road
ctx.fillStyle = '#8B4513'; // Brown color for the road
ctx.fillRect(0, roadStartY, canvas.width, roadEndY - roadStartY);
ctx.strokeStyle = '#FFFFFF'; // White color for the lines
ctx.lineWidth = 4;
// Start line
ctx.beginPath();
ctx.moveTo(0, roadEndY);
ctx.lineTo(canvas.width, roadEndY);
ctx.stroke();
// End line
ctx.beginPath();
ctx.moveTo(0, roadStartY);
ctx.lineTo(canvas.width, roadStartY);
ctx.stroke();
}
// Draw player
function drawPlayer() {
// Draw the lizard as a simple shape
ctx.fillStyle = player.color;
ctx.beginPath();
ctx.arc(player.x + player.width / 2, player.y + player.height / 2, player.width / 2, 0, Math.PI * 2);
ctx.fill();
}
// Draw 8-bit vehicles
function drawVehicles() {
vehicles.forEach((vehicle) => {
ctx.fillStyle = vehicle.color;
// Draw the body of the car
ctx.fillRect(
vehicle.x,
vehicle.y + 5,
vehicle.width,
vehicle.height - 10
);
// Draw the top of the car
ctx.fillRect(
vehicle.x + 10,
vehicle.y,
vehicle.width - 20,
vehicle.height - 15
);
// Draw wheels
ctx.fillStyle = '#000000'; // Black color for the wheels
ctx.fillRect(
vehicle.x + 5,
vehicle.y + vehicle.height - 5,
10,
5
);
ctx.fillRect(
vehicle.x + vehicle.width - 15,
vehicle.y + vehicle.height - 5,
10,
5
);
});
}
// Move player
function movePlayer() {
player.x += player.dx;
player.y += player.dy;
// Boundary checking
if (player.x < 0) player.x = 0;
if (player.x + player.width > canvas.width) player.x = canvas.width - player.width;
if (player.y < 0) player.y = 0;
if (player.y + player.height > canvas.height) player.y = canvas.height - player.height;
}
// Move vehicles
function moveVehicles() {
vehicles.forEach((vehicle) => {
vehicle.x += vehicle.speed * vehicle.direction;
// Wrap vehicles around the screen
if (vehicle.direction === 1 && vehicle.x > canvas.width) {
vehicle.x = -vehicle.width;
} else if (vehicle.direction === -1 && vehicle.x + vehicle.width < 0) {
vehicle.x = canvas.width;
}
});
}
// Check collisions
function checkCollisions() {
vehicles.forEach((vehicle) => {
if (
player.x < vehicle.x + vehicle.width &&
player.x + player.width > vehicle.x &&
player.y < vehicle.y + vehicle.height &&
player.y + player.height > vehicle.y
) {
// Collision detected
lives--;
resetPlayer();
if (lives === 0) {
gameOver = true;
setTimeout(() => {
alert('Game Over!');
window.location.reload();
}, 100);
}
}
});
}
// Reset player position
function resetPlayer() {
player.x = canvas.width / 2 - player.width / 2;
player.y = canvas.height - 70;
}
// Check win condition
function checkWin() {
if (player.y <= roadStartY) {
gameOver = true;
setTimeout(() => {
alert('Congratulations! You crossed the road safely.');
window.location.reload();
}, 100);
}
}
// Draw game info
function drawInfo() {
ctx.fillStyle = '#000';
ctx.font = '20px Arial';
ctx.fillText(`Lives: ${lives}`, 10, 30);
}
// Game loop
function gameLoop() {
if (gameOver) {
return;
}
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the road and lines
drawRoadLines();
drawPlayer();
drawVehicles();
drawInfo();
movePlayer();
moveVehicles();
checkCollisions();
checkWin();
requestAnimationFrame(gameLoop);
}
// Keyboard controls
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowUp') {
player.dy = -player.speed;
}
if (e.key === 'ArrowDown') {
player.dy = player.speed;
}
if (e.key === 'ArrowLeft') {
player.dx = -player.speed;
}
if (e.key === 'ArrowRight') {
player.dx = player.speed;
}
});
document.addEventListener('keyup', (e) => {
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
player.dy = 0;
}
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
player.dx = 0;
}
});
// Initialize game
function init() {
createVehicles();
resetPlayer();
gameLoop();
}
init();
</script>
</body>
</html>