-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
301 lines (257 loc) · 10.2 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
// Create the scene and camera
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.y = 2; // Position the camera higher
camera.position.z = 5;
// Load the background texture
let textureLoader = new THREE.TextureLoader();
let backgroundTexture = textureLoader.load('bg.jpeg');
// Set the background of the scene
scene.background = backgroundTexture;
// Create the renderer
let renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Create the cube
let cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
let cubeMaterial = new THREE.MeshBasicMaterial({color: 0x00ff00});
let cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.y = 0.5; // Position the cube on the platform
scene.add(cube);
// Add a black outline to the cube
let outlineGeometry = new THREE.BoxGeometry(1.1, 1.1, 1.1);
let outlineMaterial = new THREE.MeshBasicMaterial({color: 0x000000, side: THREE.BackSide});
let outline = new THREE.Mesh(outlineGeometry, outlineMaterial);
cube.add(outline);
// Create the platforms
let platformGeometry = new THREE.BoxGeometry(5, 0.1, 5);
let platformMaterial = new THREE.MeshBasicMaterial({color: 0xffffff});
for (let i = 0; i < 10; i++) {
let platform = new THREE.Mesh(platformGeometry, platformMaterial);
platform.position.y = -1; // Position the platform below the cube
platform.position.z = -i * 5; // Position the platforms along the z-axis
scene.add(platform);
}
// Define the colors
let colors = [
new THREE.Color('blue'),
new THREE.Color('gold'),
new THREE.Color('red'),
new THREE.Color('purple')
];
let colorIndex = 0;
// Create the obstacles
let obstacleGeometry = new THREE.BoxGeometry(1, 1, 1);
let obstacleMaterial = new THREE.MeshBasicMaterial({color: 0xff0000});
let obstacles = [];
for (let i = 0; i < 10; i++) {
// Use the colors array for the obstacle material
let obstacleMaterial = new THREE.MeshBasicMaterial({color: colors[colorIndex]});
let obstacle = new THREE.Mesh(obstacleGeometry, obstacleMaterial);
obstacle.position.y = 0; // Lower the obstacle to touch the platform
obstacle.position.z = -(i * 10 + 10); // Position the obstacles further apart along the z-axis
obstacle.position.x = Math.random() * 4 - 2; // Position the obstacles randomly along the x-axis
obstacles.push(obstacle);
scene.add(obstacle);
// Cycle through the colors
colorIndex = (colorIndex + 1) % colors.length;
// Add a glowing yellow outline to the obstacles
let outlineGeometry = new THREE.BoxGeometry(1.1, 1.1, 1.1);
let outlineMaterial = new THREE.MeshBasicMaterial({color: 0xffff00, side: THREE.BackSide});
let outline = new THREE.Mesh(outlineGeometry, outlineMaterial);
obstacle.add(outline);
}
// Create the spike obstacles
let spikeGeometry = new THREE.BoxGeometry(4, 1, 1);
let spikeMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
let spikes = [];
for (let i = 0; i < 10; i++) {
if (Math.random() < 0.2) { // Adjust the probability (e.g., 0.2 for 20% chance)
let spikeMaterial = new THREE.MeshBasicMaterial({color: colors[colorIndex]});
let spike = new THREE.Mesh(spikeGeometry, spikeMaterial);
spike.position.y = 0.0; // Lower the spike to touch the platform
spike.position.z = -(i * 10 + 10); // Position the spikes further apart along the z-axis
spike.position.x = Math.random() * 4 - 2; // Position the spikes randomly along the x-axis
spikes.push(spike);
scene.add(spike);
// Add a glowing yellow outline to the spikes
let outlineGeometry = new THREE.BoxGeometry(4.1, 1.1, 1.1);
let outlineMaterial = new THREE.MeshBasicMaterial({color: 0xffff00, side: THREE.BackSide});
let outline = new THREE.Mesh(outlineGeometry, outlineMaterial);
spike.add(outline);
// Cycle through the colors
colorIndex = (colorIndex + 1) % colors.length;
}
}
// Create the stars
let stars = [];
for (let i = 0; i < 100; i++) {
let starGeometry = new THREE.BoxGeometry(0.1, 0.1, 0.1);
let starMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
let star = new THREE.Mesh(starGeometry, starMaterial);
star.position.x = Math.random() * 10 - 5;
star.position.y = Math.random() * 10;
star.position.z = Math.random() * 10 - 5;
stars.push(star);
scene.add(star); // Add stars to the main scene, not a specific cube
}
// Animation
let colorChangeSpeed = 0.01; // Set the speed of the color change
let jumpTime = 200; // Set the jump time to 1000 milliseconds (1 second)
let isJumping = false;
let jumpSpeed = 0.3; // Increase the jump speed to make the cube jump higher
let fallSpeed = 0.2; // Increase the fall speed to match the jump speed
let jumpRotation = 0; // Variable to control the cube's rotation during jump
let spinSpeed = -0.5; // Adjust the speed of cube spin during jump
let spinResetSpeed = 0.30; // Adjust the speed of returning to the original angle
let obstacleSpeed = 0.05;
let isGameOver = false;
let score = 0;
let animate = function () {
if (isGameOver) return;
requestAnimationFrame(animate);
// Continuous movement based on key flags
if (moveLeft && cube.position.x > -2.5 + cube.geometry.parameters.width / 2) {
cube.position.x -= 0.2;
}
if (moveRight && cube.position.x < 2.5 - cube.geometry.parameters.width / 2) {
cube.position.x += 0.2;
}
// Check for collisions with obstacles and spikes
let cubeBox = new THREE.Box3().setFromObject(cube);
for (let i = 0; i < obstacles.length; i++) {
let obstacleBox = new THREE.Box3().setFromObject(obstacles[i]);
if (cubeBox.intersectsBox(obstacleBox)) {
isGameOver = true;
updateHighScore(); // Update high score if needed
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
document.getElementById('gameOverScreen').style.display = 'flex';
}
}
for (let i = 0; i < spikes.length; i++) {
let spikeBox = new THREE.Box3().setFromObject(spikes[i]);
if (cubeBox.intersectsBox(spikeBox)) {
isGameOver = true;
updateHighScore(); // Update high score if needed
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
document.getElementById('gameOverScreen').style.display = 'flex';
}
}
if (isJumping) {
if (jumpTime > 0) {
cube.position.y += jumpSpeed;
jumpRotation += spinSpeed; // Increase the rotation angle during jump
cube.rotation.x = jumpRotation;
jumpTime -= 16.66;
} else {
isJumping = false;
jumpTime = 200;
jumpRotation = 0; // Reset the jump rotation angle
}
} else if (cube.position.y > 0.5) {
cube.position.y -= fallSpeed;
} else {
// Reset rotation angle when cube is on the ground
cube.rotation.x -= spinResetSpeed;
if (cube.rotation.x < 0) {
cube.rotation.x = 0;
}
}
// Move the stars
for (let i = 0; i < stars.length; i++) {
stars[i].position.z += obstacleSpeed;
if (stars[i].position.z > 5) {
stars[i].position.z = -5; // Reposition the stars further away along the z-axis
stars[i].position.x = Math.random() * 10 - 5; // Reposition the stars randomly along the x-axis
}
}
// Move the obstacles
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].position.z += obstacleSpeed;
if (obstacles[i].position.z > 5) {
obstacles[i].position.z = -95; // Reposition the obstacles further away along the z-axis
obstacles[i].position.x = Math.random() * 4 - 2; // Reposition the obstacles randomly along the x-axis
score++; // Increase the score when the cube passes an obstacle
document.getElementById('score').textContent = 'Score: ' + score;
}
}
// Move the spike obstacles
for (let i = 0; i < spikes.length; i++) {
spikes[i].position.z += obstacleSpeed;
if (spikes[i].position.z > 5) {
spikes[i].position.z = -95; // Reposition the spikes further away along the z-axis
spikes[i].position.x = Math.random() * 4 - 2; // Reposition the spikes randomly along the x-axis
}
}
obstacleSpeed += 0.0002; // Increase the obstacle speed...
camera.position.x = cube.position.x;
renderer.render(scene, camera);
};
// Flag variables for continuous movement
let moveLeft = false;
let moveRight = false;
// Keyboard controls
window.addEventListener('keydown', function (event) {
if (isGameOver) return;
switch (event.keyCode) {
case 37: // Left arrow key
moveLeft = true;
break;
case 39: // Right arrow key
moveRight = true;
break;
case 32: // Space key
if (!isJumping && cube.position.y <= 0.5) {
isJumping = true;
}
break;
}
}, false);
window.addEventListener('keyup', function (event) {
// Reset the move flags on key release
switch (event.keyCode) {
case 37: // Left arrow key
moveLeft = false;
break;
case 39: // Right arrow key
moveRight = false;
break;
}
}, false);
// Update high score in local storage
function updateHighScore() {
let currentHighScore = localStorage.getItem('highScore');
if (currentHighScore === null || score > parseInt(currentHighScore)) {
localStorage.setItem('highScore', score.toString());
}
}
// Display high score
function displayHighScore() {
let currentHighScore = localStorage.getItem('highScore');
if (currentHighScore !== null) {
document.getElementById('highScore').textContent = 'High Score: ' + currentHighScore;
}
}
// Display high score on page load
displayHighScore();
let backgroundMusic = new Audio('press_start.mp3');
// Start button
document.getElementById('startButton').addEventListener('click', function () {
document.getElementById('startScreen').style.display = 'none';
backgroundMusic.play();
animate();
});
// Restart button
document.getElementById('restartButton').addEventListener('click', function () {
location.reload(); // Reload the page to restart the game
});
// Event listener for the tutorial button
document.getElementById('tutorialButton').addEventListener('click', function () {
document.getElementById('tutorialContent').style.display = 'block';
});
// Event listener for the close tutorial button
document.getElementById('closeTutorialButton').addEventListener('click', function () {
document.getElementById('tutorialContent').style.display = 'none';
});