-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
261 lines (214 loc) · 6.86 KB
/
script.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
// Game area
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Set the desired size for the player and apple
let playerSize = 100;
let appleSize = 60;
// Calculate the margin from the bottom border
const marginBottom = 0.3 * playerSize;
// Calculate the game area height
const gameHeight = canvas.height - marginBottom;
let gameStarted = false;
let gamePaused = false;
// Variables to hold the position of the player
let playerX = canvas.width / 2 - playerSize / 2;
let playerY = canvas.height - playerSize - marginBottom;
// Variables to hold the falling apples' positions and speed
let apples = [];
// Variables for the score, high score
let score = 0;
// Load the player image
const playerImage = new Image();
playerImage.src = 'img/player.webp';
// Load the apple image
const appleImage = new Image();
appleImage.src = 'img/apple.webp';
// Load the golden apple image
const goldenAppleImage = new Image();
goldenAppleImage.src = 'img/golden-apple.webp';
// Load the audio files
const bgMusic = [
new Audio('./audio/sanjin-i-sma-bitar-5.mp3'),
new Audio('./audio/sanjin-i-sma-bitar-7.mp3'),
];
const redAppleSound = new Audio('./audio/red-apple-sound.mp3');
const goldenAppleSound = new Audio('./audio/golden-apple-sound.mp3');
const missedGoldenAppleSound = new Audio('./audio/miss-golden-apple.mp3');
// Set the volume for the audio files
bgMusic.forEach((track) => (track.volume = 0.2));
redAppleSound.volume = 0.2;
goldenAppleSound.volume = 0.2;
missedGoldenAppleSound.volume = 0.2;
let currentTrack;
// Function to start playing background music randomly
function playBgMusic() {
currentTrack = bgMusic[Math.floor(Math.random() * bgMusic.length)];
currentTrack.loop = true;
currentTrack.play();
}
// Function to pause the background music
function pauseBgMusic() {
if (currentTrack) {
currentTrack.pause();
}
}
// Function to resume the background music
function resumeBgMusic() {
if (currentTrack) {
currentTrack.play();
}
}
// Event listener to move the player with cursor keys
document.addEventListener('keydown', movePlayer);
function movePlayer(event) {
switch (event.keyCode) {
case 37: // Left arrow key
if (playerX - playerSize >= 0) {
playerX -= playerSize; // Move player to the left
}
break;
case 39: // Right arrow key
if (playerX + playerSize <= canvas.width - playerSize) {
playerX += playerSize; // Move player to the right
}
break;
}
}
// Update the game state every frame
function update() {
// Clear the canvas at the beginning of each frame
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!gameStarted) {
let text = 'CLICK ON GAME TO START';
ctx.font = '3rem Pixelify Sans';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
// Measure text width and height
let metrics = ctx.measureText(text);
let textWidth = metrics.width;
let textHeight =
metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
// Draw background rectangle
ctx.fillStyle = 'black';
ctx.fillRect(
canvas.width / 2 - textWidth / 2 - 10,
canvas.height / 2 - textHeight / 2 - 10,
textWidth + 20,
textHeight + 20,
);
// Draw text
ctx.fillStyle = 'white';
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
requestAnimationFrame(update);
return;
}
if (gamePaused) {
let text = 'GAME PAUSED';
ctx.font = '3rem Pixelify Sans';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
// Measure text width and height
let metrics = ctx.measureText(text);
let textWidth = metrics.width;
let textHeight =
metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
// Draw background rectangle
ctx.fillStyle = 'black';
ctx.fillRect(
canvas.width / 2 - textWidth / 2 - 10,
canvas.height / 2 - textHeight / 2 - 10,
textWidth + 20,
textHeight + 20,
);
// Draw text
ctx.fillStyle = 'white';
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
requestAnimationFrame(update);
return;
}
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the player
ctx.drawImage(playerImage, playerX, playerY, playerSize, playerSize);
// Generate new apples randomly
if (Math.random() < 0.02 && apples.length < 7) {
const appleX = Math.random() * (canvas.width - appleSize);
const appleY = Math.random() < 0.01 ? 0 : gameHeight - appleSize;
// Determine if the apple is golden
const isGolden = Math.random() < 0.15;
// Use the same appleSpeed calculation for both red and golden apples
const appleSpeed = Math.random() + 2;
apples.push({ x: appleX, y: 0, speed: appleSpeed, isGolden });
}
// Update apples' positions and draw them
apples.forEach((apple) => {
apple.y += apple.speed;
if (apple.isGolden) {
ctx.drawImage(goldenAppleImage, apple.x, apple.y, appleSize, appleSize);
} else {
ctx.drawImage(appleImage, apple.x, apple.y, appleSize, appleSize);
}
// Check if the player caught the apple
if (
playerX < apple.x + appleSize &&
playerX + playerSize > apple.x &&
playerY < apple.y + appleSize &&
playerY + playerSize > apple.y
) {
// Increase the score and update it on the screen
score += apple.isGolden ? 50 : 10;
updateScore();
// Remove the caught apple
const index = apples.indexOf(apple);
apples.splice(index, 1);
// Play the corresponding sound effect when an apple is caught
if (apple.isGolden) {
goldenAppleSound.play();
} else {
redAppleSound.play();
}
}
// Check if apple reached the bottom
if (apple.y > gameHeight) {
// Decrease the score by 200 for golden apples, 20 for regular apples
score -= apple.isGolden ? 200 : 20;
// Remove the fallen apple
const index = apples.indexOf(apple);
apples.splice(index, 1);
// Play the corresponding sound effect when a golden apple is missed
if (apple.isGolden) {
missedGoldenAppleSound.play();
}
}
});
// Draw score
ctx.fillStyle = 'black';
ctx.font = '2rem Pixelify Sans';
ctx.textAlign = 'center';
ctx.fillText('Score: ' + score, canvas.width / 2, 30);
// Update game state recursively
requestAnimationFrame(update);
}
function updateScore() {
ctx.clearRect(0, 0, canvas.width, 30);
ctx.fillStyle = 'black';
ctx.font = '2rem Pixelify Sans';
ctx.textAlign = 'center';
ctx.fillText('Score: ' + score, canvas.width / 2, 30);
}
// Start the game loop
update();
// Start or pause the game when the game area is clicked
canvas.addEventListener('click', function () {
if (!gameStarted) {
gameStarted = true;
playBgMusic();
} else {
gamePaused = !gamePaused;
if (gamePaused) {
pauseBgMusic();
} else {
resumeBgMusic();
}
}
});