File size: 4,157 Bytes
f79607d
3a4f99d
f79607d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>
<head>
    <title>Autonomous Pong</title>
    <style>
        canvas {
            border: 1px solid white;
            background: black;
            margin: 20px auto;
            display: block;
        }
    </style>
</head>
<body>
    <canvas id="gameCanvas" width="800" height="400"></canvas>

    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');

        // Game objects
        const ball = {
            x: canvas.width / 2,
            y: canvas.height / 2,
            radius: 10,
            speedX: 5,
            speedY: 5
        };

        const paddleWidth = 10;
        const paddleHeight = 60;

        const leftPaddle = {
            x: 50,
            y: canvas.height / 2 - paddleHeight / 2,
            speed: 4
        };

        const rightPaddle = {
            x: canvas.width - 50 - paddleWidth,
            y: canvas.height / 2 - paddleHeight / 2,
            speed: 4
        };

        // Game loop
        function gameLoop() {
            update();
            draw();
            requestAnimationFrame(gameLoop);
        }

        // Update game state
        function update() {
            // Move ball
            ball.x += ball.speedX;
            ball.y += ball.speedY;

            // Ball collision with top and bottom
            if (ball.y + ball.radius > canvas.height || ball.y - ball.radius < 0) {
                ball.speedY = -ball.speedY;
            }

            // Autonomous paddle movement
            // Left paddle follows ball
            if (ball.y > leftPaddle.y + paddleHeight / 2) {
                leftPaddle.y += leftPaddle.speed;
            } else {
                leftPaddle.y -= leftPaddle.speed;
            }

            // Right paddle follows ball
            if (ball.y > rightPaddle.y + paddleHeight / 2) {
                rightPaddle.y += rightPaddle.speed;
            } else {
                rightPaddle.y -= rightPaddle.speed;
            }

            // Keep paddles within canvas
            leftPaddle.y = Math.max(0, Math.min(canvas.height - paddleHeight, leftPaddle.y));
            rightPaddle.y = Math.max(0, Math.min(canvas.height - paddleHeight, rightPaddle.y));

            // Ball collision with paddles
            if (
                (ball.x - ball.radius < leftPaddle.x + paddleWidth &&
                 ball.y > leftPaddle.y && 
                 ball.y < leftPaddle.y + paddleHeight) ||
                (ball.x + ball.radius > rightPaddle.x &&
                 ball.y > rightPaddle.y && 
                 ball.y < rightPaddle.y + paddleHeight)
            ) {
                ball.speedX = -ball.speedX;
                // Add some randomness to ball direction
                ball.speedY += (Math.random() - 0.5) * 2;
            }

            // Reset ball if it goes past paddles
            if (ball.x < 0 || ball.x > canvas.width) {
                ball.x = canvas.width / 2;
                ball.y = canvas.height / 2;
                ball.speedX = -ball.speedX;
                ball.speedY = (Math.random() - 0.5) * 10;
            }
        }

        // Draw game elements
        function draw() {
            // Clear canvas
            ctx.fillStyle = 'black';
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            // Draw ball
            ctx.beginPath();
            ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
            ctx.fillStyle = 'white';
            ctx.fill();
            ctx.closePath();

            // Draw paddles
            ctx.fillStyle = 'white';
            ctx.fillRect(leftPaddle.x, leftPaddle.y, paddleWidth, paddleHeight);
            ctx.fillRect(rightPaddle.x, rightPaddle.y, paddleWidth, paddleHeight);

            // Draw center line
            ctx.beginPath();
            ctx.setLineDash([5, 15]);
            ctx.moveTo(canvas.width / 2, 0);
            ctx.lineTo(canvas.width / 2, canvas.height);
            ctx.strokeStyle = 'white';
            ctx.stroke();
            ctx.setLineDash([]);
        }

        // Start the game
        gameLoop();
    </script>
</body>
</html>