o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
instruction: use html and js make a simple 3d fps game
- User
instruction: use html and js make a simple 3d fps game
- Assistant
instruction: use html and js make a simple 3d fps game
Certainly! Let's create a simple 3D first-person shooter (FPS) game using HTML and JavaScript. We'll use the Three.js library, which simplifies working with 3D graphics in the browser.
Below is a step-by-step guide to set up your game.
Create an index.html file with the basic HTML structure and include the Three.js library.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple 3D FPS Game</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<!-- Include Three.js Library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/three.min.js"></script>
<!-- Include PointerLockControls -->
<script src="https://threejs.org/examples/jsm/controls/PointerLockControls.js"></script>
<!-- Game Script -->
<script src="game.js"></script>
</body>
</html>
Create a game.js file where we'll write the game logic.
// Initialize scene, camera, and renderer
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb); // Sky blue background
const camera = new THREE.PerspectiveCamera(
75, window.innerWidth / window.innerHeight, 1, 1000
);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add lighting
const light = new THREE.HemisphereLight(0xffffff, 0x444444);
light.position.set(0, 200, 0);
scene.add(light);
// Add ground
const floorGeometry = new THREE.PlaneGeometry(2000, 2000);
const floorMaterial = new THREE.MeshPhongMaterial({ color: 0x228B22 });
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
// Add some cubes (targets)
const cubeGeometry = new THREE.BoxGeometry(20, 20, 20);
const cubeMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000 });
for (let i = 0; i < 10; i++) {
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.set(
Math.random() * 800 - 400,
10,
Math.random() * 800 - 400
);
scene.add(cube);
}
// Pointer Lock Controls
const controls = new THREE.PointerLockControls(camera, document.body);
document.addEventListener('click', () => {
controls.lock();
});
controls.addEventListener('lock', () => {
console.log('Pointer locked');
});
controls.addEventListener('unlock', () => {
console.log('Pointer unlocked');
});
// Movement variables
let moveForward = false;
let moveBackward = false;
let moveLeft = false;
let moveRight = false;
let canJump = false;
const velocity = new THREE.Vector3();
const direction = new THREE.Vector3();
document.addEventListener('keydown', (event) => {
switch (event.code) {
case 'ArrowUp':
case 'KeyW':
moveForward = true;
break;
case 'ArrowLeft':
case 'KeyA':
moveLeft = true;
break;
case 'ArrowDown':
case 'KeyS':
moveBackward = true;
break;
case 'ArrowRight':
case 'KeyD':
moveRight = true;
break;
case 'Space':
if (canJump) velocity.y += 350;
canJump = false;
break;
}
});
document.addEventListener('keyup', (event) => {
switch (event.code) {
case 'ArrowUp':
case 'KeyW':
moveForward = false;
break;
case 'ArrowLeft':
case 'KeyA':
moveLeft = false;
break;
case 'ArrowDown':
case 'KeyS':
moveBackward = false;
break;
case 'ArrowRight':
case 'KeyD':
moveRight = false;
break;
}
});
// Shooting mechanic
document.addEventListener('mousedown', shoot);
function shoot() {
if (controls.isLocked) {
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(new THREE.Vector2(0, 0), camera);
const intersects = raycaster.intersectObjects(scene.children, false);
if (intersects.length > 0) {
const intersected = intersects[0].object;
if (intersected !== floor) {
scene.remove(intersected);
}
}
}
}
// Animate the scene
let prevTime = performance.now();
function animate() {
requestAnimationFrame(animate);
if (controls.isLocked) {
const time = performance.now();
const delta = (time - prevTime) / 1000;
velocity.x -= velocity.x * 10.0 * delta;
velocity.z -= velocity.z * 10.0 * delta;
velocity.y -= 9.8 * 100.0 * delta; // gravity
direction.z = Number(moveForward) - Number(moveBackward);
direction.x = Number(moveRight) - Number(moveLeft);
direction.normalize();
if (moveForward || moveBackward)
velocity.z -= direction.z * 400.0 * delta;
if (moveLeft || moveRight)
velocity.x -= direction.x * 400.0 * delta;
controls.moveRight(-velocity.x * delta);
controls.moveForward(-velocity.z * delta);
controls.getObject().position.y += velocity.y * delta; // new position
if (controls.getObject().position.y < 10) {
velocity.y = 0;
controls.getObject().position.y = 10;
canJump = true;
}
prevTime = time;
}
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', onWindowResize);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
index.html and game.js in the same directory.index.html in a modern web browser (Chrome or Firefox recommended).By following these steps, you've created a simple 3D FPS game using HTML and JavaScript. Experiment with the code and explore additional features to enhance your game further. Happy coding!
Not yet included in a published release.