write me a working 3d game
- User
write me a working 3d game
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write me a working 3d game
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll create a simple 3D game using HTML5 and Three.js - a first-person maze game where you navigate through corridors to reach a goal. Here's a complete working game:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Maze Game</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: rgba(0,0,0,0.7);
padding: 10px;
border-radius: 5px;
z-index: 100;
}
#winMessage {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
background: rgba(0,200,0,0.9);
padding: 20px;
border-radius: 10px;
font-size: 24px;
display: none;
z-index: 100;
}
</style>
</head>
<body>
<div id="info">
Use WASD to move, Mouse to look around<br>
Find the golden cube to win!
</div>
<div id="winMessage">You Win! Refresh to play again.</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Scene setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
scene.fog = new THREE.Fog(0x87CEEB, 10, 100);
// Camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1.6, 0);
// Renderer
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 5);
directionalLight.castShadow = true;
directionalLight.shadow.camera.near = 0.1;
directionalLight.shadow.camera.far = 50;
directionalLight.shadow.camera.left = -20;
directionalLight.shadow.camera.right = 20;
directionalLight.shadow.camera.top = 20;
directionalLight.shadow.camera.bottom = -20;
scene.add(directionalLight);
// Materials
const wallMaterial = new THREE.MeshPhongMaterial({ color: 0x8B4513 });
const floorMaterial = new THREE.MeshPhongMaterial({ color: 0x228B22 });
const goalMaterial = new THREE.MeshPhongMaterial({
color: 0xFFD700,
emissive: 0xFFD700,
emissiveIntensity: 0.3
});
// Create maze
const mazeLayout = [
[1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,1,0,0,0,0,1],
[1,0,1,0,1,0,1,1,0,1],
[1,0,1,0,0,0,0,1,0,1],
[1,0,1,1,1,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,1,1,0,1,1,1,1,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,1,1,1,1,1,1,2,1],
[1,1,1,1,1,1,1,1,1,1]
];
const walls = [];
const cellSize = 4;
// Create floor
const floorGeometry = new THREE.BoxGeometry(mazeLayout[0].length * cellSize, 0.1, mazeLayout.length * cellSize);
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.position.y = -0.05;
floor.receiveShadow = true;
scene.add(floor);
// Create walls and goal
let goalPosition = null;
for (let z = 0; z < mazeLayout.length; z++) {
for (let x = 0; x < mazeLayout[z].length; x++) {
if (mazeLayout[z][x] === 1) {
const wallGeometry = new THREE.BoxGeometry(cellSize, cellSize, cellSize);
const wall = new THREE.Mesh(wallGeometry, wallMaterial);
wall.position.set(
(x - mazeLayout[0].length / 2) * cellSize + cellSize / 2,
cellSize / 2,
(z - mazeLayout.length / 2) * cellSize + cellSize / 2
);
wall.castShadow = true;
wall.receiveShadow = true;
walls.push(wall);
scene.add(wall);
} else if (mazeLayout[z][x] === 2) {
// Create goal
const goalGeometry = new THREE.BoxGeometry(1, 1, 1);
const goal = new THREE.Mesh(goalGeometry, goalMaterial);
goalPosition = new THREE.Vector3(
(x - mazeLayout[0].length / 2) * cellSize + cellSize / 2,
1,
(z - mazeLayout.length / 2) * cellSize + cellSize / 2
);
goal.position.copy(goalPosition);
goal.castShadow = true;
scene.add(goal);
}
}
}
// Player movement
const moveSpeed = 0.1;
const keys = {};
let mouseX = 0;
let mouseY = 0;
let yaw = 0;
let pitch = 0;
let isPointerLocked = false;
let gameWon = false;
// Keyboard controls
document.addEventListener('keydown', (e) => {
keys[e.key.toLowerCase()] = true;
});
document.addEventListener('keyup', (e) => {
keys[e.key.toLowerCase()] = false;
});
// Mouse controls
document.addEventListener('click', () => {
if (!isPointerLocked) {
renderer.domElement.requestPointerLock();
}
});
document.addEventListener('pointerlockchange', () => {
isPointerLocked = document.pointerLockElement === renderer.domElement;
});
document.addEventListener('mousemove', (e) => {
if (isPointerLocked) {
mouseX = e.movementX;
mouseY = e.movementY;
}
});
// Collision detection
function checkCollision(position) {
const playerRadius = 0.5;
for (let wall of walls) {
const wallBox = new THREE.Box3().setFromObject(wall);
const playerSphere = new THREE.Sphere(position, playerRadius);
if (wallBox.intersectsSphere(playerSphere)) {
return true;
}
}
return false;
}
// Check win condition
function checkWin() {
if (goalPosition && camera.position.distanceTo(goalPosition) < 2) {
gameWon = true;
document.getElementById('winMessage').style.display = 'block';
document.exitPointerLock();
}
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
if (!gameWon) {
// Update camera rotation
if (isPointerLocked) {
yaw -= mouseX * 0.002;
pitch -= mouseY * 0.002;
pitch = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, pitch));
camera.rotation.order = 'YXZ';
camera.rotation.y = yaw;
camera.rotation.x = pitch;
mouseX = 0;
mouseY = 0;
}
// Movement
const forward = new THREE.Vector3();
const right = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
right.crossVectors(forward, new THREE.Vector3(0, 1, 0));
const newPosition = camera.position.clone();
if (keys['w']) {
newPosition.add(forward.multiplyScalar(moveSpeed));
}
if (keys['s']) {
newPosition.sub(forward.multiplyScalar(moveSpeed));
}
if (keys['a']) {
newPosition.sub(right.multiplyScalar(moveSpeed));
}
if (keys['d']) {
newPosition.add(right.multiplyScalar(moveSpeed));
}
// Check collision before moving
if (!checkCollision(newPosition)) {
camera.position.copy(newPosition);
}
checkWin();
}
renderer.render(scene, camera);
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Start game
animate();
</script>
</body>
</html>
This is a fully functional 3D maze game that includes:
Features:
How to play:
The game uses Three.js loaded from a CDN, so you need an internet connection for it to work. The maze layout can be easily modified by changing the numbers in the mazeLayout array (0 = empty space, 1 = wall, 2 = goal position).