Write a 3D shooter in HTML CSS and JS, write a full working code
- User
Write a 3D shooter in HTML CSS and JS, write a full working code
- Assistant
Write a 3D shooter in HTML CSS and JS, write a full working code
Creating a complete 3D shooter game is a complex task that typically involves advanced concepts in 3D graphics, physics, and game design. However, I can provide you with a simplified version using Three.js, a popular JavaScript library for 3D graphics, along with HTML and CSS. This example will demonstrate a basic first-person shooter where you can move around and shoot projectiles.
For simplicity, we'll keep everything in a single HTML file.
Here's a complete example. You can copy this code into an index.html file and open it in your browser to see the basic shooter in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple 3D Shooter</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
#instructions {
position: absolute;
top: 50%;
width: 100%;
text-align: center;
color: white;
font-family: Arial, sans-serif;
user-select: none;
cursor: pointer;
z-index: 1;
}
</style>
</head>
<body>
<div id="instructions">Click to play</div>
<!-- Include Three.js from CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r136/three.min.js"></script>
<script>
// Basic Setup
let scene, camera, renderer;
let controlsEnabled = false;
let moveForward = false, moveBackward = false, moveLeft = false, moveRight = false;
let velocity = new THREE.Vector3();
const objects = [];
const bullets = [];
const bulletSpeed = 1.5;
const clock = new THREE.Clock();
// Initialize Scene
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x222222);
camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.y = 1.6; // Typical eye level height
// Light
const light = new THREE.HemisphereLight(0xffffff, 0x444444);
light.position.set(0, 20, 0);
scene.add(light);
// Floor
const floorGeometry = new THREE.PlaneGeometry(100, 100);
const floorMaterial = new THREE.MeshStandardMaterial({ color: 0x555555, depthWrite: false });
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
// Simple Target
const boxGeometry = new THREE.BoxGeometry(1,1,1);
const boxMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
for(let i=0;i<10;i++) {
const box = new THREE.Mesh(boxGeometry, boxMaterial);
box.position.set(Math.random()*20-10, 0.5, Math.random()*20-10);
scene.add(box);
objects.push(box);
}
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Pointer Lock for controls
const instructions = document.getElementById('instructions');
instructions.addEventListener('click', function () {
renderer.domElement.requestPointerLock();
}, false);
document.addEventListener('pointerlockchange', onPointerLockChange, false);
document.addEventListener('pointerlockerror', function () {
instructions.style.display = '';
}, false);
// Movement Controls
const onKeyDown = function (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;
}
};
const onKeyUp = function (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;
}
};
document.addEventListener('keydown', onKeyDown, false);
document.addEventListener('keyup', onKeyUp, false);
// Shooting
document.addEventListener('mousedown', onMouseDown, false);
window.addEventListener('resize', onWindowResize, false);
}
function onPointerLockChange() {
if (document.pointerLockElement === renderer.domElement) {
controlsEnabled = true;
document.getElementById('instructions').style.display = 'none';
document.addEventListener('mousemove', onMouseMove, false);
} else {
controlsEnabled = false;
document.getElementById('instructions').style.display = '';
document.removeEventListener('mousemove', onMouseMove, false);
}
}
let pitch = 0, yaw = 0;
function onMouseMove(event) {
const movementX = event.movementX || 0;
const movementY = event.movementY || 0;
yaw -= movementX * 0.002;
pitch -= movementY * 0.002;
pitch = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, pitch));
camera.rotation.x = pitch;
camera.rotation.y = yaw;
}
function onMouseDown(event) {
if (!controlsEnabled) return;
shoot();
}
function shoot() {
const bulletGeometry = new THREE.SphereGeometry(0.05, 8, 8);
const bulletMaterial = new THREE.MeshStandardMaterial({ color: 0xffff00 });
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
const direction = new THREE.Vector3();
camera.getWorldDirection(direction);
bullet.position.copy(camera.position).add(direction.clone().multiplyScalar(1));
bullet.velocity = direction.multiplyScalar(bulletSpeed);
scene.add(bullet);
bullets.push(bullet);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
const speed = 5;
if (controlsEnabled) {
// Movement
velocity.x -= velocity.x * 10.0 * delta;
velocity.z -= velocity.z * 10.0 * delta;
if (moveForward) velocity.z -= speed * delta;
if (moveBackward) velocity.z += speed * delta;
if (moveLeft) velocity.x -= speed * delta;
if (moveRight) velocity.x += speed * delta;
camera.position.x += velocity.x;
camera.position.z += velocity.z;
}
// Update bullets
for(let i=bullets.length -1; i>=0; i--){
const bullet = bullets[i];
bullet.position.add(bullet.velocity.clone().multiplyScalar(delta * 60));
// Check collision with targets
for(let j=0; j < objects.length; j++) {
const obj = objects[j];
if (bullet.position.distanceTo(obj.position) < 0.5) {
scene.remove(obj);
objects.splice(j,1);
scene.remove(bullet);
bullets.splice(i,1);
break;
}
}
// Remove bullets that are too far
if (bullet.position.length() > 100) {
scene.remove(bullet);
bullets.splice(i,1);
}
}
renderer.render(scene, camera);
}
init();
animate();
</script>
</body>
</html>
HTML Structure:
<div> with the id instructions prompts the user to click to play and lock the pointer for first-person controls.CSS Styling:
JavaScript Functionality:
W, A, S, D or arrow keys) to move the camera forward, backward, left, and right.index.html.index.html in your preferred web browser.W, A, S, D or arrow keys to move around.This is a very basic shooter. Here are some ideas to enhance it:
This should give you a solid starting point to build and expand upon a simple 3D shooter game using HTML, CSS, and JavaScript with Three.js. Happy coding!
Not yet included in a published release.