File size: 8,675 Bytes
f58a769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a442c0
629e6cb
 
2a442c0
9675c6d
f58a769
7f06df5
f58a769
9675c6d
f58a769
 
 
 
 
 
 
 
 
 
5d24137
7f06df5
f58a769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
629e6cb
 
 
 
 
 
f58a769
629e6cb
f58a769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9675c6d
f58a769
 
629e6cb
 
 
 
 
 
 
 
 
 
 
9675c6d
f58a769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f06df5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d24137
 
 
 
7f06df5
 
 
 
 
 
 
 
5d24137
 
7f06df5
 
5d24137
7f06df5
 
 
 
 
 
 
 
5d24137
7f06df5
5d24137
7f06df5
 
 
 
5d24137
 
 
7f06df5
f58a769
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
document.addEventListener('DOMContentLoaded', () => {
    const canvas = document.getElementById('gameCanvas');
    const ctx = canvas.getContext('2d');
    const startScreen = document.getElementById('startScreen');
    const gameOverScreen = document.getElementById('gameOverScreen');
    const startBtn = document.getElementById('startBtn');
    const restartBtn = document.getElementById('restartBtn');
    const scoreDisplay = document.getElementById('scoreDisplay');
    
    // Set canvas size
    function resizeCanvas() {
        const container = canvas.parentElement;
        canvas.width = container.clientWidth;
        canvas.height = container.clientHeight;
    }
    
    resizeCanvas();
    window.addEventListener('resize', resizeCanvas);
    // Game variables
    let ball = {
        x: 0,
        y: 0,
        radius: 110,
        minRadius: 5,
        maxRadius: 200,
color: '#8B5CF6',
        speed: 3,
        dx: 0,
        dy: 0 // Start with no movement
    };
let rings = [];
    let currentRing = 0;
    let gameActive = false;
    let animationId;
    let touchStartX = 0;
    let touchStartY = 0;
    // Initialize game
    function initGame() {
        ball.x = canvas.width / 2;
        ball.y = canvas.height - 100;
        ball.dx = 0;
        ball.dy = 0; // Start with no movement (let player control)
rings = [];
        currentRing = 0;
        
        // Create 100 rings with random gaps
        for (let i = 0; i < 100; i++) {
            rings.push({
                x: canvas.width / 2,
                y: 50 + i * 60,
                radius: 40 + i * 2,
                thickness: 8,
                gapStart: Math.random() * Math.PI * 2,
                gapSize: Math.PI / 3 + Math.random() * Math.PI / 6,
                color: i % 2 === 0 ? '#7C3AED' : '#6D28D9'
            });
        }
        
        gameActive = true;
        startScreen.classList.add('hidden');
        gameOverScreen.classList.add('hidden');
        animate();
    }
    // Draw ball
    function drawBall() {
        ctx.beginPath();
        ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
        ctx.fillStyle = ball.color;
        ctx.fill();
        ctx.closePath();
        
        // Add shine effect
        ctx.beginPath();
        ctx.arc(ball.x - ball.radius/3, ball.y - ball.radius/3, ball.radius/4, 0, Math.PI * 2);
        ctx.fillStyle = 'rgba(255, 255, 255, 0.4)';
        ctx.fill();
        ctx.closePath();

        // Draw size counter
        ctx.font = `${Math.min(20, ball.radius/2)}px Arial`;
        ctx.fillStyle = 'white';
        ctx.textAlign = 'center';
        ctx.fillText(Math.round(ball.radius), ball.x, ball.y + 5);
    }
// Draw rings
    function drawRings() {
        for (let i = currentRing; i < Math.min(currentRing + 10, rings.length); i++) {
            const ring = rings[i];
            
            // Draw the ring with gap
            ctx.beginPath();
            ctx.arc(ring.x, ring.y, ring.radius, ring.gapStart + ring.gapSize, ring.gapStart, true);
            ctx.lineWidth = ring.thickness;
            ctx.strokeStyle = ring.color;
            ctx.stroke();
            ctx.closePath();
            
            // Add glow effect
            ctx.beginPath();
            ctx.arc(ring.x, ring.y, ring.radius + 5, 0, Math.PI * 2);
            ctx.strokeStyle = 'rgba(167, 139, 250, 0.3)';
            ctx.lineWidth = 2;
            ctx.stroke();
            ctx.closePath();
        }
    }
    
    // Check collision with current ring
    function checkCollision() {
        const ring = rings[currentRing];
        const distance = Math.sqrt((ball.x - ring.x) ** 2 + (ball.y - ring.y) ** 2);
        
        // Check if ball is within ring boundaries
        if (distance > ring.radius - ring.thickness/2 && distance < ring.radius + ring.thickness/2) {
            // Calculate angle to check if ball is passing through the gap
            const angle = Math.atan2(ball.y - ring.y, ball.x - ring.x) + Math.PI;
            const normalizedAngle = (angle - ring.gapStart + Math.PI * 2) % (Math.PI * 2);
            
            if (normalizedAngle > ring.gapSize) {
                return true; // Collision detected
            }
        } else if (distance < ring.radius - ring.thickness/2) {
            return true; // Collision with inner part
        }
        
        return false;
    }
    
    // Update game state
    function update() {
        // Move ball (auto movement only)
        ball.x += ball.dx;
        ball.y += ball.dy;

        // Wall collision - change size
        if (ball.x - ball.radius < 0 || ball.x + ball.radius > canvas.width) {
            ball.dx = -ball.dx;
            ball.radius = Math.max(ball.minRadius, Math.min(ball.maxRadius, ball.radius + (ball.dx > 0 ? -5 : 5)));
        }
        
        if (ball.y - ball.radius < 0 || ball.y + ball.radius > canvas.height) {
            ball.dy = -ball.dy;
            ball.radius = Math.max(ball.minRadius, Math.min(ball.maxRadius, ball.radius + (ball.dy > 0 ? -5 : 5)));
        }
// Check if ball passed current ring
        if (currentRing < rings.length) {
            const ring = rings[currentRing];
            if (ball.y < ring.y - ring.radius - ring.thickness/2) {
                currentRing++;
                
                // Check if game is won
                if (currentRing >= rings.length) {
                    gameWon();
                    return;
                }
            }
            
            // Check collision with current ring
            if (checkCollision()) {
                gameOver();
                return;
            }
        }
    }
    
    // Game over
    function gameOver() {
        gameActive = false;
        cancelAnimationFrame(animationId);
        scoreDisplay.innerHTML = `You reached ring: <span class="text-purple-300">${currentRing}</span>`;
        gameOverScreen.classList.remove('hidden');
    }
    
    // Game won
    function gameWon() {
        gameActive = false;
        cancelAnimationFrame(animationId);
        scoreDisplay.innerHTML = `Congratulations! You completed all <span class="text-purple-300">100</span> rings!`;
        gameOverScreen.classList.remove('hidden');
    }
    
    // Animation loop
    function animate() {
        animationId = requestAnimationFrame(animate);
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        
        drawRings();
        drawBall();
        
        if (gameActive) {
            update();
        }
    }
    // Event listeners
    startBtn.addEventListener('click', () => {
        initGame();
        // Enable controls after game starts
        document.addEventListener('keydown', handleKeyDown);
        document.addEventListener('keyup', handleKeyUp);
        canvas.addEventListener('touchstart', handleTouchStart);
        canvas.addEventListener('touchmove', handleTouchMove);
    });
    
    restartBtn.addEventListener('click', () => {
        initGame();
        // Re-enable controls on restart
        document.addEventListener('keydown', handleKeyDown);
        document.addEventListener('keyup', handleKeyUp);
        canvas.addEventListener('touchstart', handleTouchStart);
        canvas.addEventListener('touchmove', handleTouchMove);
    });

    // Control handlers
    function handleKeyDown(e) {
        if (!gameActive) return;
        
        if (e.key === 'ArrowLeft') {
            ball.dx = -ball.speed;
        } else if (e.key === 'ArrowRight') {
            ball.dx = ball.speed;
        } else if (e.key === 'ArrowUp') {
            ball.dy = -ball.speed;
        } else if (e.key === 'ArrowDown') {
            ball.dy = ball.speed;
        }
    }

    function handleKeyUp(e) {
        if (!gameActive) return;
        
        if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
            ball.dx = 0;
        } else if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
            ball.dy = 0;
        }
    }
function handleTouchStart(e) {
        if (!gameActive) return;
        touchStartX = e.touches[0].clientX;
        touchStartY = e.touches[0].clientY;
    }
    function handleTouchMove(e) {
        if (!gameActive) return;
        e.preventDefault();
        const touchX = e.touches[0].clientX;
        const touchY = e.touches[0].clientY;
        const diffX = touchX - touchStartX;
        const diffY = touchY - touchStartY;
        
        if (Math.abs(diffX) > 5) { // Threshold to prevent accidental movement
            ball.dx = diffX > 0 ? ball.speed : -ball.speed;
        }
        if (Math.abs(diffY) > 5) {
            ball.dy = diffY > 0 ? ball.speed : -ball.speed;
        }
    }
});