🎮ArcadeLab

بازی شوتر اول شخص ساده

by WildWave36
185 lines7.3 KB🛠️ Three.js (3D graphics)
▶ Play
<!--ARCADELAB title: بازی شوتر اول شخص description: یک بازی شوتر ساده با تفنگ و چاقو ساخته شده با Three.js libraries: three emoji: 🔫 color: blue -->
<!DOCTYPE html>
<html lang="fa">
<head>
    <meta charset="UTF-8">
    <title>بازی شوتر اول شخص ساده</title>
    <style>
        body { margin: 0; overflow: hidden; font-family: sans-serif; }
        #info {
            position: absolute; top: 10px; left: 10px; color: white;
            background: rgba(0,0,0,0.5); padding: 10px; border-radius: 5px;
            pointer-events: none; z-index: 10; font-size: 14px;
        }
        #crosshair {
            position: absolute; top: 50%; left: 50%;
            width: 20px; height: 20px;
            transform: translate(-50%, -50%);
            pointer-events: none; z-index: 10;
        }
        #crosshair::before, #crosshair::after {
            content: ''; position: absolute; background: red;
        }
        #crosshair::before { width: 2px; height: 20px; left: 9px; }
        #crosshair::after { width: 20px; height: 2px; top: 9px; }
        #weapon-indicator {
            position: absolute; bottom: 20px; left: 20px;
            color: white; background: rgba(0,0,0,0.5);
            padding: 10px 20px; border-radius: 5px;
            font-size: 18px; z-index: 10;
        }
    </style>
</head>
<body>
    <div id="info">
        <strong>حرکت:</strong> WASD | <strong>شلیک:</strong> کلیک چپ | 
        <strong>تعویض اسلحه:</strong> کلید 1 (تفنگ) / 2 (چاقو)
    </div>
    <div id="crosshair"></div>
    <div id="weapon-indicator">🔫 تفنگ</div>

    <!-- بدون import، بدون type="module" -->
    <script>
        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0x87CEEB);
        scene.fog = new THREE.Fog(0x87CEEB, 10, 50);

        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.rotation.order = 'YXZ';

        const renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.shadowMap.enabled = true;
        document.body.appendChild(renderer.domElement);

        const ambientLight = new THREE.AmbientLight(0x404060);
        scene.add(ambientLight);
        const dirLight = new THREE.DirectionalLight(0xffffff, 1);
        dirLight.position.set(5, 10, 7);
        dirLight.castShadow = true;
        scene.add(dirLight);

        const groundGeo = new THREE.PlaneGeometry(100, 100);
        const groundMat = new THREE.MeshStandardMaterial({ color: 0x3a5f3a, roughness: 0.8 });
        const ground = new THREE.Mesh(groundGeo, groundMat);
        ground.rotation.x = -Math.PI / 2;
        ground.receiveShadow = true;
        scene.add(ground);

        const obstacles = [];
        const boxGeo = new THREE.BoxGeometry(1.5, 1.5, 1.5);
        const boxMat = new THREE.MeshStandardMaterial({ color: 0x8B4513 });
        for (let i = 0; i < 15; i++) {
            const box = new THREE.Mesh(boxGeo, boxMat);
            box.position.set(
                (Math.random() - 0.5) * 40,
                0.75,
                (Math.random() - 0.5) * 40
            );
            box.castShadow = true;
            box.receiveShadow = true;
            scene.add(box);
            obstacles.push(box);
        }

        const player = { height: 1.6, speed: 6, yaw: 0, pitch: 0 };
        camera.position.y = player.height;

        let isLocked = false;
        const canvas = renderer.domElement;

        canvas.addEventListener('click', () => {
            if (!isLocked) {
                canvas.requestPointerLock();
            } else {
                fireWeapon();
            }
        });

        document.addEventListener('pointerlockchange', () => {
            isLocked = document.pointerLockElement === canvas;
        });

        document.addEventListener('mousemove', (e) => {
            if (!isLocked) return;
            player.yaw -= e.movementX * 0.002;
            player.pitch -= e.movementY * 0.002;
            player.pitch = Math.max(-Math.PI/2 + 0.1, Math.min(Math.PI/2 - 0.1, player.pitch));
        });

        const keys = {};
        document.addEventListener('keydown', (e) => {
            keys[e.code] = true;
            if (e.code === 'Digit1') switchWeapon('gun');
            if (e.code === 'Digit2') switchWeapon('knife');
        });
        document.addEventListener('keyup', (e) => { keys[e.code] = false; });

        let currentWeapon = 'gun';
        const weaponIndicator = document.getElementById('weapon-indicator');

        function switchWeapon(type) {
            currentWeapon = type;
            weaponIndicator.textContent = type === 'gun' ? '🔫 تفنگ' : '🔪 چاقو';
        }

        function fireWeapon() {
            if (currentWeapon === 'gun') {
                const raycaster = new THREE.Raycaster();
                raycaster.setFromCamera(new THREE.Vector2(0, 0), camera);
                const intersects = raycaster.intersectObjects(obstacles);
                if (intersects.length > 0) {
                    const hit = intersects[0].object;
                    hit.material.color.setHex(0xff0000);
                    setTimeout(() => hit.material.color.setHex(0x8B4513), 200);
                }
                camera.position.y += 0.02;
                setTimeout(() => camera.position.y -= 0.02, 50);
            } else if (currentWeapon === 'knife') {
                const raycaster = new THREE.Raycaster();
                raycaster.setFromCamera(new THREE.Vector2(0, 0), camera);
                raycaster.far = 2.5;
                const intersects = raycaster.intersectObjects(obstacles);
                if (intersects.length > 0) {
                    const hit = intersects[0].object;
                    hit.material.color.setHex(0xffaa00);
                    setTimeout(() => hit.material.color.setHex(0x8B4513), 200);
                }
            }
        }

        const clock = new THREE.Clock();

        function animate() {
            requestAnimationFrame(animate);
            const delta = clock.getDelta();

            camera.rotation.y = player.yaw;
            camera.rotation.x = player.pitch;

            const moveSpeed = player.speed * delta;
            const forward = new THREE.Vector3(0, 0, -1).applyEuler(new THREE.Euler(0, player.yaw, 0));
            const right = new THREE.Vector3(1, 0, 0).applyEuler(new THREE.Euler(0, player.yaw, 0));

            if (keys['KeyW']) camera.position.add(forward.clone().multiplyScalar(moveSpeed));
            if (keys['KeyS']) camera.position.add(forward.clone().multiplyScalar(-moveSpeed));
            if (keys['KeyA']) camera.position.add(right.clone().multiplyScalar(-moveSpeed));
            if (keys['KeyD']) camera.position.add(right.clone().multiplyScalar(moveSpeed));

            camera.position.x = Math.max(-45, Math.min(45, camera.position.x));
            camera.position.z = Math.max(-45, Math.min(45, camera.position.z));
            camera.position.y = player.height;

            renderer.render(scene, camera);
        }

        animate();

        window.addEventListener('resize', () => {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        });
    </script>
</body>
</html>

Game Source: بازی شوتر اول شخص ساده

Creator: WildWave36

Libraries: three

Complexity: moderate (185 lines, 7.3 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: game-wildwave36" to link back to the original. Then publish at arcadelab.ai/publish.