-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
332 lines (282 loc) · 11.7 KB
/
game.js
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
325
326
327
328
329
330
331
332
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Images
function createImage(src) {
const img = new Image();
img.src = src;
return img;
}
// const image = new Image();
// image.src = "_f709b0cd-51a9-49f2-a942-aace1b73fb3e.jfif";
// image.src = "img/background.png";
// Images
let platformImage = createImage('./img/platform.png');
let platformSmallTallImage = createImage('./img/platformSmallTall.png');
let hills = createImage('./img/hills.png');
let background = createImage('./img/background.png');
// let mars = createImage('./img/mars.jfif');
// Sprite
let spriteRunLeft = createImage('./img/spriteRunLeft.png');
let spriteRunRight = createImage('./img/spriteRunRight.png');
let spriteStandLeft = createImage('./img/spriteStandLeft.png');
let spriteStandRight = createImage('./img/spriteStandRight.png');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let player = {};
let platforms = [];
function init(){
player = {
x: canvas.width / 2 - 200,
y: canvas.height - 500,
width: 66,
height: 150,
image: spriteStandRight,
frame: 0,
speed: 10,
jumping: false,
dy: 0, // Vertical velocity
jumpPower: 22,
gravity: 0.5, // Gravity force
};
// Now that 'player' is defined, we can properly assign properties that refer to 'player'.
player.sprites = {
stand: {
right: spriteStandRight,
cropWidth: 177,
width: player.width, // Now correctly refers to 'player.width'
left: spriteStandLeft
},
run: {
right: spriteRunRight,
cropWidth: 341,
width: 127.875,
left: spriteRunLeft
}
};
player.currentSprite = player.sprites.stand.right; // Now correctly refers to 'player.sprites.stand.right'
player.currentCropWidth = player.sprites.stand.cropWidth;
// Add a property to track the last direction
player.lastDirection = 'right';
// Platforms
platforms = [
// { x: 50, y: canvas.height - 200, width: 250, height: 10 },
// { x: 400, y: canvas.height - 300, width: 250, height: 10 },
// ctx.drawImage(platformImage, 0, 500, 250, 100),
// { x: 1000, y: canvas.height - 380, width: 250, height: 10 },
// { x: 2000, y: canvas.height - 580, width: 250, height: 10 },
// { x: 3000, y: canvas.height - 380, width: 250, height: 10 },
// { x: 4000, y: canvas.height - 580, width: 250, height: 10 },
// { x: 5000, y: canvas.height - 380, width: 250, height: 10 },
// { x: 6000, y: canvas.height - 580, width: 250, height: 10 },
// { x: 7000, y: canvas.height - 380, width: 250, height: 10 },
{ x: 7000, y: canvas.height - 380, width: platformImage.width, height: platformImage.width },
{ x: 7000, y: canvas.height - 380, width: platformImage.width, height: platformImage.width },
{ x: 7000, y: canvas.height - 380, width: platformImage.width, height: platformImage.width },
{ x: 7000, y: canvas.height - 380, width: platformImage.width, height: platformImage.height },
{ x: 0, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width - 2, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 2 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 3 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 4 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 5 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 6 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 7 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 8 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 9 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 10 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
{ x: platformImage.width * 11 - 3, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
// { x: 0, y: canvas.height - 100, width: 250000, height: 100 },
// { x: 0, y: canvas.height - 50, width: 1500, height: 10 },
// { x: 2000, y: canvas.height - 50, width: 1500, height: 10 },
// { x: 4000, y: canvas.height - 50, width: 1500, height: 10 },
// { x: 6000, y: canvas.height - 50, width: 1500, height: 10 },
// { x: 6000, y: canvas.height - 50, width: 15000, height: 10 },
];
offSet = 0;
backgroundX = 0;
backgroundX2 = 0;
}
// Scrolling Background
let backgroundX = 0; // Initial background X position
const backgroundSpeed = 3; // Speed of background scrolling
// Scrolling Hills
let backgroundX2 = 0; // Initial background X position
const backgroundSpeed2 = 2; // Speed of background scrolling
// Winning Situation
let offSet = 0;
// A function to draw the player character
function drawPlayer() {
// Draw player sprite
ctx.drawImage(
player.currentSprite,
player.currentCropWidth * player.frame,
0,
player.currentCropWidth,
400,
player.x,
player.y,
player.width,
player.height
);
}
// A function to draw platforms
function drawPlatforms() {
ctx.fillStyle = "green";
platforms.forEach((platform) => {
ctx.drawImage(platformImage, platform.x, platform.y, platformImage.width, platformImage.height);
});
}
// A function to update the game's logic.
function updateGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
// Update player frames and sprites
player.frame++;
if (player.frame > 59 && (player.currentSprite === player.sprites.stand.right ||
player.currentSprite === player.sprites.stand.left)
) {
player.frame = 0;
} else if (
player.frame > 29 && (
player.currentSprite === player.sprites.run.right ||
player.currentSprite === player.sprites.run.left)
) {
player.frame = 0;
}
ctx.drawImage(background, backgroundX, 0, background.width, canvas.height);
// ctx.drawImage(image, backgroundX, 0, image.width, canvas.height);
// ctx.drawImage(image, backgroundX + canvas.width, 0, canvas.width, canvas.height);
ctx.drawImage(hills, backgroundX2, 20, hills.width, hills.height);
ctx.drawImage(hills, backgroundX2+800, 20, hills.width, hills.height);
// ctx.drawImage(platformImage, 0, canvas.height-platformImage.height, platformImage.width, platformImage.height);
drawPlayer();
drawPlatforms();
const leftBound = canvas.width * 0.10; // 10% of the screen width
const rightBound = canvas.width * 0.60; // 60% of the screen width
function updatePlayerSprite(spriteType, direction) {
player.currentSprite = player.sprites[spriteType][direction];
player.currentCropWidth = player.sprites[spriteType].cropWidth;
player.width = player.sprites[spriteType].width;
player.lastDirection = direction;
}
// Move the player to the left if within the bounds
if ((keys["ArrowLeft"] && player.x > leftBound)||
(keys["ArrowLeft"] && offSet === 0 && player.x > 3)) {
player.x -= player.speed;
updatePlayerSprite('run', 'left');
} else if (keys["ArrowLeft"] && player.x <= leftBound && offSet > 0) {
platforms.forEach(platform => platform.x += player.speed);
backgroundX += backgroundSpeed;
backgroundX2 += backgroundSpeed2;
offSet -= 5;
updatePlayerSprite('run', 'left');
} else if (keys["ArrowRight"] && player.x < rightBound) {
player.x += player.speed;
updatePlayerSprite('run', 'right');
} else if (keys["ArrowRight"] && player.x >= rightBound) {
platforms.forEach(platform => platform.x -= player.speed);
backgroundX -= backgroundSpeed;
backgroundX2 -= backgroundSpeed2;
offSet += 5;
updatePlayerSprite('run', 'right');
} else {
// Key release logic
updatePlayerSprite('stand', player.lastDirection);
}
// Handle jumping
if ((keys["ArrowUp"] || keys[" "]) && !player.jumping) {
player.jumping = true;
player.dy = -player.jumpPower; // Jump upwards
}
// if (offSet > 5000) {
// console.log("You Win")
// }
// Apply gravity
player.dy += player.gravity;
player.y += player.dy;
// Check collision with platforms
for (let platform of platforms) {
// if (player.x <= platform.x + (platform.width-20) &&
if (player.x <= platform.x + platform.width &&
// player.x + (player.width/2) >= platform.x &&
player.x + player.width >= platform.x &&
player.y + player.height >= platform.y &&
player.y + player.dy <= platform.y + platform.height) {
player.jumping = false;
player.y = platform.y - player.height;
player.dy = 0;
}
}
// win condition
if (offSet > platformImage.width * 50 - 150) {
console.log("You Win");
displayWinMessage();
setTimeout(() => {
hideMessages();
}, 1000);
setTimeout(() => {
init();
}, 2000);
// offSet = player.x;
console.log(offSet);
} else if (player.y > canvas.height) { // lose condition
console.log("You Lose")
displayLoseMessage();
setTimeout(() => {
init();
hideMessages();
}, 1000);
}
// console.log(`Off Set ${offSet} `);
// console.log(`Player Positiont ${player.x} `);
requestAnimationFrame(updateGame);
}
setInterval(()=>{
console.log(`Off Set ${offSet} `);
console.log(`Player Positiont ${player.x} `);
}, 2500)
// An object to track key presses.
const keys = {};
// An event listeners to handle key presses.
document.addEventListener("keydown", (event) => {
keys[event.key] = true;
// An event listener for jumping (Space key)
if (event.key === " ") {
event.preventDefault(); // Prevent the spacebar from scrolling the page
}
});
document.addEventListener("keyup", (event) => {
keys[event.key] = false;
});
// Start the game loop.
// image.onload = function(){
// init();
// updateGame();
// }
spriteStandRight.onload = function () {
// Start the game loop here, after all images are loaded.
init();
updateGame();
};
function displayWinMessage() {
const winMessage = document.getElementById('winMessage');
winMessage.style.display = 'block';
}
// Displaying the "You Lose" message
function displayLoseMessage() {
const loseMessage = document.getElementById('loseMessage');
loseMessage.style.display = 'block';
}
// This event listener restarts the game when the "Restart" button is clicked
const restartButton = document.getElementById('restartButton');
restartButton.addEventListener('click', () => {
// Reset the game state here
init(); // initialization function to reset the game
hideMessages(); // A function to hide messages
});
function hideMessages() {
const winMessage = document.getElementById('winMessage');
const loseMessage = document.getElementById('loseMessage');
winMessage.style.display = 'none';
loseMessage.style.display = 'none';
}