Skip to content
The Chat Archive
claude-3-5-sonnet-20241022· Anthropic LMArena Human Preference 140K

i want to create an air hockey game for mobile where player would use his thumb/finger…

  1. User

    i want to create an air hockey game for mobile where player would use his thumb/finger to move the circle to hit the "ball/ puck". The players paddle should follow the players thumb/finger and should be very fast to keep up. I dont want to use game engines like unity, i want simple ways: javascript with libraries with cdn links in single html file. The game should have an AI-player that will play against the player. The game field should be on full screen of the phone, the scores and buttons should appear only after the round/game ends. After the game starts the puck should be in still position (not moving) in front of the player - by touching the puck the game starts. When the round ends the score should appear and the "start next round" button should appear as well. The round starts again with standing still puck but now in front of the player who lost the previous round. The AI should also start the round with the initial hit if he lost the previous round (add 2 second delay before he makes a hit so the user can be prepared at the start of the round). The Ai should not just bluntly move towards the puck, he should move on backside and move up and down only if the puck slows down or stops on his field. I think this is simple and intuitive way to make a game like that. I have linux mint cinnamon and samsung galaxy core 03

  2. Assistant

    Here's a simplified version of the air hockey game using Matter.js physics engine for smoother gameplay and better collision handling:

    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
        <title>Air Hockey</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.19.0/matter.min.js"></script>
        <style>
            * { margin: 0; padding: 0; }
            body { overflow: hidden; }
            canvas { touch-action: none; }
            #score {
                position: fixed;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                background: rgba(0,0,0,0.8);
                color: white;
                padding: 20px;
                border-radius: 10px;
                text-align: center;
                display: none;
            }
            #nextRound {
                margin-top: 10px;
                padding: 10px 20px;
                background: #4CAF50;
                border: none;
                color: white;
                border-radius: 5px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div id="score">
            <div id="scoreText"></div>
            <button id="nextRound">Next Round</button>
        </div>
        <script>
            const Engine = Matter.Engine,
                  Render = Matter.Render,
                  World = Matter.World,
                  Bodies = Matter.Bodies,
                  Body = Matter.Body;
    
            const engine = Engine.create();
            const render = Render.create({
                element: document.body,
                engine: engine,
                options: {
                    width: window.innerWidth,
                    height: window.innerHeight,
                    wireframes: false,
                    background: '#223'
                }
            });
    
            // Game objects
            const wallOptions = { isStatic: true, render: { fillStyle: '#666' } };
            const walls = [
                Bodies.rectangle(window.innerWidth/2, 0, window.innerWidth, 20, wallOptions),
                Bodies.rectangle(window.innerWidth/2, window.innerHeight, window.innerWidth, 20, wallOptions),
            ];
    
            const paddleOptions = {
                friction: 0,
                restitution: 0.7,
                density: 0.1,
                render: { fillStyle: '#4CAF50' }
            };
    
            const playerPaddle = Bodies.circle(window.innerWidth - 50, window.innerHeight/2, 30, paddleOptions);
            const aiPaddle = Bodies.circle(50, window.innerHeight/2, 30, { ...paddleOptions, render: { fillStyle: '#f44336' } });
            const puck = Bodies.circle(window.innerWidth - 100, window.innerHeight/2, 15, {
                friction: 0,
                restitution: 1,
                density: 0.01,
                render: { fillStyle: '#fff' }
            });
    
            World.add(engine.world, [...walls, playerPaddle, aiPaddle, puck]);
            Engine.run(engine);
            Render.run(render);
    
            let playerScore = 0;
            let aiScore = 0;
            let gameStarted = false;
            let aiStarts = false;
    
            // Touch controls
            document.addEventListener('touchmove', (e) => {
                e.preventDefault();
                const touch = e.touches[0];
                Body.setPosition(playerPaddle, {
                    x: Math.min(Math.max(touch.clientX, window.innerWidth/2), window.innerWidth - 30),
                    y: touch.clientY
                });
    
                if (!gameStarted) {
                    const dx = touch.clientX - puck.position.x;
                    const dy = touch.clientY - puck.position.y;
                    if (Math.sqrt(dx*dx + dy*dy) < 45) {
                        gameStarted = true;
                        Body.setVelocity(puck, { x: -10, y: 0 });
                    }
                }
            }, { passive: false });
    
            // AI logic
            function updateAI() {
                if (!gameStarted) return;
                
                const defaultX = 100;
                let targetY = window.innerHeight/2;
    
                if (puck.position.x < window.innerWidth/2) {
                    if (puck.velocity.x < 1 && puck.velocity.x > -1) {
                        targetY = puck.position.y;
                    }
                }
    
                Body.setPosition(aiPaddle, {
                    x: defaultX,
                    y: aiPaddle.position.y + (targetY - aiPaddle.position.y) * 0.1
                });
            }
    
            function resetRound(aiStart) {
                gameStarted = false;
                const startX = aiStart ? 100 : window.innerWidth - 100;
                Body.setPosition(puck, { x: startX, y: window.innerHeight/2 });
                Body.setVelocity(puck, { x: 0, y: 0 });
                Body.setPosition(playerPaddle, { x: window.innerWidth - 50, y: window.innerHeight/2 });
                Body.setPosition(aiPaddle, { x: 50, y: window.innerHeight/2 });
    
                if (aiStart) {
                    setTimeout(() => {
                        if (!gameStarted) {
                            gameStarted = true;
                            Body.setVelocity(puck, { x: 10, y: 0 });
                        }
                    }, 2000);
                }
            }
    
            function checkScore() {
                if (puck.position.x < 0) {
                    playerScore++;
                    showScore(false);
                } else if (puck.position.x > window.innerWidth) {
                    aiScore++;
                    showScore(true);
                }
            }
    
            function showScore(aiWon) {
                const scoreDiv = document.getElementById('score');
                const scoreText = document.getElementById('scoreText');
                scoreText.innerHTML = `Player: ${playerScore}<br>AI: ${aiScore}`;
                scoreDiv.style.display = 'block';
                aiStarts = !aiWon;
            }
    
            document.getElementById('nextRound').addEventListener('click', () => {
                document.getElementById('score').style.display = 'none';
                resetRound(aiStarts);
            });
    
            // Game loop
            setInterval(() => {
                updateAI();
                checkScore();
            }, 1000/60);
    
            // Initial setup
            resetRound(false);
        </script>
    </body>
    </html>
    

    This version includes:

    1. Matter.js Physics Engine for realistic collisions and movement
    2. Responsive Design that works on mobile screens
    3. Touch Controls that follow your finger quickly
    4. AI Behavior that:
      • Stays back on its side
      • Only moves to intercept when the puck is slow or stopped
      • Has a 2-second delay when starting rounds
    5. Scoring System that:
      • Shows scores after each round
      • Allows starting next round
      • Places puck in front of the losing player

    To use:

    1. Save as an HTML file
    2. Open in your mobile browser
    3. Touch and drag to move your paddle
    4. Touch the puck to start the game
    5. Click "Next Round" after scoring

    The game should run smoothly on your Samsung Galaxy Core 03. The physics engine ensures realistic puck movement and collisions, while the touch controls are optimized for mobile play.

    Let me know if you'd like any adjustments to the game mechanics or visual style!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.