Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -11,6 +11,8 @@ let isPaused = false;
|
|
| 11 |
let orbSpeed = 2;
|
| 12 |
let bonusSpeed = 4;
|
| 13 |
let intervalId;
|
|
|
|
|
|
|
| 14 |
|
| 15 |
// Player movement
|
| 16 |
document.addEventListener('mousemove', (e) => {
|
|
@@ -48,13 +50,11 @@ gameArea.addEventListener('touchstart', (e) => {
|
|
| 48 |
gameArea.addEventListener('touchmove', (e) => {
|
| 49 |
touchCurrentX = e.touches[0].clientX;
|
| 50 |
if (!isPaused && !gameOver) {
|
| 51 |
-
const playerRect = player.getBoundingClientRect();
|
| 52 |
const gameAreaRect = gameArea.getBoundingClientRect();
|
| 53 |
-
let playerLeft =
|
| 54 |
if (playerLeft < 0) playerLeft = 0;
|
| 55 |
if (playerLeft > gameArea.offsetWidth - player.offsetWidth) playerLeft = gameArea.offsetWidth - player.offsetWidth;
|
| 56 |
player.style.left = `${playerLeft}px`;
|
| 57 |
-
touchStartX = touchCurrentX;
|
| 58 |
}
|
| 59 |
}, { passive: true });
|
| 60 |
|
|
@@ -77,6 +77,7 @@ function createOrb() {
|
|
| 77 |
orb.style.left = `${Math.random() * (gameArea.offsetWidth - 30)}px`;
|
| 78 |
orb.style.top = `${Math.random() * 50 - 50}px`; // Start above the top
|
| 79 |
gameArea.appendChild(orb);
|
|
|
|
| 80 |
|
| 81 |
// Create trail
|
| 82 |
const trail = document.createElement('div');
|
|
@@ -93,10 +94,12 @@ function createOrb() {
|
|
| 93 |
}
|
| 94 |
let orbTop = parseFloat(getComputedStyle(orb).getPropertyValue('top'));
|
| 95 |
orb.style.top = `${orbTop + orbSpeed}px`;
|
|
|
|
| 96 |
if (orbTop > gameArea.offsetHeight) {
|
| 97 |
clearInterval(interval);
|
| 98 |
gameArea.removeChild(orb);
|
| 99 |
gameArea.removeChild(trail);
|
|
|
|
| 100 |
}
|
| 101 |
|
| 102 |
// Collision detection
|
|
@@ -119,6 +122,7 @@ function createBonus() {
|
|
| 119 |
bonus.style.left = `${Math.random() * (gameArea.offsetWidth - 30)}px`;
|
| 120 |
bonus.style.top = `${Math.random() * 50 - 50}px`; // Start above the top
|
| 121 |
gameArea.appendChild(bonus);
|
|
|
|
| 122 |
|
| 123 |
// Create trail
|
| 124 |
const trail = document.createElement('div');
|
|
@@ -135,10 +139,12 @@ function createBonus() {
|
|
| 135 |
}
|
| 136 |
let bonusTop = parseFloat(getComputedStyle(bonus).getPropertyValue('top'));
|
| 137 |
bonus.style.top = `${bonusTop + bonusSpeed}px`;
|
|
|
|
| 138 |
if (bonusTop > gameArea.offsetHeight) {
|
| 139 |
clearInterval(interval);
|
| 140 |
gameArea.removeChild(bonus);
|
| 141 |
gameArea.removeChild(trail);
|
|
|
|
| 142 |
}
|
| 143 |
|
| 144 |
// Collision detection
|
|
@@ -213,6 +219,8 @@ function startGame() {
|
|
| 213 |
isPaused = false;
|
| 214 |
pauseButton.style.display = 'none';
|
| 215 |
pauseMenu.style.display = 'none';
|
|
|
|
|
|
|
| 216 |
|
| 217 |
intervalId = setInterval(() => {
|
| 218 |
createOrb();
|
|
|
|
| 11 |
let orbSpeed = 2;
|
| 12 |
let bonusSpeed = 4;
|
| 13 |
let intervalId;
|
| 14 |
+
let orbs = [];
|
| 15 |
+
let bonuses = [];
|
| 16 |
|
| 17 |
// Player movement
|
| 18 |
document.addEventListener('mousemove', (e) => {
|
|
|
|
| 50 |
gameArea.addEventListener('touchmove', (e) => {
|
| 51 |
touchCurrentX = e.touches[0].clientX;
|
| 52 |
if (!isPaused && !gameOver) {
|
|
|
|
| 53 |
const gameAreaRect = gameArea.getBoundingClientRect();
|
| 54 |
+
let playerLeft = touchCurrentX - gameAreaRect.left - player.offsetWidth / 2;
|
| 55 |
if (playerLeft < 0) playerLeft = 0;
|
| 56 |
if (playerLeft > gameArea.offsetWidth - player.offsetWidth) playerLeft = gameArea.offsetWidth - player.offsetWidth;
|
| 57 |
player.style.left = `${playerLeft}px`;
|
|
|
|
| 58 |
}
|
| 59 |
}, { passive: true });
|
| 60 |
|
|
|
|
| 77 |
orb.style.left = `${Math.random() * (gameArea.offsetWidth - 30)}px`;
|
| 78 |
orb.style.top = `${Math.random() * 50 - 50}px`; // Start above the top
|
| 79 |
gameArea.appendChild(orb);
|
| 80 |
+
orbs.push(orb);
|
| 81 |
|
| 82 |
// Create trail
|
| 83 |
const trail = document.createElement('div');
|
|
|
|
| 94 |
}
|
| 95 |
let orbTop = parseFloat(getComputedStyle(orb).getPropertyValue('top'));
|
| 96 |
orb.style.top = `${orbTop + orbSpeed}px`;
|
| 97 |
+
trail.style.top = `${orbTop + orbSpeed}px`;
|
| 98 |
if (orbTop > gameArea.offsetHeight) {
|
| 99 |
clearInterval(interval);
|
| 100 |
gameArea.removeChild(orb);
|
| 101 |
gameArea.removeChild(trail);
|
| 102 |
+
orbs = orbs.filter(o => o !== orb);
|
| 103 |
}
|
| 104 |
|
| 105 |
// Collision detection
|
|
|
|
| 122 |
bonus.style.left = `${Math.random() * (gameArea.offsetWidth - 30)}px`;
|
| 123 |
bonus.style.top = `${Math.random() * 50 - 50}px`; // Start above the top
|
| 124 |
gameArea.appendChild(bonus);
|
| 125 |
+
bonuses.push(bonus);
|
| 126 |
|
| 127 |
// Create trail
|
| 128 |
const trail = document.createElement('div');
|
|
|
|
| 139 |
}
|
| 140 |
let bonusTop = parseFloat(getComputedStyle(bonus).getPropertyValue('top'));
|
| 141 |
bonus.style.top = `${bonusTop + bonusSpeed}px`;
|
| 142 |
+
trail.style.top = `${bonusTop + bonusSpeed}px`;
|
| 143 |
if (bonusTop > gameArea.offsetHeight) {
|
| 144 |
clearInterval(interval);
|
| 145 |
gameArea.removeChild(bonus);
|
| 146 |
gameArea.removeChild(trail);
|
| 147 |
+
bonuses = bonuses.filter(b => b !== bonus);
|
| 148 |
}
|
| 149 |
|
| 150 |
// Collision detection
|
|
|
|
| 219 |
isPaused = false;
|
| 220 |
pauseButton.style.display = 'none';
|
| 221 |
pauseMenu.style.display = 'none';
|
| 222 |
+
orbs = [];
|
| 223 |
+
bonuses = [];
|
| 224 |
|
| 225 |
intervalId = setInterval(() => {
|
| 226 |
createOrb();
|