<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Endless Forest Game</title>
<style>
canvas {
display: block;
margin: 0;
padding: 0;
background: black;
}
body {
margin: 0;
overflow: hidden;
}
</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;
// Player object
const player = {
x: 0,
y: 0,
size: 10,
speed: 2,
};
// Lantern fuel
let lanternFuel = 100;
const maxFuel = 100;
// Input keys
const keys = {};
// Event listeners for keys
window.addEventListener('keydown', function(e) {
keys[e.key] = true;
});
window.addEventListener('keyup', function(e) {
keys[e.key] = false;
});
// Update function
function update() {
// Move player
if (keys['ArrowUp']) player.y -= player.speed;
if (keys['ArrowDown']) player.y += player.speed;
if (keys['ArrowLeft']) player.x -= player.speed;
if (keys['ArrowRight']) player.x += player.speed;
// Decrease lantern fuel
lanternFuel -= 0.1;
if (lanternFuel <= 0) {
lanternFuel = 0;
// Game over
alert('Your lantern has run out of fuel. Game over!');
// Restart game
player.x = 0;
player.y = 0;
lanternFuel = maxFuel;
}
}
// Draw function
function draw() {
// Clear canvas
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Save context
ctx.save();
// Implement darkness and lantern light
ctx.globalCompositeOperation = 'destination-out';
ctx.beginPath();
const lightRadius = 100 + (lanternFuel / maxFuel) * 100;
ctx.arc(canvas.width / 2, canvas.height / 2, lightRadius, 0, Math.PI * 2);
ctx.fill();
// Restore context
ctx.restore();
// Draw player (at center of canvas)
ctx.fillStyle = 'white';
ctx.fillRect((canvas.width / 2) - (player.size / 2),
(canvas.height / 2) - (player.size / 2),
player.size, player.size);
// Draw surroundings (trees, animals, oil)
drawEnvironment();
}
// Function to draw environment
function drawEnvironment() {
// Determine visible area
const startX = player.x - canvas.width / 2;
const startY = player.y - canvas.height / 2;
const endX = player.x + canvas.width / 2;
const endY = player.y + canvas.height / 2;
// Grid size
const gridSize = 50;
// Loop through grid in visible area
for (let x = Math.floor(startX / gridSize) * gridSize;
x <= endX; x += gridSize) {
for (let y = Math.floor(startY / gridSize) * gridSize;
y <= endY; y += gridSize) {
// Determine if there's a tree at this position
if (isTree(x, y)) {
ctx.fillStyle = 'green';
const screenX = (x - player.x) + canvas.width / 2;
const screenY = (y - player.y) + canvas.height / 2;
ctx.fillRect(screenX, screenY, gridSize, gridSize);
}
// Determine if there's an oil at this position
if (isOil(x, y)) {
ctx.fillStyle = 'yellow';
const screenX = (x - player.x) + canvas.width / 2 + gridSize / 4;
const screenY = (y - player.y) + canvas.height / 2 + gridSize / 4;
ctx.fillRect(screenX, screenY, gridSize / 2, gridSize / 2);
// Check if player is on oil
if (Math.abs(player.x - x) < player.size &&
Math.abs(player.y - y) < player.size) {
lanternFuel = Math.min(lanternFuel + 50, maxFuel);
}
}
// Determine if there's an animal at this position
if (isAnimal(x, y)) {
ctx.fillStyle = 'red';
const screenX = (x - player.x) + canvas.width / 2 + gridSize / 4;
const screenY = (y - player.y) + canvas.height / 2 + gridSize / 4;
ctx.fillRect(screenX, screenY, gridSize / 2, gridSize / 2);
// Check if player is on animal
if (Math.abs(player.x - x) < player.size &&
Math.abs(player.y - y) < player.size) {
// Game over
alert('An animal has eaten you. Game over!');
// Restart game
player.x = 0;
player.y = 0;
lanternFuel = maxFuel;
}
}
}
}
}
// Functions to determine if there's a tree, oil, or animal at a position
function isTree(x, y) {
const val = noise(x, y);
return val > 0.6;
}
function isOil(x, y) {
const val = noise(x + 1000, y + 1000);
return val > 0.95;
}
function isAnimal(x, y) {
const val = noise(x - 1000, y - 1000);
return val > 0.9;
}
// Simple noise function
function noise(x, y) {
const s = Math.sin(x * 12.9898 + y * 78.233) * 43758.5453;
return s - Math.floor(s);
}
// Game loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Start game
gameLoop();
</script>
</body>
</html>