-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
285 lines (254 loc) · 7.4 KB
/
scripts.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
// Canvas setup
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Load assets
const playerImage = new Image();
playerImage.src = "assets/pharaoh.png";
const bgMusic = document.getElementById("bgMusic");
const jumpSound = document.getElementById("jumpSound");
// Declare essential variables
let cameraOffsetX = 0;
const gravity = 0.8;
const keys = {};
// Platform class
class Platform {
constructor(x, y, width, height, type) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.type = type;
}
draw() {
ctx.beginPath();
ctx.rect(this.x - cameraOffsetX, this.y, this.width, this.height);
if (this.type === "normal") {
ctx.fillStyle = "sienna";
} else if (this.type === "powerup") {
ctx.fillStyle = "gold"; // Change color for powerup platform
} else {
ctx.fillStyle = "gray";
}
ctx.fill();
ctx.strokeStyle = "black";
ctx.stroke();
// Draw a circle in the middle of the powerup platform
if (this.type === "powerup") {
ctx.beginPath();
ctx.arc(
this.x - cameraOffsetX + this.width / 2,
this.y + this.height / 2,
this.width / 4,
0,
Math.PI * 2,
false
);
ctx.fillStyle = "#6b5c00"; // Change this to the color you want for the circle
ctx.fill();
ctx.strokeStyle = "black";
ctx.stroke();
}
}
}
// Load player object
const player = {
x: 50,
y: 200,
width: 60,
height: 100,
speed: 6,
dy: 0,
jumpPower: 15,
grounded: false,
};
// Platforms array
const platforms = [
{ x: 0, y: 400, width: 10000, height: 100 },
{ x: 300, y: 300, width: 100, height: 10 },
{ x: 450, y: 250, width: 100, height: 10 },
{ x: 600, y: 200, width: 100, height: 10 },
{ x: 750, y: 150, width: 100, height: 10 },
{ x: 900, y: 200, width: 100, height: 10 },
{ x: 1100, y: 300, width: 100, height: 10 },
{ x: 1300, y: 200, width: 100, height: 10 },
];
// Create a powerup platform
const powerupPlatform = new Platform(150, 130, 60, 60, "powerup");
platforms.push(powerupPlatform);
// Function to setup event listeners
function setupEventListeners() {
document.addEventListener("keydown", (e) => (keys[e.key] = true));
document.addEventListener("keyup", (e) => (keys[e.key] = false));
}
// Function to draw the player
function drawPlayer() {
ctx.drawImage(
playerImage,
player.x - cameraOffsetX,
player.y,
player.width,
player.height
);
}
// Function to draw platforms
function drawPlatforms() {
platforms.forEach((p) => {
const platform = new Platform(
p.x,
p.y,
p.width,
p.height,
p.type || "normal"
);
platform.draw();
});
}
// Function to draw pyramids
function drawPyramids() {
const bigPyramidY = 200; // Y-coordinate for big pyramid
const smallPyramidY = 250; // Y-coordinate for small pyramid
const offset = 80; // Offset value
const pyramidSpacing = 600; // Spacing between pyramids
// Generate array of x-coordinates for pyramids
const pyramidXCoordinates = [];
for (
let i = -cameraOffsetX - pyramidSpacing;
i < cameraOffsetX + canvas.width + pyramidSpacing;
i += pyramidSpacing
) {
pyramidXCoordinates.push(i);
}
pyramidXCoordinates.forEach((pyramidX) => {
// Drawing big pyramid
let adjustedX = pyramidX - cameraOffsetX * 0.2 + offset; // Apply parallax and offset
drawPyramid(ctx, adjustedX, bigPyramidY, 300, 200, "sandybrown"); // Pass color as an argument
// Drawing small pyramid
adjustedX = pyramidX - cameraOffsetX * 0.2 - offset; // Apply parallax and reverse offset
drawPyramid(ctx, adjustedX, smallPyramidY, 200, 150, "darkgoldenrod"); // Pass color as an argument
});
}
// Function to draw a pyramid
function drawPyramid(ctx, x, y, width, height, color) {
ctx.beginPath();
ctx.moveTo(x, y); // Top of the pyramid
ctx.lineTo(x - width / 2, y + height); // Bottom left
ctx.lineTo(x + width / 2, y + height); // Bottom right
ctx.closePath();
ctx.fillStyle = color; // Use color argument
ctx.fill();
ctx.strokeStyle = "sienna";
ctx.stroke();
ctx.shadowColor = "rgba(0, 0, 0, 0.5)";
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 10;
ctx.shadowOffsetY = 10;
}
// Function to update the camera
function updateCamera() {
let maxOffsetX =
platforms.reduce((max, p) => Math.max(max, p.x + p.width), 0) -
canvas.width +
player.width;
let playerCenterX = player.x + player.width / 2;
if (
playerCenterX > canvas.width / 2 &&
playerCenterX < maxOffsetX + canvas.width / 2
) {
cameraOffsetX = playerCenterX - canvas.width / 2;
}
cameraOffsetX = Math.max(0, Math.min(cameraOffsetX, maxOffsetX));
}
// Function to handle player controls
function playerControls() {
if (keys["ArrowRight"]) {
let newX = player.x + player.speed;
if (!isColliding(newX, player.y, player.width, player.height)) {
player.x = newX;
}
}
if (keys["ArrowLeft"]) {
let newX = player.x - player.speed;
if (newX > 0 && !isColliding(newX, player.y, player.width, player.height)) {
player.x = newX;
}
}
if (keys[" "]) {
if (player.grounded) {
player.dy = -player.jumpPower;
player.grounded = false;
jumpSound.play();
}
}
}
// Function to update the player
function updatePlayer() {
player.dy += gravity;
let newY = player.y + player.dy;
if (!isColliding(player.x, newY, player.width, player.height)) {
player.y = newY;
} else {
if (player.dy > 0) {
// Falling down
while (isColliding(player.x, newY, player.width, player.height)) {
newY--;
}
player.y = newY;
player.dy = -player.dy * 0.3; // Bounce back with 0.3 times the velocity
player.grounded = true;
} else if (player.dy < 0) {
// Jumping up
while (isColliding(player.x, newY, player.width, player.height)) {
newY++;
}
player.y = newY;
player.dy = -player.dy * 0.3; // Bounce back with 0.3 times the velocity
}
}
updateCamera();
}
// Detect collision between player and platforms
function isColliding(x, y, width, height) {
const playerX = x - cameraOffsetX; // Subtract cameraOffsetX from player's x-coordinate
return platforms.some((p) => {
const playerLeft = playerX;
const playerRight = playerX + width;
const playerTop = y;
const playerBottom = y + height;
const platformLeft = p.x - cameraOffsetX; // Subtract cameraOffsetX from platform's x-coordinate
const platformRight = p.x + p.width - cameraOffsetX; // Subtract cameraOffsetX from platform's x-coordinate
const platformTop = p.y;
const platformBottom = p.y + p.height;
return !(
playerLeft > platformRight ||
playerRight < platformLeft ||
playerTop > platformBottom ||
playerBottom < platformTop
);
});
}
// Main game loop
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPyramids();
// Reset shadow properties so they don't affect other drawings
ctx.shadowColor = "rgba(0, 0, 0, 0)";
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
drawPlatforms();
drawPlayer();
playerControls();
updatePlayer();
requestAnimationFrame(gameLoop);
}
// Background music
window.onload = function () {
setupEventListeners();
gameLoop();
window.addEventListener("keydown", function (e) {
if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
bgMusic.volume = 0.3;
bgMusic.play();
}
});
};