Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
324 lines (282 loc) · 7.93 KB
/
Copy pathgame.js
File metadata and controls
324 lines (282 loc) · 7.93 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Setup canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Info elements
const infoLives = document.getElementById('lives');
const infoScore = document.getElementById('score');
const infoLevel = document.getElementById('level');
// Global game variables
let level = 1;
let levelEnd = 1000; // x-coordinate objective to finish level
let gameOver = false;
// Key tracking
const keys = {};
window.addEventListener('keydown', e => keys[e.code] = true);
window.addEventListener('keyup', e => keys[e.code] = false);
// Player object with animation properties
const player = {
x: 100,
y: 500,
width: 32,
height: 48,
vx: 0,
vy: 0,
speed: 3,
jumpStrength: -10,
onGround: false,
lives: 3,
score: 0,
startX: 100,
startY: 500,
frameIndex: 0, // current frame of animation
frameCount: 4, // total frames in sprite sheet
frameDuration: 200, // time (ms) per frame
lastFrameTime: 0
};
// Load player sprite sheet image
const playerSprite = new Image();
playerSprite.src = 'player.png'; // Ensure this file exists in your project
// Gravity constant
const gravity = 0.5;
// Platforms array – common to all levels
let platforms = [
{ x: 0, y: 550, width: 1200, height: 50 }, // ground platform
{ x: 200, y: 450, width: 100, height: 10 },
{ x: 400, y: 400, width: 150, height: 10 },
{ x: 700, y: 350, width: 100, height: 10 }
];
// Enemies array – will be spawned based on level
let enemies = [];
// Bullets array – player’s pistol bullets
let bullets = [];
// Camera offset for horizontal scrolling
let cameraX = 0;
// Spawn enemies based on current level. In level 1 we keep them sparse.
function spawnEnemies() {
enemies = [];
let numEnemies = Math.min(2 + level, 6);
for (let i = 0; i < numEnemies; i++) {
let ex = 250 + i * ((levelEnd - 350) / numEnemies);
enemies.push({
x: ex,
y: 510, // positioned on the ground platform
width: 32,
height: 32,
vx: 1 + 0.2 * level, // slightly faster at higher levels
minX: ex - 50,
maxX: ex + 50
});
}
}
spawnEnemies();
// Update info display
function updateInfo() {
infoLives.innerText = player.lives;
infoScore.innerText = player.score;
infoLevel.innerText = level;
}
// Player shooting: when "KeyZ" is pressed, fire a bullet.
function shootBullet() {
bullets.push({
x: player.x + player.width, // start from player's right side
y: player.y + player.height / 2,
width: 8,
height: 4,
vx: 6
});
}
window.addEventListener('keydown', e => {
if (e.code === 'KeyZ') {
shootBullet();
}
});
// Update player's animation frame based on time
function updatePlayerAnimation(deltaTime) {
player.lastFrameTime += deltaTime;
if (player.lastFrameTime >= player.frameDuration) {
player.frameIndex = (player.frameIndex + 1) % player.frameCount;
player.lastFrameTime = 0;
}
}
// Draw the parallax background
function drawParallax() {
// Far background: a gradient sky.
let gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, '#001');
gradient.addColorStop(1, '#123');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Near background: hills that move slower than the camera for parallax effect.
ctx.fillStyle = '#022';
let hillOffset = (cameraX * 0.3) % canvas.width;
ctx.beginPath();
ctx.arc(-hillOffset, canvas.height, 150, Math.PI, 2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.arc(canvas.width - hillOffset, canvas.height, 150, Math.PI, 2 * Math.PI);
ctx.fill();
}
// Update game state
let lastTime = performance.now();
function update() {
let now = performance.now();
let deltaTime = now - lastTime;
lastTime = now;
if (gameOver) return;
// Update player animation
updatePlayerAnimation(deltaTime);
// Horizontal movement
if (keys['ArrowLeft']) {
player.vx = -player.speed;
} else if (keys['ArrowRight']) {
player.vx = player.speed;
} else {
player.vx = 0;
}
// Jump if on ground
if ((keys['Space'] || keys['ArrowUp']) && player.onGround) {
player.vy = player.jumpStrength;
player.onGround = false;
}
// Apply gravity
player.vy += gravity;
// Update player position
player.x += player.vx;
player.y += player.vy;
// Collision with platforms
player.onGround = false;
for (let platform of platforms) {
if (player.x < platform.x + platform.width &&
player.x + player.width > platform.x &&
player.y < platform.y + platform.height &&
player.y + player.height > platform.y) {
if (player.vy > 0) {
player.y = platform.y - player.height;
player.vy = 0;
player.onGround = true;
}
}
}
// Lose life if player falls off screen
if (player.y > canvas.height) {
loseLife();
}
// Update bullets
for (let i = bullets.length - 1; i >= 0; i--) {
let b = bullets[i];
b.x += b.vx;
if (b.x > canvas.width + cameraX) {
bullets.splice(i, 1);
} else {
for (let j = enemies.length - 1; j >= 0; j--) {
let e = enemies[j];
if (b.x < e.x + e.width &&
b.x + b.width > e.x &&
b.y < e.y + e.height &&
b.y + b.height > e.y) {
enemies.splice(j, 1);
bullets.splice(i, 1);
player.score += 100;
break;
}
}
}
}
// Update enemy positions and check collisions with player
for (let enemy of enemies) {
enemy.x += enemy.vx;
if (enemy.x < enemy.minX || enemy.x + enemy.width > enemy.maxX) {
enemy.vx *= -1;
}
if (player.x < enemy.x + enemy.width &&
player.x + player.width > enemy.x &&
player.y < enemy.y + enemy.height &&
player.y + player.height > enemy.y) {
loseLife();
}
}
// Level objective: progress to next level if player reaches levelEnd
if (player.x >= levelEnd) {
levelComplete();
}
// Update camera to follow player horizontally
cameraX = player.x - 100;
updateInfo();
}
// Handle life loss and game over
function loseLife() {
player.lives--;
if (player.lives <= 0) {
alert("Game Over!");
resetGame();
} else {
player.x = player.startX;
player.y = player.startY;
player.vx = 0;
player.vy = 0;
}
}
// Handle level completion: advance level and increase difficulty
function levelComplete() {
alert("Level " + level + " Complete!");
level++;
levelEnd += 500;
spawnEnemies();
player.x = player.startX;
player.y = player.startY;
player.vx = 0;
player.vy = 0;
}
// Reset entire game state
function resetGame() {
player.lives = 3;
player.score = 0;
level = 1;
levelEnd = 1000;
spawnEnemies();
player.x = player.startX;
player.y = player.startY;
player.vx = 0;
player.vy = 0;
gameOver = false;
}
// Draw game scene
function draw() {
// Draw parallax background first
drawParallax();
// Draw platforms
ctx.fillStyle = '#555';
for (let platform of platforms) {
ctx.fillRect(platform.x - cameraX, platform.y, platform.width, platform.height);
}
// Draw enemies
ctx.fillStyle = '#FF0000';
for (let enemy of enemies) {
ctx.fillRect(enemy.x - cameraX, enemy.y, enemy.width, enemy.height);
}
// Draw bullets
ctx.fillStyle = '#FFFF00';
for (let bullet of bullets) {
ctx.fillRect(bullet.x - cameraX, bullet.y, bullet.width, bullet.height);
}
// Draw player using the sprite sheet animation
let frameWidth = playerSprite.width / player.frameCount;
let sx = player.frameIndex * frameWidth;
let sy = 0;
ctx.drawImage(playerSprite, sx, sy, frameWidth, playerSprite.height,
player.x - cameraX, player.y, player.width, player.height);
// Draw level finish line
ctx.strokeStyle = '#00FF00';
ctx.beginPath();
ctx.moveTo(levelEnd - cameraX, 0);
ctx.lineTo(levelEnd - cameraX, canvas.height);
ctx.stroke();
}
// Main game loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Start the game loop
gameLoop();