🎮ArcadeLab

Forest Friends 2D: The Night Deer

by LunarFlare59
538 lines16.3 KB
▶ Play

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Forest Friends 2D</title>
<style>
*{box-sizing:border-box}
body{margin:0;background:#b8e6a2;font-family:Arial,sans-serif;color:#24452c;overflow:hidden;touch-action:none}
header{height:54px;background:#347b43;color:white;display:flex;align-items:center;justify-content:space-around;gap:5px;padding:5px}
h1{font-size:clamp(15px,4vw,23px);margin:0}
.stats{display:flex;gap:5px;flex-wrap:wrap}
.stat{background:white;color:#24512f;border-radius:9px;padding:6px;font-size:13px;font-weight:bold}
#game{display:block;width:100%;height:calc(100dvh - 160px);background:#83c95f;touch-action:none}
.controls{height:106px;background:#d9edc8;display:flex;justify-content:space-around;align-items:center;gap:5px;padding:5px;flex-wrap:wrap}
.pad{display:grid;grid-template-columns:repeat(3,40px);grid-template-rows:repeat(2,38px);gap:3px}
button{border:0;border-radius:11px;background:#398c4d;color:white;font-weight:bold;font-size:14px;padding:8px;box-shadow:0 3px #246238;touch-action:none}
button:active{transform:translateY(2px);box-shadow:0 1px #246238}
.pad button{font-size:20px;padding:2px}
.action{background:#e89a38;box-shadow:0 3px #a96120;min-width:59px}
.blue{background:#3687bb;box-shadow:0 3px #205a80}
.pink{background:#ce7296;box-shadow:0 3px #904666}
#message{position:absolute;top:61px;left:50%;transform:translateX(-50%);background:#fff8d9;border:2px solid #dfbd65;border-radius:12px;padding:8px 12px;font-size:13px;font-weight:bold;width:max-content;max-width:92%;text-align:center;pointer-events:none;z-index:2}
#start{position:fixed;inset:0;background:#245b39dd;z-index:5;display:flex;align-items:center;justify-content:center;padding:20px}
.panel{background:#fff5d7;border:5px solid #e7bb58;border-radius:24px;padding:22px;max-width:430px;text-align:center}
.panel h2{font-size:29px}
.panel p{font-size:16px;line-height:1.5}
.panel button{font-size:19px;padding:14px 24px}
#craftPanel{position:fixed;inset:0;background:#173c2baa;z-index:4;display:none;align-items:center;justify-content:center;padding:15px}
#craftPanel .panel{width:100%}
#craftPanel button{margin:5px}
@media(max-height:550px){
 header{height:42px}
 #game{height:calc(100dvh - 120px)}
 .controls{height:78px}
 #message{top:47px}
 .pad{grid-template-columns:repeat(3,35px);grid-template-rows:repeat(2,29px)}
}
</style>
</head>
<body>
<header>
<h1>🌲 Forest Friends 2D</h1>
<div class="stats">
<div class="stat">🪵 <span id="wood">0</span></div>
<div class="stat">🍓 <span id="berries">0</span></div>
<div class="stat">🔥 <span id="fuel">0</span></div>
<div class="stat">🌙 <span id="night">0</span>/3</div>
</div>
</header>

<canvas id="game"></canvas>
<div id="message">Welcome! Explore the forest and find some wood!</div>

<div class="controls">
<div class="pad">
<span></span><button data-key="up">⬆️</button><span></span>
<button data-key="left">⬅️</button>
<button data-key="down">⬇️</button>
<button data-key="right">➡️</button>
</div>
<button class="action" id="collect">🪓<br>Collect</button>
<button class="action" id="craft">🛠️<br>Craft</button>
<button class="blue" id="fuelBtn">🔥<br>Fuel</button>
<button class="pink" id="berryBtn">🍓<br>Pick</button>
<button class="blue" id="nightBtn">🌙<br>Night</button>
<button class="pink" id="feedBtn">🍓<br>Feed Deer</button>
</div>

<div id="start">
<div class="panel">
<div style="font-size:65px">🌲🦌🌲</div>
<h2>Forest Friends 2D</h2>
<p>Explore a colorful forest, collect wood, and build a cozy campfire!</p>
<p>☀️ The deer sleeps during the day.</p>
<p>🌙 At night, a slow, grumpy deer comes out to find you!</p>
<p><b>Move:</b> Use arrow buttons or WASD.</p>
<button onclick="startGame()">🌳 START ADVENTURE!</button>
</div>
</div>

<div id="craftPanel">
<div class="panel">
<h2>🛠️ Crafting Table</h2>
<p>What would you like to build?</p>
<button onclick="makeFire()">🔥 Campfire (5 wood)</button>
<button onclick="makeFence()">🪵 Wooden Fence (3 wood)</button>
<button onclick="closeCraft()">❌ Close</button>
</div>
</div>

<script>
const canvas=document.getElementById('game');
const ctx=canvas.getContext('2d');

let W=800,H=500;
const MAP=1800;
let running=false,last=0;
let wood=0,berries=0,fuel=0,night=0;
let fireBuilt=false,fireX=0,fireY=0;
let trees=[],bushes=[],fences=[];
let keys={},touch={};
let audio=null;
let player={x:900,y:700,speed:185};
let deer={x:1120,y:780,speed:65,stun:0,cheer:0};
let camera={x:0,y:0};
let warningShown=false;

function resize(){
 const r=canvas.getBoundingClientRect();
 const d=Math.min(devicePixelRatio||1,2);
 canvas.width=Math.round(r.width*d);
 canvas.height=Math.round(r.height*d);
 W=r.width;H=r.height;
 ctx.setTransform(d,0,0,d,0,0);
}
window.addEventListener('resize',resize);
resize();

function say(t){
 document.getElementById('message').textContent=t;
}
function beep(f=440,d=.1){
 try{
 audio=audio||new(window.AudioContext||window.webkitAudioContext)();
 const o=audio.createOscillator(),g=audio.createGain();
 o.connect(g);g.connect(audio.destination);
 o.frequency.value=f;
 g.gain.setValueAtTime(.05,audio.currentTime);
 g.gain.exponentialRampToValueAtTime(.001,audio.currentTime+d);
 o.start();o.stop(audio.currentTime+d);
 }catch(e){}
}
function rand(a,b){return a+Math.random()*(b-a)}
function distance(a,b){return Math.hypot(a.x-b.x,a.y-b.y)}

function newWorld(){
 trees=[];bushes=[];fences=[];
 for(let i=0;i<95;i++){
  let x=rand(45,MAP-45),y=rand(45,MAP-45);
  if(Math.hypot(x-900,y-700)<130)continue;
  trees.push({x,y,alive:true,size:rand(.85,1.2)});
 }
 for(let i=0;i<30;i++){
  bushes.push({x:rand(40,MAP-40),y:rand(40,MAP-40),alive:true});
 }
}
newWorld();

function startGame(){
 document.getElementById('start').style.display='none';
 running=true;
 last=performance.now();
 say('☀️ It is daytime! The deer is sleeping. Explore safely!');
 beep(600);
 requestAnimationFrame(loop);
}

function drawTree(t){
 ctx.save();
 ctx.translate(t.x,t.y);
 const s=t.size;
 ctx.fillStyle='#79502e';
 ctx.fillRect(-9*s,-2*s,18*s,34*s);
 ctx.fillStyle='#247c3c';
 ctx.beginPath();ctx.arc(0,-22*s,26*s,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#379b4c';
 ctx.beginPath();ctx.arc(-14*s,-34*s,19*s,0,Math.PI*2);ctx.fill();
 ctx.beginPath();ctx.arc(14*s,-35*s,19*s,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#68bb58';
 ctx.beginPath();ctx.arc(-6*s,-43*s,10*s,0,Math.PI*2);ctx.fill();
 ctx.restore();
}

function drawBush(b){
 ctx.fillStyle='#348444';
 ctx.beginPath();ctx.arc(b.x,b.y,15,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#e9577c';
 for(let i=0;i<4;i++){
  ctx.beginPath();ctx.arc(b.x+(i-2)*6,b.y-5,3,0,Math.PI*2);ctx.fill();
 }
}

function drawPlayer(){
 ctx.save();
 ctx.translate(player.x,player.y);
 ctx.fillStyle='#35633a66';
 ctx.beginPath();ctx.ellipse(0,13,16,7,0,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#345b87';
 ctx.fillRect(-11,4,8,15);ctx.fillRect(4,4,8,15);
 ctx.fillStyle='#438dcc';
 ctx.fillRect(-14,-18,28,27);
 ctx.fillStyle='#a66d36';
 ctx.fillRect(-18,-14,7,19);
 ctx.fillStyle='#f1c08d';
 ctx.beginPath();ctx.arc(0,-24,12,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#69452e';
 ctx.fillRect(-11,-35,22,7);
 ctx.fillStyle='#fff';
 ctx.fillRect(-5,-27,3,3);ctx.fillRect(4,-27,3,3);
 ctx.restore();
}

function drawDeer(){
 if(night===0)return;
 ctx.save();
 ctx.translate(deer.x,deer.y);
 const angle=Math.atan2(player.y-deer.y,player.x-deer.x);
 ctx.rotate(angle);
 ctx.fillStyle='#315e3066';
 ctx.beginPath();ctx.ellipse(0,12,25,10,0,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#76502e';
 ctx.fillRect(-15,0,6,16);ctx.fillRect(9,0,6,16);
 ctx.fillStyle='#aa7042';
 ctx.beginPath();ctx.ellipse(0,-7,25,15,0,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#ba8050';
 ctx.fillRect(13,-26,12,23);
 ctx.beginPath();ctx.arc(24,-25,13,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#e7c59a';
 ctx.beginPath();ctx.arc(33,-20,7,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#fff';
 ctx.beginPath();ctx.arc(26,-29,4,0,Math.PI*2);ctx.fill();
 ctx.fillStyle='#251b13';
 ctx.beginPath();ctx.arc(27,-29,2,0,Math.PI*2);ctx.fill();
 ctx.strokeStyle='#3b251a';ctx.lineWidth=3;
 ctx.beginPath();ctx.moveTo(22,-35);ctx.lineTo(30,-32);ctx.stroke();
 ctx.strokeStyle='#eee1bd';ctx.lineWidth=4;
 ctx.beginPath();
 ctx.moveTo(18,-36);ctx.lineTo(12,-50);ctx.lineTo(5,-54);
 ctx.moveTo(12,-50);ctx.lineTo(14,-58);
 ctx.moveTo(31,-36);ctx.lineTo(37,-50);ctx.lineTo(44,-54);
 ctx.moveTo(37,-50);ctx.lineTo(35,-58);
 ctx.stroke();
 ctx.fillStyle='#fff';
 ctx.font='bold 16px Arial';
 ctx.fillText('😠',-12,-46);
 ctx.restore();
}

function drawFire(){
 if(!fireBuilt)return;
 ctx.fillStyle='#76502d';
 ctx.fillRect(fireX-23,fireY-5,46,9);
 ctx.fillRect(fireX-17,fireY-13,34,8);
 if(fuel>0){
  ctx.font='35px Arial';
  ctx.textAlign='center';
  ctx.fillText('🔥',fireX,fireY-16);
  ctx.textAlign='left';
 }else{
  ctx.font='20px Arial';
  ctx.fillText('🪵',fireX-10,fireY-16);
 }
}

function drawFences(){
 ctx.fillStyle='#a97a47';
 for(const f of fences){
  ctx.fillRect(f.x-16,f.y-15,32,7);
  ctx.fillRect(f.x-16,f.y+3,32,7);
  ctx.fillRect(f.x-14,f.y-22,6,38);
  ctx.fillRect(f.x+9,f.y-22,6,38);
 }
}

function render(){
 camera.x=Math.max(0,Math.min(MAP-W,player.x-W/2));
 camera.y=Math.max(0,Math.min(MAP-H,player.y-H/2));
 ctx.clearRect(0,0,W,H);
 ctx.save();
 ctx.translate(-camera.x,-camera.y);

 ctx.fillStyle=night===0?'#85c95e':'#547b51';
 ctx.fillRect(0,0,MAP,MAP);

 ctx.strokeStyle=night===0?'#78bc57':'#496f49';
 ctx.lineWidth=1;
 for(let x=0;x<MAP;x+=65){
  for(let y=0;y<MAP;y+=65){
   ctx.beginPath();ctx.moveTo(x,y);ctx.lineTo(x+4,y-5);ctx.stroke();
  }
 }

 ctx.strokeStyle='#e2d29a';ctx.lineWidth=15;
 ctx.strokeRect(0,0,MAP,MAP);

 let objects=[];
 for(const t of trees)if(t.alive)objects.push({y:t.y,draw:()=>drawTree(t)});
 for(const b of bushes)if(b.alive)objects.push({y:b.y,draw:()=>drawBush(b)});
 objects.push({y:fireY,draw:drawFire});
 if(night>0)objects.push({y:deer.y,draw:drawDeer});
 objects.push({y:player.y,draw:drawPlayer});
 objects.sort((a,b)=>a.y-b.y);
 objects.forEach(o=>o.draw());
 drawFences();
 ctx.restore();

 if(night>0){
  ctx.fillStyle='rgba(20,34,78,.38)';
  ctx.fillRect(0,0,W,H);
  ctx.fillStyle='#fff5a8';
  ctx.font='30px Arial';
  ctx.fillText('🌙',W-55,45);
  ctx.fillStyle='#fff';
  ctx.font='14px Arial';
  ctx.fillText('NIGHT '+night, W-115,65);
  // Campfire glow
  if(fireBuilt&&fuel>0){
   const x=fireX-camera.x,y=fireY-camera.y;
   const glow=ctx.createRadialGradient(x,y,10,x,y,145);
   glow.addColorStop(0,'rgba(255,180,50,.24)');
   glow.addColorStop(1,'rgba(255,180,50,0)');
   ctx.fillStyle=glow;
   ctx.fillRect(x-145,y-145,290,290);
  }
 }else{
  ctx.fillStyle='#fff';
  ctx.font='23px Arial';
  ctx.fillText('☀️',W-42,38);
 }
}

function move(dt){
 let dx=0,dy=0;
 if(keys['w']||keys['arrowup']||touch.up)dy--;
 if(keys['s']||keys['arrowdown']||touch.down)dy++;
 if(keys['a']||keys['arrowleft']||touch.left)dx--;
 if(keys['d']||keys['arrowright']||touch.right)dx++;
 if(dx||dy){
  const len=Math.hypot(dx,dy);
  dx/=len;dy/=len;
  player.x+=dx*player.speed*dt;
  player.y+=dy*player.speed*dt;
  player.x=Math.max(25,Math.min(MAP-25,player.x));
  player.y=Math.max(25,Math.min(MAP-25,player.y));
 }
}

function deerAI(dt){
 // The deer NEVER appears or chases during daytime.
 if(night===0)return;
 if(deer.stun>0){deer.stun-=dt;return}
 if(deer.cheer>0){deer.cheer-=dt;return}

 const d=distance(player,deer);

 // The deer only starts following when it gets reasonably close.
 if(d<550&&d>35){
  const speed=65;
  deer.x+=(player.x-deer.x)/d*speed*dt;
  deer.y+=(player.y-deer.y)/d*speed*dt;
 }

 if(d<35){
  deer.stun=3;
  wood=Math.max(0,wood-1);
  say('🦌😠 GOTCHA! The grumpy deer stole one piece of wood! Run away!');
  beep(180,.3);
  updateStats();
 }
}

function collect(){
 if(!running)return;
 let nearest=null,d=65;
 for(const t of trees){
  if(!t.alive)continue;
  const n=distance(player,t);
  if(n<d){nearest=t;d=n}
 }
 if(nearest){
  nearest.alive=false;
  wood+=2;
  say('🌳 Great job! You collected 2 pieces of wood!');
  beep(520);
 }else say('Walk closer to a tree to collect wood!');
 updateStats();
}

function pickBerry(){
 if(!running)return;
 let nearest=null,d=65;
 for(const b of bushes){
  if(!b.alive)continue;
  const n=distance(player,b);
  if(n<d){nearest=b;d=n}
 }
 if(nearest){
  nearest.alive=false;
  berries++;
  say('🍓 You found a juicy berry!');
  beep(650);
 }else say('Walk closer to a berry bush to pick berries!');
 updateStats();
}

function feedDeer(){
 if(night===0){say('The deer is sleeping during the day!');return}
 if(berries<1){say('You need a berry first!');return}
 if(distance(player,deer)>120){say('Get closer to the grumpy deer!');return}
 berries--;
 wood+=2;
 deer.cheer=10;
 deer.stun=0;
 say('🍓 The deer loves your berry! It takes a 10-second break and gives you 2 wood!');
 beep(800);
 updateStats();
}

function openCraft(){
 document.getElementById('craftPanel').style.display='flex';
}
function closeCraft(){
 document.getElementById('craftPanel').style.display='none';
}
function makeFire(){
 if(wood<5){say('You need 5 wood to build a campfire!');return}
 if(fireBuilt){say('You already have a campfire!');return}
 wood-=5;
 fireBuilt=true;
 fireX=player.x;fireY=player.y;
 closeCraft();
 say('🔥 You built a campfire! Add wood to keep it glowing.');
 beep(700,.3);updateStats();
}
function makeFence(){
 if(wood<3){say('You need 3 wood for a fence!');return}
 wood-=3;
 fences.push({x:player.x+35,y:player.y+35});
 closeCraft();
 say('🪵 You built a wooden fence!');
 beep(500);updateStats();
}
function addFuel(){
 if(!fireBuilt){say('Build a campfire first!');return}
 if(wood<1){say('You need wood to fuel your fire!');return}
 wood--;fuel++;
 say('🔥 Your campfire is glowing brighter!');
 beep(600);updateStats();
}

function nextNight(){
 if(!running)return;
 if(night===0){
  if(!fireBuilt){say('Build your campfire first!');return}
  if(fuel<2){say('Add at least 2 wood to your fire before nightfall!');return}
  night=1;
  deer.x=player.x+430;
  deer.y=player.y+250;
  deer.cheer=0;
  warningShown=false;
  say('🌙 NIGHT 1! The grumpy deer has woken up! It moves slowly. Keep your distance!');
  beep(260,.3);
 }else{
  if(fuel<2){say('You need 2 firewood to get through another night!');return}
  fuel-=2;
  night++;
  if(night>3){
   running=false;
   document.getElementById('start').style.display='flex';
   document.querySelector('#start .panel').innerHTML=
   '<div style="font-size:65px">🏆🦌🔥</div><h2>Forest Champion!</h2><p>You survived all three nights! Even the grumpy deer is impressed!</p><button onclick="resetGame()">🔄 Play Again!</button>';
   return;
  }
  deer.x=player.x+430;
  deer.y=player.y+250;
  deer.stun=0;
  deer.cheer=0;
  warningShown=false;
  say('🌙 Night '+night+'! The grumpy deer is back! Keep moving!');
  beep(260,.3);
 }
 updateStats();
}

function resetGame(){
 wood=0;berries=0;fuel=0;night=0;
 fireBuilt=false;
 player={x:900,y:700,speed:185};
 deer={x:1120,y:780,speed:65,stun:0,cheer:0};
 newWorld();
 document.getElementById('start').style.display='none';
 running=true;last=performance.now();
 say('☀️ A fresh daytime adventure begins!');
 updateStats();
 requestAnimationFrame(loop);
}

function loop(time){
 if(!running)return;
 const dt=Math.min((time-last)/1000||0,.04);
 last=time;
 move(dt);
 deerAI(dt);
 render();
 requestAnimationFrame(loop);
}

window.addEventListener('keydown',e=>{
 const k=e.key.toLowerCase();
 keys[k]=true;
 if(['arrowup','arrowdown','arrowleft','arrowright',' '].includes(k))e.preventDefault();
 if(e.repeat)return;
 if(k==='e')collect();
 if(k==='b')pickBerry();
 if(k==='q')feedDeer();
 if(k==='c')openCraft();
 if(k==='f')addFuel();
 if(k==='n')nextNight();
});
window.addEventListener('keyup',e=>keys[e.key.toLowerCase()]=false);
window.addEventListener('blur',()=>{keys={};touch={}});

document.querySelectorAll('[data-key]').forEach(btn=>{
 const k=btn.dataset.key;
 btn.addEventListener('pointerdown',e=>{
  e.preventDefault();touch[k]=true;
  btn.setPointerCapture(e.pointerId);
 });
 const stop=()=>touch[k]=false;
 btn.addEventListener('pointerup',stop);
 btn.addEventListener('pointercancel',stop);
 btn.addEventListener('lostpointercapture',stop);
});

document.getElementById('collect').onclick=collect;
document.getElementById('craft').onclick=openCraft;
document.getElementById('fuelBtn').onclick=addFuel;
document.getElementById('berryBtn').onclick=pickBerry;
document.getElementById('feedBtn').onclick=feedDeer;
document.getElementById('nightBtn').onclick=nextNight;

updateStats();
</script>
</body>
</html>

Game Source: Forest Friends 2D: The Night Deer

Creator: LunarFlare59

Libraries: none

Complexity: complex (538 lines, 16.3 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: forest-friends-2d-the-night-deer-lunarflare59" to link back to the original. Then publish at arcadelab.ai/publish.