make a html5 script that makes a 3d horror game
- User
make a html5 script that makes a 3d horror game
- Assistant
make a html5 script that makes a 3d horror game
Creating a fully-featured 3D horror game is a substantial project that typically involves using sophisticated game engines like Unity or Unreal Engine. However, you can start building a basic 3D environment for a horror game using HTML5 and JavaScript with the help of libraries such as Three.js. Below, I'll provide a simple example to help you get started.
Here's a simple example that sets up a 3D scene with basic controls, lighting, and an eerie environment suitable for a horror theme.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Horror Game Prototype</title>
<style>
body { margin: 0; overflow: hidden; background-color: #000; }
#info { position: absolute; top: 10px; width: 100%; text-align: center; color: #fff; z-index: 1; }
canvas { display: block; }
</style>
</head>
<body>
<div id="info">Use WASD to move, Mouse to look around</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128/examples/js/controls/PointerLockControls.js"></script>
<script>
// Scene, Camera, Renderer
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Pointer Lock Controls
const controls = new THREE.PointerLockControls(camera, renderer.domElement);
const info = document.getElementById('info');
info.addEventListener('click', () => {
controls.lock();
}, false);
controls.addEventListener('lock', () => {
info.style.display = 'none';
});
controls.addEventListener('unlock', () => {
info.style.display = 'block';
});
scene.add(controls.getObject());
// Movement Variables
const move = {
forward: false,
backward: false,
left: false,
right: false
};
const velocity = new THREE.Vector3();
const direction = new THREE.Vector3();
const speed = 5.0;
document.addEventListener('keydown', (event) => {
switch (event.code) {
case 'ArrowUp':
case 'KeyW':
move.forward = true;
break;
case 'ArrowLeft':
case 'KeyA':
move.left = true;
break;
case 'ArrowDown':
case 'KeyS':
move.backward = true;
break;
case 'ArrowRight':
case 'KeyD':
move.right = true;
break;
}
}, false);
document.addEventListener('keyup', (event) => {
switch (event.code) {
case 'ArrowUp':
case 'KeyW':
move.forward = false;
break;
case 'ArrowLeft':
case 'KeyA':
move.left = false;
break;
case 'ArrowDown':
case 'KeyS':
move.backward = false;
break;
case 'ArrowRight':
case 'KeyD':
move.right = false;
break;
}
}, false);
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040, 0.5); // soft white light
scene.add(ambientLight);
const pointLight = new THREE.PointLight(0xff0000, 1, 100);
pointLight.position.set(10, 10, 10);
scene.add(pointLight);
// Floor
const floorGeometry = new THREE.PlaneGeometry(200, 200, 10, 10);
const floorMaterial = new THREE.MeshStandardMaterial({ color: 0x222222 });
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
// Walls (placeholder)
const wallGeometry = new THREE.BoxGeometry(200, 10, 1);
const wallMaterial = new THREE.MeshStandardMaterial({ color: 0x555555 });
const wall1 = new THREE.Mesh(wallGeometry, wallMaterial);
wall1.position.set(0, 5, -100);
scene.add(wall1);
const wall2 = new THREE.Mesh(wallGeometry, wallMaterial);
wall2.position.set(0, 5, 100);
scene.add(wall2);
const wall3 = new THREE.Mesh(new THREE.BoxGeometry(1, 10, 200), wallMaterial);
wall3.position.set(-100, 5, 0);
scene.add(wall3);
const wall4 = new THREE.Mesh(new THREE.BoxGeometry(1, 10, 200), wallMaterial);
wall4.position.set(100, 5, 0);
scene.add(wall4);
// Scary Object (e.g., Ghost)
const ghostGeometry = new THREE.SphereGeometry(1, 32, 32);
const ghostMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00, emissive: 0x00ff00, transparent: true, opacity: 0.5 });
const ghost = new THREE.Mesh(ghostGeometry, ghostMaterial);
ghost.position.set(0, 1, -10);
scene.add(ghost);
// Animate Ghost Movement
let ghostDirection = 1;
function animateGhost(delta) {
ghost.position.z += delta * ghostDirection;
if (ghost.position.z > -5 || ghost.position.z < -15) {
ghostDirection *= -1;
}
}
// Handle Window Resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}, false);
// Animation Loop
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
// Update ghost
animateGhost(delta);
if (controls.isLocked === true) {
velocity.x -= velocity.x * 10.0 * delta;
velocity.z -= velocity.z * 10.0 * delta;
direction.z = Number(move.forward) - Number(move.backward);
direction.x = Number(move.right) - Number(move.left);
direction.normalize(); // this ensures consistent movements in all directions
if (move.forward || move.backward) velocity.z -= direction.z * speed * delta;
if (move.left || move.right) velocity.x -= direction.x * speed * delta;
controls.moveRight(-velocity.x * delta);
controls.moveForward(-velocity.z * delta);
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
Scene Setup: Initializes the Three.js scene, camera, and renderer. Sets the background to black to create a dark environment typical of horror games.
Controls: Uses PointerLockControls to allow first-person movement using the mouse and keyboard (WASD keys).
Lighting: Adds ambient and point lights to create an eerie atmosphere. You can experiment with different colors and intensities to enhance the horror effect.
Environment:
Scary Object (Ghost): Adds a semi-transparent glowing sphere that moves back and forth, simulating a ghost. You can replace this with more detailed models and animations.
Animation Loop: Continuously renders the scene and updates object positions. It also handles user input for movement.
To develop this into a more compelling horror game, consider the following enhancements:
Models and Textures: Use detailed 3D models and high-quality textures to create a more immersive environment.
Sound Effects: Add spooky sounds and music to enhance the atmosphere.
Lighting Effects: Incorporate dynamic lighting, shadows, and flickering lights to create tension.
Interactivity: Implement puzzles, triggers, and events that respond to player actions.
Storyline: Develop a compelling narrative to engage players emotionally.
Optimizations: Ensure the game runs smoothly across different devices by optimizing models, textures, and code.
Building a 3D horror game with HTML5 and JavaScript is an ambitious but achievable goal. Start small by creating simple scenes and gradually add complexity as you become more comfortable with the tools and concepts. Leveraging libraries like Three.js can significantly speed up development and allow you to focus on the creative aspects of your game.
Good luck with your game development journey! If you have specific questions or need further assistance, feel free to ask.
Not yet included in a published release.