Forest Friends 2D: Grumpy Deer
by LunarFlare59534 lines16.3 KB
<!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}
.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:15px;padding:9px;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:65px}
.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>
</div>
<div id="start">
<div class="panel">
<div style="font-size:65px">🌲🦌🌲</div>
<h2>Forest Friends 2D</h2>
<p>Welcome, explorer! Explore a colorful forest, collect wood, and build a cozy campfire.</p>
<p>Watch out! A grumpy deer is roaming around and loves chasing explorers!</p>
<p><b>Move:</b> Use the arrow buttons or WASD.</p>
<p><b>Collect:</b> Walk near a tree or berry bush and press the matching button.</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 soundOn=true,audio=null;
let messageTimer=0;
let player={x:900,y:700,speed:185};
let deer={x:1120,y:780,speed:105,stun:0,cheer:0};
let caught=0;
let camera={x:0,y:0};
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;
messageTimer=3;
}
function beep(f=440,d=.1){
if(!soundOn)return;
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(.06,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('Explore the forest! The grumpy deer is somewhere nearby!');
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+rand(-9,9),b.y+rand(-9,9),3,0,Math.PI*2);ctx.fill();
}
}
function drawPlayer(){
ctx.save();
ctx.translate(player.x,player.y);
// shadow
ctx.fillStyle='#35633a66';
ctx.beginPath();ctx.ellipse(0,13,16,7,0,0,Math.PI*2);ctx.fill();
// legs
ctx.fillStyle='#345b87';
ctx.fillRect(-11,4,8,15);ctx.fillRect(4,4,8,15);
// body
ctx.fillStyle='#438dcc';
ctx.fillRect(-14,-18,28,27);
// backpack
ctx.fillStyle='#a66d36';
ctx.fillRect(-18,-14,7,19);
// head
ctx.fillStyle='#f1c08d';
ctx.beginPath();ctx.arc(0,-24,12,0,Math.PI*2);ctx.fill();
// hair
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(){
ctx.save();
ctx.translate(deer.x,deer.y);
const angle=Math.atan2(player.y-deer.y,player.x-deer.x);
ctx.rotate(angle);
// shadow
ctx.fillStyle='#315e3066';
ctx.beginPath();ctx.ellipse(0,12,25,10,0,0,Math.PI*2);ctx.fill();
// legs
ctx.fillStyle='#76502e';
ctx.fillRect(-15,0,6,16);ctx.fillRect(9,0,6,16);
// body
ctx.fillStyle='#aa7042';
ctx.beginPath();ctx.ellipse(0,-7,25,15,0,0,Math.PI*2);ctx.fill();
// neck and head
ctx.fillStyle='#ba8050';
ctx.fillRect(13,-26,12,23);
ctx.beginPath();ctx.arc(24,-25,13,0,Math.PI*2);ctx.fill();
// muzzle
ctx.fillStyle='#e7c59a';
ctx.beginPath();ctx.arc(33,-20,7,0,Math.PI*2);ctx.fill();
// eye
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();
// angry eyebrows
ctx.strokeStyle='#3b251a';ctx.lineWidth=3;
ctx.beginPath();ctx.moveTo(22,-35);ctx.lineTo(30,-32);ctx.stroke();
// antlers
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();
// angry speech
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 follows the player.
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);
// Grass
ctx.fillStyle=night===0?'#85c95e':'#547b51';
ctx.fillRect(0,0,MAP,MAP);
// Grass details
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();
}
}
// World border
ctx.strokeStyle='#e2d29a';ctx.lineWidth=15;
ctx.strokeRect(0,0,MAP,MAP);
// Sort objects by vertical position
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});
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();
// Day/night tint
if(night>0){
ctx.fillStyle='rgba(20,34,78,.18)';
ctx.fillRect(0,0,W,H);
}
// Compass
ctx.fillStyle='#ffffffcc';
ctx.fillRect(10,H-37,125,27);
ctx.fillStyle='#285436';
ctx.font='bold 12px Arial';
ctx.fillText('🧭 Forest Explorer',17,H-19);
}
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){
let 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){
if(deer.stun>0){deer.stun-=dt;return}
if(deer.cheer>0){deer.cheer-=dt;return}
const d=distance(player,deer);
if(d>30){
deer.x+=(player.x-deer.x)/d*deer.speed*dt;
deer.y+=(player.y-deer.y)/d*deer.speed*dt;
}
if(d<34){
deer.stun=2;
wood=Math.max(0,wood-1);
caught++;
say('🦌😠 GRRR! The deer caught you and 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(berries<1){say('You need a berry first!');return}
if(distance(player,deer)>100){say('Get closer to the grumpy deer!');return}
berries--;
wood+=2;
deer.cheer=8;
deer.stun=0;
say('🍓 The deer loves your berry! It gives you 2 wood and takes a little break!');
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 updateStats(){
document.getElementById('wood').textContent=wood;
document.getElementById('berries').textContent=berries;
document.getElementById('fuel').textContent=fuel;
document.getElementById('night').textContent=night;
}
function nextNight(){
if(fuel<2){say('You need 2 firewood for another cozy 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>';
}else say('🌙 Night '+night+' is complete! Keep exploring and gathering wood!');
updateStats();
}
function resetGame(){
wood=0;berries=0;fuel=0;night=0;
fireBuilt=false;caught=0;
player={x:900,y:700,speed:185};
deer={x:1120,y:780,speed:105,stun:0,cheer:0};
newWorld();
document.getElementById('start').style.display='none';
document.querySelector('#start .panel').innerHTML=
'<div style="font-size:65px">🌲🦌🌲</div><h2>Forest Friends 2D</h2><p>Explore, collect wood, and watch out for the grumpy deer!</p><button onclick="startGame()">START ADVENTURE</button>';
running=true;last=performance.now();
say('A fresh 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();
if(messageTimer>0)messageTimer-=dt;
requestAnimationFrame(loop);
}
// Keyboard movement
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={}});
// Touch directional controls
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);
});
// Buttons
document.getElementById('collect').onclick=collect;
document.getElementById('craft').onclick=openCraft;
document.getElementById('fuelBtn').onclick=addFuel;
document.getElementById('berryBtn').onclick=pickBerry;
document.getElementById('berryBtn').addEventListener('dblclick',feedDeer);
document.getElementById('berryBtn').addEventListener('contextmenu',e=>{
e.preventDefault();feedDeer();
});
// Helpful keyboard instructions
say('Explore the forest! Use WASD or arrows. E collects, B picks berries, Q feeds the deer.');
updateStats();
</script>
</body>
</html>
Game Source: Forest Friends 2D: Grumpy Deer
Creator: LunarFlare59
Libraries: none
Complexity: complex (534 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-grumpy-deer-lunarflare59" to link back to the original. Then publish at arcadelab.ai/publish.