🎮ArcadeLab

<script> /* ===== نظام السيارات 3D ===== */ let cars = [];

by LunarGamer64
209 lines4.0 KB🛠️ Three.js (3D graphics)
▶ Play
<script>
/* ===== نظام السيارات 3D ===== */

let cars = [];
let currentCar = null;
let driving = false;

function createCar(x, z) {
  const car = new THREE.Group();

  // جسم السيارة
  const body = new THREE.Mesh(
    new THREE.BoxGeometry(2.2, 0.65, 4),
    new THREE.MeshStandardMaterial({color: 0x1976d2})
  );
  body.position.y = 0.65;
  car.add(body);

  // سقف السيارة
  const roof = new THREE.Mesh(
    new THREE.BoxGeometry(1.7, 0.55, 1.9),
    new THREE.MeshStandardMaterial({color: 0x222222})
  );
  roof.position.y = 1.15;
  car.add(roof);

  // العجلات
  const wheelGeo = new THREE.CylinderGeometry(
    0.42, 0.42, 0.28, 16
  );
  const wheelMat = new THREE.MeshStandardMaterial({
    color: 0x111111
  });

  const wheelPositions = [
    [-1.05,0.45,-1.35],
    [ 1.05,0.45,-1.35],
    [-1.05,0.45, 1.35],
    [ 1.05,0.45, 1.35]
  ];

  wheelPositions.forEach(p => {
    const wheel = new THREE.Mesh(wheelGeo, wheelMat);
    wheel.rotation.z = Math.PI / 2;
    wheel.position.set(p[0],p[1],p[2]);
    car.add(wheel);
  });

  car.position.set(x,0,z);

  car.userData.speed = 0;
  car.userData.maxSpeed = 0.35;
  car.userData.rotationSpeed = 0.035;

  scene.add(car);
  cars.push(car);
}

// سيارات في الخريطة
createCar(8,8);
createCar(-15,20);
createCar(35,-20);
createCar(-40,-35);

/* زر الدخول */
const enterButton = document.createElement("button");
enterButton.innerText = "🚗 ركوب السيارة";
enterButton.style.position = "fixed";
enterButton.style.bottom = "240px";
enterButton.style.right = "25px";
enterButton.style.zIndex = "10";
enterButton.style.display = "none";
document.body.appendChild(enterButton);

/* أزرار السيارة */
const carControls = document.createElement("div");

carControls.innerHTML = `
<button id="gas">⬆️</button>
<button id="brake">⬇️</button>
<button id="left">⬅️</button>
<button id="right">➡️</button>
`;

carControls.style.position = "fixed";
carControls.style.bottom = "20px";
carControls.style.right = "120px";
carControls.style.zIndex = "10";
carControls.style.display = "none";

document.body.appendChild(carControls);

const gas = document.getElementById("gas");
const brake = document.getElementById("brake");
const left = document.getElementById("left");
const right = document.getElementById("right");

let carKeys = {
  gas:false,
  brake:false,
  left:false,
  right:false
};

function hold(button,key){

  button.addEventListener("touchstart",e=>{
    e.preventDefault();
    carKeys[key]=true;
  });

  button.addEventListener("touchend",e=>{
    e.preventDefault();
    carKeys[key]=false;
  });

  button.addEventListener("mousedown",()=>{
    carKeys[key]=true;
  });

  button.addEventListener("mouseup",()=>{
    carKeys[key]=false;
  });
}

hold(gas,"gas");
hold(brake,"brake");
hold(left,"left");
hold(right,"right");

/* البحث عن أقرب سيارة */
function findNearestCar(){

  let nearest = null;
  let distance = Infinity;

  cars.forEach(car=>{

    const d = player.position.distanceTo(car.position);

    if(d < distance){
      distance = d;
      nearest = car;
    }

  });

  if(distance < 5){
    return nearest;
  }

  return null;
}

/* ركوب السيارة */
enterButton.onclick = () => {

  if(!driving){

    const car = findNearestCar();

    if(!car) return;

    currentCar = car;
    driving = true;

    player.visible = false;

    enterButton.innerText = "🚪 النزول";

    carControls.style.display = "block";

  }else{

    driving = false;

    player.visible = true;

    player.position.copy(currentCar.position);
    player.position.x += 3;

    currentCar.userData.speed = 0;

    currentCar = null;

    enterButton.innerText = "🚗 ركوب السيارة";

    carControls.style.display = "none";
  }
};

/* تحديث السيارة */
function updateCar(){

  if(!driving || !currentCar) return;

  const car = currentCar;

  if(carKeys.gas){
    car.userData.speed += 0.015;
  }

  if(carKeys.brake){
    car.userData.speed -= 0.025;
  }

  car.userData.speed *= 0.96;

  car.userData.speed =

Game Source: <script> /* ===== نظام السيارات 3D ===== */ let cars = [];

Creator: LunarGamer64

Libraries: three

Complexity: complex (209 lines, 4.0 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: script-3d-let-cars-lunargamer64" to link back to the original. Then publish at arcadelab.ai/publish.