Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Hi!

  1. User

    Hi!

  2. Assistant

    Hello! How can I assist you today?

  3. User

    Give me code of fps game in html, css, three.js

  4. Assistant

    Sure! Here's a basic example of an FPS (first-person shooter) game using HTML, CSS, and the Three.js library:

    HTML:

    <!DOCTYPE html>
    <html>
    <head>
        <title>FPS Game</title>
        <style>
            body {
                margin: 0;
                overflow: hidden;
                background-color: #000;
            }
            #game-container {
                width: 100%;
                height: 100%;
            }
        </style>
    </head>
    <body>
        <div id="game-container"></div>
    
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
        <script src="game.js"></script>
    </body>
    </html>
    

    CSS:

    #game-container {
        position: absolute;
        top: 0;
        left: 0;
    }
    

    JS (game.js):

    // Set up the scene
    const scene = new THREE.Scene();
    
    // Create the camera
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.z = 5;
    
    // Create the renderer
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.getElementById('game-container').appendChild(renderer.domElement);
    
    // Create a cube as the player object
    const geometry = new THREE.BoxGeometry(1, 1, 1);
    const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
    const player = new THREE.Mesh(geometry, material);
    scene.add(player);
    
    // Create a basic movement control for the player
    const movement = { forward: false, backward: false, left: false, right: false };
    
    function handleKeyDown(event) {
        switch (event.keyCode) {
            case 87: // W
                movement.forward = true;
                break;
            case 83: // S
                movement.backward = true;
                break;
            case 65: // A
                movement.left = true;
                break;
            case 68: // D
                movement.right = true;
                break;
        }
    }
    
    function handleKeyUp(event) {
        switch (event.keyCode) {
            case 87: // W
                movement.forward = false;
                break;
            case 83: // S
                movement.backward = false;
                break;
            case 65: // A
                movement.left = false;
                break;
            case 68: // D
                movement.right = false;
                break;
        }
    }
    
    document.addEventListener('keydown', handleKeyDown);
    document.addEventListener('keyup', handleKeyUp);
    
    // Create the game loop
    function animate() {
        requestAnimationFrame(animate);
    
        // Move the player based on movement controls
        if (movement.forward) player.position.z -= 0.1;
        if (movement.backward) player.position.z += 0.1;
        if (movement.left) player.position.x -= 0.1;
        if (movement.right) player.position.x += 0.1;
    
        renderer.render(scene, camera);
    }
    animate();
    

    This code sets up a simple scene with a cube representing the player object. The player can be moved using the WASD keys. However, keep in mind that this is just a basic example and doesn't include features like collisions, enemies, or shooting mechanics. You can further enhance the game by adding more elements and functionality to suit your needs.

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