create a fully working doom game
- User
create a fully working doom 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.
create a fully working doom game
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple Doom-like game using JavaScript and HTML5 Canvas:
<!DOCTYPE html>
<html>
<head>
<title>Mini Doom</title>
<style>
body { margin: 0; overflow: hidden; background-color: #000; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Set up canvas
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Game constants
const FOV = Math.PI / 3;
const WALL_HEIGHT = 1;
const PLAYER_HEIGHT = 0.5;
const MOVE_SPEED = 0.05;
const TURN_SPEED = 0.05;
const RAY_COUNT = canvas.width;
const MAX_DEPTH = 20;
// Map (1 = wall, 0 = empty space)
const MAP = [
[1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,1,1,1,0,1],
[1,0,1,0,0,0,0,1,0,1],
[1,0,1,0,0,0,0,1,0,1],
[1,0,1,1,1,0,0,0,0,1],
[1,0,0,0,1,0,0,0,0,1],
[1,0,0,0,1,0,1,0,0,1],
[1,0,0,0,0,0,1,0,0,1],
[1,1,1,1,1,1,1,1,1,1]
];
// Player state
const player = {
x: 1.5,
y: 1.5,
angle: Math.PI / 4,
moving: {
forward: false,
backward: false,
left: false,
right: false
}
};
// Colors
const COLORS = {
floor: '#444',
ceiling: '#222',
wall: {
north: '#8B0000',
south: '#A52A2A',
east: '#B22222',
west: '#CD5C5C'
}
};
// Input handling
window.addEventListener('keydown', (e) => {
switch(e.key) {
case 'w': player.moving.forward = true; break;
case 's': player.moving.backward = true; break;
case 'a': player.moving.left = true; break;
case 'd': player.moving.right = true; break;
case 'ArrowLeft': player.angle -= TURN_SPEED; break;
case 'ArrowRight': player.angle += TURN_SPEED; break;
}
});
window.addEventListener('keyup', (e) => {
switch(e.key) {
case 'w': player.moving.forward = false; break;
case 's': player.moving.backward = false; break;
case 'a': player.moving.left = false; break;
case 'd': player.moving.right = false; break;
}
});
// Cast a single ray and return distance to wall
function castRay(angle) {
const sin = Math.sin(angle);
const cos = Math.cos(angle);
// Check horizontal intersections
let horzDist = MAX_DEPTH;
let horzWallType = null;
if (sin !== 0) {
const invSin = 1 / sin;
const firstY = sin > 0 ? Math.floor(player.y) + 1 : Math.ceil(player.y - 1);
const firstX = player.x + (firstY - player.y) / Math.tan(angle);
const dy = sin > 0 ? 1 : -1;
const dx = dy / Math.tan(angle);
let x = firstX;
let y = firstY;
for (let i = 0; i < MAX_DEPTH; i++) {
const mapX = Math.floor(x);
const mapY = Math.floor(y - (sin <= 0 ? 0.001 : 0));
if (mapX < 0 || mapX >= MAP[0].length || mapY < 0 || mapY >= MAP.length) break;
if (MAP[mapY][mapX] === 1) {
const dist = (y - player.y) * invSin;
horzDist = dist;
horzWallType = sin > 0 ? 'north' : 'south';
break;
}
x += dx;
y += dy;
}
}
// Check vertical intersections
let vertDist = MAX_DEPTH;
let vertWallType = null;
if (cos !== 0) {
const invCos = 1 / cos;
const firstX = cos > 0 ? Math.floor(player.x) + 1 : Math.ceil(player.x - 1);
const firstY = player.y + (firstX - player.x) * Math.tan(angle);
const dx = cos > 0 ? 1 : -1;
const dy = dx * Math.tan(angle);
let x = firstX;
let y = firstY;
for (let i = 0; i < MAX_DEPTH; i++) {
const mapX = Math.floor(x - (cos <= 0 ? 0.001 : 0));
const mapY = Math.floor(y);
if (mapX < 0 || mapX >= MAP[0].length || mapY < 0 || mapY >= MAP.length) break;
if (MAP[mapY][mapX] === 1) {
const dist = (x - player.x) * invCos;
vertDist = dist;
vertWallType = cos > 0 ? 'west' : 'east';
break;
}
x += dx;
y += dy;
}
}
// Return the closest intersection
if (vertDist < horzDist) {
return { distance: vertDist, wallType: vertWallType };
} else {
return { distance: horzDist, wallType: horzWallType };
}
}
// Update player position based on input
function updatePlayer() {
const moveX = Math.cos(player.angle) * MOVE_SPEED;
const moveY = Math.sin(player.angle) * MOVE_SPEED;
const strafeX = Math.cos(player.angle + Math.PI/2) * MOVE_SPEED;
const strafeY = Math.sin(player.angle + Math.PI/2) * MOVE_SPEED;
let newX = player.x;
let newY = player.y;
if (player.moving.forward) {
newX += moveX;
newY += moveY;
}
if (player.moving.backward) {
newX -= moveX;
newY -= moveY;
}
if (player.moving.left) {
newX += strafeX;
newY += strafeY;
}
if (player.moving.right) {
newX -= strafeX;
newY -= strafeY;
}
// Collision detection
const checkX = Math.floor(newX);
const checkY = Math.floor(newY);
if (MAP[checkY] && MAP[checkY][checkX] === 0) {
player.x = newX;
player.y = newY;
}
}
// Render the scene
function render() {
// Clear the canvas
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw ceiling and floor
ctx.fillStyle = COLORS.ceiling;
ctx.fillRect(0, 0, canvas.width, canvas.height / 2);
ctx.fillStyle = COLORS.floor;
ctx.fillRect(0, canvas.height / 2, canvas.width, canvas.height / 2);
// Cast rays and draw walls
const angleStep = FOV / RAY_COUNT;
const halfFOV = FOV / 2;
for (let i = 0; i < RAY_COUNT; i++) {
const rayAngle = player.angle - halfFOV + i * angleStep;
const { distance, wallType } = castRay(rayAngle);
// Fix fisheye effect
const correctedDist = distance * Math.cos(player.angle - rayAngle);
// Calculate wall height
const wallHeight = (WALL_HEIGHT / correctedDist) * canvas.height;
// Draw wall slice
const wallTop = (canvas.height - wallHeight) / 2;
ctx.fillStyle = COLORS.wall[wallType];
ctx.fillRect(i, wallTop, 1, wallHeight);
// Add shading based on distance
const shade = 1 - Math.min(distance / MAX_DEPTH, 0.8);
ctx.fillStyle = `rgba(0,0,0,${1-shade})`;
ctx.fillRect(i, wallTop, 1, wallHeight);
}
}
// Game loop
function gameLoop() {
updatePlayer();
render();
requestAnimationFrame(gameLoop);
}
// Start the game
gameLoop();
// Handle window resize
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
</script>
</body>
</html>
Would you like me to explain how this code works?