File size: 7,644 Bytes
8ee271e
 
 
ab4cf92
13a3049
04d16fc
 
8ee271e
 
13a3049
ab4cf92
f72f7ba
ab4cf92
3952081
 
8ee271e
551f5c9
8ee271e
13a3049
8ee271e
 
 
 
 
 
 
551f5c9
13a3049
 
 
 
 
 
 
05ef5e8
 
3952081
551f5c9
05ef5e8
 
13a3049
 
551f5c9
 
 
 
 
 
 
 
 
 
 
 
 
 
13a3049
 
 
 
 
04d16fc
 
 
 
 
551f5c9
8ee271e
04d16fc
8ee271e
 
13a3049
8ee271e
551f5c9
8ee271e
 
353551f
 
 
 
 
 
 
 
551f5c9
04d16fc
551f5c9
04d16fc
 
8ee271e
ab4cf92
3952081
8ee271e
551f5c9
8ee271e
353551f
3952081
8ee271e
 
 
 
551f5c9
8ee271e
353551f
f72f7ba
8ee271e
ab4cf92
 
 
551f5c9
 
ab4cf92
 
551f5c9
ab4cf92
04d16fc
ab4cf92
 
 
551f5c9
ab4cf92
 
353551f
 
 
 
 
 
 
 
551f5c9
04d16fc
551f5c9
04d16fc
 
ab4cf92
 
3952081
ab4cf92
551f5c9
ab4cf92
353551f
3952081
ab4cf92
 
 
 
551f5c9
ab4cf92
353551f
f72f7ba
ab4cf92
8ee271e
 
551f5c9
 
8ee271e
 
 
 
 
 
 
 
 
 
 
 
 
 
be5f739
ab4cf92
 
 
 
 
 
13a3049
 
 
 
551f5c9
13a3049
 
551f5c9
 
 
 
 
 
 
13a3049
 
 
 
 
 
 
551f5c9
 
 
 
 
 
 
 
 
 
 
13a3049
 
 
ab4cf92
 
 
551f5c9
 
ab4cf92
551f5c9
13a3049
551f5c9
 
ab4cf92
 
 
 
 
 
 
f72f7ba
551f5c9
ab4cf92
13a3049
551f5c9
 
3952081
 
13a3049
ab4cf92
 
 
 
 
13a3049
 
 
 
551f5c9
13a3049
ab4cf92
 
 
f72f7ba
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
const player = document.getElementById('player');
const scoreDisplay = document.getElementById('score');
const gameArea = document.getElementById('game-area');
const gameOverDisplay = document.getElementById('game-over');
const pauseButton = document.getElementById('pause-button');
const pauseMenu = document.getElementById('pause-menu');
const resumeButton = document.getElementById('resume-button');
let score = 0;
let gameOver = false;
let isPaused = false;
let orbSpeed = 2;
let bonusSpeed = 4;
let intervalId;
let orbs = [];
let bonuses = [];

// Player movement (mouse control)
document.addEventListener('mousemove', (e) => {
    if (isPaused || gameOver) return;
    const gameAreaRect = gameArea.getBoundingClientRect();
    let mouseX = e.clientX - gameAreaRect.left;
    if (mouseX < 0) mouseX = 0;
    if (mouseX > gameArea.offsetWidth - player.offsetWidth) mouseX = gameArea.offsetWidth - player.offsetWidth;
    player.style.left = `${mouseX}px`;
});

// Player movement (touch control)
gameArea.addEventListener('touchstart', (e) => {
    touchStartX = e.touches[0].clientX;
    touchCurrentX = touchStartX;
});

gameArea.addEventListener('touchmove', (e) => {
    touchCurrentX = e.touches[0].clientX;
    if (!isPaused && !gameOver) {
        const gameAreaRect = gameArea.getBoundingClientRect();
        let playerLeft = touchCurrentX - gameAreaRect.left - player.offsetWidth / 2;
        playerLeft = Math.max(0, Math.min(playerLeft, gameArea.offsetWidth - player.offsetWidth));
        player.style.left = `${playerLeft}px`;
    }
}, { passive: true });

// Pause on Esc key
document.addEventListener('keydown', (e) => {
    if (e.key === 'Escape') {
        togglePause();
    }
});

// Pause on window blur
window.addEventListener('blur', () => {
    if (!gameOver && !isPaused) {
        togglePause();
    }
});

// Pause button for mobile devices
pauseButton.addEventListener('click', () => {
    togglePause();
});

// Resume button
resumeButton.addEventListener('click', () => {
    togglePause();
});

// Create orb
function createOrb() {
    if (isPaused || gameOver) return;
    const orb = document.createElement('div');
    orb.classList.add('orb');
    orb.style.backgroundColor = `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255})`;
    orb.style.left = `${Math.random() * (gameArea.offsetWidth - 30)}px`;
    orb.style.top = `${Math.random() * -50}px`; // Start above the top
    gameArea.appendChild(orb);

    // Create trail
    const trail = document.createElement('div');
    trail.classList.add('trail');
    trail.style.background = orb.style.background;
    trail.style.left = orb.style.left;
    trail.style.top = orb.style.top;
    gameArea.appendChild(trail);

    let orbInterval = setInterval(() => {
        if (isPaused || gameOver) {
            clearInterval(orbInterval);
            return;
        }
        let orbTop = parseFloat(getComputedStyle(orb).getPropertyValue('top'));
        orb.style.top = `${orbTop + orbSpeed}px`;
        trail.style.top = `${orbTop + orbSpeed}px`;
        if (orbTop > gameArea.offsetHeight) {
            clearInterval(orbInterval);
            gameArea.removeChild(orb);
            gameArea.removeChild(trail);
            orbs = orbs.filter(o => o !== orb);
        }

        // Collision detection
        if (checkCollision(orb, player)) {
            clearInterval(orbInterval);
            gameArea.removeChild(orb);
            gameArea.removeChild(trail);
            score += 10;
            scoreDisplay.textContent = score;
            increaseDifficulty();
        }
    }, 16);

    orbs.push({ element: orb, interval: orbInterval });
}

// Create bonus
function createBonus() {
    if (isPaused || gameOver) return;
    const bonus = document.createElement('div');
    bonus.classList.add('bonus');
    bonus.style.left = `${Math.random() * (gameArea.offsetWidth - 30)}px`;
    bonus.style.top = `${Math.random() * -50}px`; // Start above the top
    gameArea.appendChild(bonus);

    // Create trail
    const trail = document.createElement('div');
    trail.classList.add('trail');
    trail.style.background = bonus.style.background;
    trail.style.left = bonus.style.left;
    trail.style.top = bonus.style.top;
    gameArea.appendChild(trail);

    let bonusInterval = setInterval(() => {
        if (isPaused || gameOver) {
            clearInterval(bonusInterval);
            return;
        }
        let bonusTop = parseFloat(getComputedStyle(bonus).getPropertyValue('top'));
        bonus.style.top = `${bonusTop + bonusSpeed}px`;
        trail.style.top = `${bonusTop + bonusSpeed}px`;
        if (bonusTop > gameArea.offsetHeight) {
            clearInterval(bonusInterval);
            gameArea.removeChild(bonus);
            gameArea.removeChild(trail);
            bonuses = bonuses.filter(b => b !== bonus);
        }

        // Collision detection
        if (checkCollision(bonus, player)) {
            clearInterval(bonusInterval);
            gameArea.removeChild(bonus);
            gameArea.removeChild(trail);
            score += 50; // Bonus gives 50 points
            scoreDisplay.textContent = score;
        }
    }, 16);

    bonuses.push({ element: bonus, interval: bonusInterval });
}

// Collision detection
function checkCollision(a, b) {
    const aRect = a.getBoundingClientRect();
    const bRect = b.getBoundingClientRect();

    return !(
        aRect.bottom < bRect.top ||
        aRect.top > bRect.bottom ||
        aRect.right < bRect.left ||
        aRect.left > bRect.right
    );
}

// Increase difficulty
function increaseDifficulty() {
    orbSpeed += 0.1;
    bonusSpeed += 0.05;
}

// Toggle pause
function togglePause() {
    if (gameOver) return;
    isPaused = !isPaused;

    if (isPaused) {
        clearInterval(intervalId);
        pauseButton.textContent = "Pause";
        pauseMenu.style.display = "block";
        pauseButton.style.display = "none";

        // Stop all orb intervals
        orbs.forEach(o => clearInterval(o.interval));
        bonuses.forEach(b => clearInterval(b.interval));
    } else {
        intervalId = setInterval(() => {
            createOrb();
            if (Math.random() < 0.1) { // 10% chance to create a bonus
                createBonus();
            }
        }, 500);

        pauseMenu.style.display = "none";
        pauseButton.style.display = "block";

        // Resume all orb intervals
        orbs.forEach(o => {
            d.interval = setInterval(o.orbInterval, 16);
        });
        bonuses.forEach(b => {
            b.interval = setInterval(b.bonusInterval, 16);
        });
    }
}

// Game over
function endGame() {
    clearInterval(intervalId);
    orbs.forEach(o => clearInterval(o.interval));
    bonuses.forEach(b => clearInterval(b.interval));
    gameOver = true;
    gameOverDisplay.style.display = "block";
    isPaused = true;
    pauseButton.style.display = "none";
    pauseMenu.style.display = "none";
}

// Start game
function startGame() {
    score = 0;
    scoreDisplay.textContent = score;
    orbSpeed = 2;
    bonusSpeed = 4;
    gameOverDisplay.style.display = "none";
    gameOver = false;
    isPaused = false;
    pauseButton.style.display = "none";
    pauseMenu.style.display = "none";
    orbs = [];
    bonuses = [];

    intervalId = setInterval(() => {
        createOrb();
        if (Math.random() < 0.1) { // 10% chance to create a bonus
            createBonus();
        }
    }, 500);

    // Show pause button on mobile devices
    if (window.innerWidth <= 600) {
        pauseButton.style.display = "block";
    }
}

// Start the game
startGame();