Skip to content
The Chat Archive
gpt-4o-2024-08-06· OpenAI WildChat-4.8M (AI2)

Write me a 3D pong game in a single HTML file

  1. User

    Write me a 3D pong game in a single HTML file

  2. Assistant

    Creating a 3D Pong game in a single HTML file involves leveraging WebGL for rendering and JavaScript for game logic. Below is a simple implementation that uses the popular library Three.js to handle 3D rendering. This basic example includes paddles, a ball, and basic controls.

    Firstly, make sure you have internet access to load the Three.js library via a CDN. Here's a simple 3D Pong game in a single HTML file:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>3D Pong Game</title>
        <style>
            body { margin: 0; }
            canvas { display: block; }
        </style>
    </head>
    <body>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
        <script>
            // Basic scene setup
            const scene = new THREE.Scene();
            const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
            const renderer = new THREE.WebGLRenderer();
            renderer.setSize(window.innerWidth, window.innerHeight);
            document.body.appendChild(renderer.domElement);
    
            // Create paddles
            const paddleGeometry = new THREE.BoxGeometry(0.2, 1, 0.3);
            const paddleMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
            const leftPaddle = new THREE.Mesh(paddleGeometry, paddleMaterial);
            const rightPaddle = new THREE.Mesh(paddleGeometry, paddleMaterial);
            leftPaddle.position.x = -2.5;
            rightPaddle.position.x = 2.5;
            scene.add(leftPaddle, rightPaddle);
    
            // Create ball
            const ballGeometry = new THREE.SphereGeometry(0.1, 32, 32);
            const ballMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
            const ball = new THREE.Mesh(ballGeometry, ballMaterial);
            scene.add(ball);
    
            // Ball velocity
            const ballVelocity = new THREE.Vector3(0.05, 0.05, 0);
    
            // Lighting
            const ambientLight = new THREE.AmbientLight(0x404040);
            scene.add(ambientLight);
    
            // Camera position
            camera.position.z = 5;
    
            // Keyboard controls
            let leftPaddleUp = false;
            let leftPaddleDown = false;
            let rightPaddleUp = false;
            let rightPaddleDown = false;
    
            window.addEventListener('keydown', function(event) {
                switch (event.key) {
                    case 'w':
                        leftPaddleUp = true;
                        break;
                    case 's':
                        leftPaddleDown = true;
                        break;
                    case 'ArrowUp':
                        rightPaddleUp = true;
                        break;
                    case 'ArrowDown':
                        rightPaddleDown = true;
                        break;
                }
            });
    
            window.addEventListener('keyup', function(event) {
                switch (event.key) {
                    case 'w':
                        leftPaddleUp = false;
                        break;
                    case 's':
                        leftPaddleDown = false;
                        break;
                    case 'ArrowUp':
                        rightPaddleUp = false;
                        break;
                    case 'ArrowDown':
                        rightPaddleDown = false;
                        break;
                }
            });
    
            // Game loop
            function animate() {
                requestAnimationFrame(animate);
                
                // Ball movement
                ball.position.add(ballVelocity);
    
                // Collision with top and bottom walls
                if (ball.position.y > 3 || ball.position.y < -3) {
                    ballVelocity.y = -ballVelocity.y;
                }
    
                // Reset ball if it goes past paddles
                if (ball.position.x > 3 || ball.position.x < -3) {
                    ball.position.set(0, 0, 0);
                    ballVelocity.x = -ballVelocity.x;
                }
    
                // Paddle collision
                if (ball.position.x < -2.4 && ball.position.y < leftPaddle.position.y + 0.5 && ball.position.y > leftPaddle.position.y - 0.5) {
                    ballVelocity.x = -ballVelocity.x;
                }
                if (ball.position.x > 2.4 && ball.position.y < rightPaddle.position.y + 0.5 && ball.position.y > rightPaddle.position.y - 0.5) {
                    ballVelocity.x = -ballVelocity.x;
                }
    
                // Paddle movement
                if (leftPaddleUp && leftPaddle.position.y < 3) {
                    leftPaddle.position.y += 0.1;
                }
                if (leftPaddleDown && leftPaddle.position.y > -3) {
                    leftPaddle.position.y -= 0.1;
                }
                if (rightPaddleUp && rightPaddle.position.y < 3) {
                    rightPaddle.position.y += 0.1;
                }
                if (rightPaddleDown && rightPaddle.position.y > -3) {
                    rightPaddle.position.y -= 0.1;
                }
    
                renderer.render(scene, camera);
            }
    
            animate();
        </script>
    </body>
    </html>
    

    Explanation of the Code:

    • Scene Setup: Creates a basic Three.js rendering environment with a scene, camera, and WebGL renderer.
    • Paddles & Ball: Uses Three.js BoxGeometry for paddles and SphereGeometry for the ball. The ball's initial velocity is set to move diagonally.
    • Simple Lighting: Adds ambient light to the scene for basic visibility of objects.
    • Event Listeners: Handles keyboard controls to move paddles. 'W' and 'S' keys are used for the left paddle, and the arrow keys for the right paddle.
    • Game Logic: Moves the ball continuously and checks for collisions with paddles and screen boundaries.
    • Game Loop: Uses requestAnimationFrame for smooth rendering. Adjusts ball and paddle positions each frame and handles game logic.

    This code provides a foundational setup for a 3D Pong game using Three.js. You can expand its capabilities by adding scoring systems, better collision detection, and more advanced graphics and shaders.