Stworzę dla Ciebie prostą grę Backrooms w HTML z wykorzystaniem Three.js do grafiki 3D. Oto kompletny kod:
html
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Backrooms 3D</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.5);
padding: 10px;
border-radius: 5px;
z-index: 100;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: red;
font-size: 48px;
display: none;
text-align: center;
background: rgba(0,0,0,0.8);
padding: 20px;
border-radius: 10px;
}
#minimap {
position: absolute;
bottom: 20px;
right: 20px;
width: 200px;
height: 200px;
background: rgba(0,0,0,0.7);
border: 2px solid #444;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="info">
<div>WASD - Poruszanie</div>
<div>Myszka - Rozglądanie</div>
<div>Shift - Bieg</div>
<div>Poziom: <span id="level">0</span></div>
<div>Wytrzymałość: <span id="stamina">100</span>%</div>
</div>
<div id="gameOver">
<div>Znalazłeś wyjście!</div>
<div style="font-size: 24px;">Poziom: <span id="finalLevel">0</span></div>
</div>
<canvas id="minimap"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Inicjalizacja sceny
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x000000, 1, 50);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1.6, 0);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// Oświetlenie
const ambientLight = new THREE.AmbientLight(0xffffcc, 0.2);
scene.add(ambientLight);
// Światło gracza
const playerLight = new THREE.PointLight(0xffffcc, 0.8, 10);
playerLight.position.copy(camera.position);
playerLight.castShadow = true;
scene.add(playerLight);
// Materiały
const wallMaterial = new THREE.MeshPhongMaterial({
color: 0xffff99,
emissive: 0x111100,
emissiveIntensity: 0.1
});
const floorMaterial = new THREE.MeshPhongMaterial({
color: 0x999966,
emissive: 0x111100,
emissiveIntensity: 0.05
});
const ceilingMaterial = new THREE.MeshPhongMaterial({
color: 0xcccc88,
emissive: 0x111100,
emissiveIntensity: 0.1
});
// Generator labiryntu
class MazeGenerator {
constructor(width, height) {
this.width = width;
this.height = height;
this.grid = [];
this.generate();
}
generate() {
// Inicjalizacja siatki
for (let y = 0; y < this.height; y++) {
this.grid[y] = [];
for (let x = 0; x < this.width; x++) {
this.grid[y][x] = 1; // 1 = ściana
}
}
// Generowanie ścieżek
this.carve(1, 1);
// Dodaj wyjście
this.grid[this.height - 2][this.width - 2] = 2; // 2 = wyjście
}
carve(x, y) {
this.grid[y][x] = 0; // 0 = korytarz
const directions = [[0, 2], [2, 0], [0, -2], [-2, 0]];
directions.sort(() => Math.random() - 0.5);
for (let [dx, dy] of directions) {
const nx = x + dx;
const ny = y + dy;
if (nx > 0 && nx < this.width - 1 && ny > 0 && ny < this.height - 1 && this.grid[ny][nx] === 1) {
this.grid[y + dy/2][x + dx/2] = 0;
this.carve(nx, ny);
}
}
}
}
// Tworzenie poziomu
let currentLevel = 0;
let maze;
const wallHeight = 3;
const cellSize = 4;
const walls = [];
let exitPosition;
function createLevel() {
// Usuń poprzednie ściany
walls.forEach(wall => scene.remove(wall));
walls.length = 0;
// Generuj nowy labirynt
const size = 15 + currentLevel * 2;
maze = new MazeGenerator(size, size);
// Twórz geometrię
for (let y = 0; y < maze.height; y++) {
for (let x = 0; x < maze.width; x++) {
if (maze.grid[y][x] === 1) {
// Ściana
const wallGeometry = new THREE.BoxGeometry(cellSize, wallHeight, cellSize);
const wall = new THREE.Mesh(wallGeometry, wallMaterial);
wall.position.set(x * cellSize, wallHeight/2, y * cellSize);
wall.castShadow = true;
wall.receiveShadow = true;
scene.add(wall);
walls.push(wall);
} else if (maze.grid[y][x] === 2) {
// Wyjście
exitPosition = new THREE.Vector3(x * cellSize, 0, y * cellSize);
const exitLight = new THREE.PointLight(0x00ff00, 2, 10);
exitLight.position.set(x * cellSize, 2, y * cellSize);
scene.add(exitLight);
walls.push(exitLight);
}
}
}
// Podłoga
const floorGeometry = new THREE.PlaneGeometry(maze.width * cellSize, maze.height * cellSize);
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2;
floor.position.set(maze.width * cellSize / 2, 0, maze.height * cellSize / 2);
floor.receiveShadow = true;
scene.add(floor);
walls.push(floor);
// Sufit
const ceilingGeometry = new THREE.PlaneGeometry(maze.width * cellSize, maze.height * cellSize);
const ceiling = new THREE.Mesh(ceilingGeometry, ceilingMaterial);
ceiling.rotation.x = Math.PI / 2;
ceiling.position.set(maze.width * cellSize / 2, wallHeight, maze.height * cellSize / 2);
scene.add(ceiling);
walls.push(ceiling);
// Resetuj pozycję gracza
camera.position.set(cellSize, 1.6, cellSize);
camera.rotation.set(0, 0, 0);
}
// Kontrola gracza
const keys = {};
let mouseX = 0;
let mouseY = 0;
let stamina = 100;
let isRunning = false;
document.addEventListener('keydown', (e) => keys[e.key.toLowerCase()] = true);
document.addEventListener('keyup', (e) => keys[e.key.toLowerCase()] = false);
document.addEventListener('mousemove', (e) => {
if (document.pointerLockElement === renderer.domElement) {
mouseX -= e.movementX * 0.002;
mouseY -= e.movementY * 0.002;
mouseY = Math.max(-Math.PI/2, Math.min(Math.PI/2, mouseY));
}
});
renderer.domElement.addEventListener('click', () => {
renderer.domElement.requestPointerLock();
});
// Kolizje
function checkCollision(position) {
const margin = 0.5;
const gridX = Math.floor(position.x / cellSize);
const gridZ = Math.floor(position.z / cellSize);
if (gridX < 0 || gridX >= maze.width || gridZ < 0 || gridZ >= maze.height) {
return true;
}
// Sprawdź okoliczne komórki
for (let dx = -1; dx <= 1; dx++) {
for (let dz = -1; dz <= 1; dz++) {
const checkX = gridX + dx;
const checkZ = gridZ + dz;
if (checkX >= 0 && checkX < maze.width && checkZ >= 0 && checkZ < maze.height) {
if (maze.grid[checkZ][checkX] === 1) {
const wallCenterX = checkX * cellSize + cellSize/2;
const wallCenterZ = checkZ * cellSize + cellSize/2;
if (Math.abs(position.x - wallCenterX) < cellSize/2 + margin &&
Math.abs(position.z - wallCenterZ) < cellSize/2 + margin) {
return true;
}
}
}
}
}
return false;
}
// Minimapa
const minimapCanvas = document.getElementById('minimap');
const minimapCtx = minimapCanvas.getContext('2d');
minimapCanvas.width = 200;
minimapCanvas.height = 200;
function drawMinimap() {
minimapCtx.fillStyle = '#000';
minimapCtx.fillRect(0, 0, 200, 200);
const scale = Math.min(200 / maze.width, 200 / maze.height);
const offsetX = (200 - maze.width * scale) / 2;
const offsetY = (200 - maze.height * scale) / 2;
// Rysuj labirynt
for (let y = 0; y < maze.height; y++) {
for (let x = 0; x < maze.width; x++) {
if (maze.grid[y][x] === 0) {
minimapCtx.fillStyle = '#333';
minimapCtx.fillRect(offsetX + x * scale, offsetY + y * scale, scale, scale);
} else if (maze.grid[y][x] === 2) {
minimapCtx.fillStyle = '#0f0';
minimapCtx.fillRect(offsetX + x * scale, offsetY + y * scale, scale, scale);
}
}
}
// Pozycja gracza
const playerX = camera.position.x / cellSize;
const playerZ = camera.position.z / cellSize;
minimapCtx.fillStyle = '#f00';
minimapCtx.beginPath();
minimapCtx.arc(offsetX + playerX * scale, offsetY + playerZ * scale, 3, 0, Math.PI * 2);
minimapCtx.fill();
}
// Główna pętla gry
function animate() {
requestAnimationFrame(animate);
// Aktualizacja rotacji kamery
camera.rotation.y = mouseX;
camera.rotation.x = mouseY;
// Ruch gracza
const moveSpeed = (isRunning && stamina > 0) ? 0.15 : 0.08;
const direction = new THREE.Vector3();
if (keys['w']) direction.z -= 1;
if (keys['s']) direction.z += 1;
if (keys['a']) direction.x -= 1;
if (keys['d']) direction.x += 1;
isRunning = keys['shift'];
if (direction.length() > 0) {
direction.normalize();
direction.applyQuaternion(camera.quaternion);
direction.y = 0;
direction.normalize();
const newPosition = camera.position.clone();
newPosition.add(direction.multiplyScalar(moveSpeed));
if (!checkCollision(newPosition)) {
camera.position.copy(newPosition);
}
// Aktualizacja staminy
if (isRunning && stamina > 0) {
stamina -= 0.5;
} else if (!isRunning && stamina < 100) {
stamina += 0.3;
}
stamina = Math.max(0, Math.min(100, stamina));
} else if (stamina < 100) {
stamina += 0.5;
stamina = Math.min(100, stamina);
}
// Aktualizacja światła gracza
playerLight.position.copy(camera.position);
// Sprawdź czy gracz dotarł do wyjścia
if (exitPosition && camera.position.distanceTo(exitPosition) < 2) {
currentLevel++;
document.getElementById('level').textContent = currentLevel;
if (currentLevel >= 5) {
document.getElementById('finalLevel').textContent = currentLevel;
document.getElementById('gameOver').style.display = 'block';
document.exitPointerLock();
} else {
createLevel();
}
}
// Aktualizacja UI
document.getElementById('stamina').textContent = Math.floor(stamina);
// Rysuj minimapę
drawMinimap();
renderer.render(scene, camera);
}
// Obsługa zmiany rozmiaru okna
window.addEventListener('resize', ()