Forest Friends 3D: Grumpy Deer
by LunarFlare59591 lines17.6 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 3D</title>
<style>
*{box-sizing:border-box}
body{
margin:0;background:#a9df96;color:#24472c;
font-family:Trebuchet MS,Arial,sans-serif;
overflow:hidden;touch-action:none
}
header{
height:55px;background:#347b43;color:white;
display:flex;align-items:center;justify-content:space-around;
padding:5px;gap:5px
}
h1{font-size:clamp(16px,4vw,24px);margin:0}
.stats{display:flex;gap:7px;font-size:13px;font-weight:bold;flex-wrap:wrap}
.stat{background:#fff;color:#24512f;padding:6px 9px;border-radius:9px}
#game{
display:block;width:100%;height:calc(100dvh - 55px - 105px);
background:#8bd4ed;touch-action:none
}
.controls{
height:105px;background:#d5edc5;
display:flex;align-items:center;justify-content:space-around;
padding:5px;gap:5px
}
.pad{display:grid;grid-template-columns:repeat(3,42px);grid-template-rows:repeat(2,39px);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:67px}
.blue{background:#3687bb;box-shadow:0 3px #205a80}
.pink{background:#ce7296;box-shadow:0 3px #904666}
#message{
position:absolute;top:65px;left:50%;transform:translateX(-50%);
background:#fff8d9;border:2px solid #dfbd65;border-radius:12px;
padding:9px 14px;font-size:14px;font-weight:bold;
width:max-content;max-width:90%;text-align:center;
pointer-events:none;z-index:2
}
#help{
position:absolute;bottom:110px;left:50%;transform:translateX(-50%);
background:#ffffffcc;border-radius:10px;padding:6px 10px;
font-size:12px;pointer-events:none
}
#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 - 122px)}
.controls{height:80px}
#help{bottom:84px}
#message{top:48px}
}
</style>
</head>
<body>
<header>
<h1>๐ฒ Forest Friends 3D</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="fire">0</span></div>
<div class="stat">๐ <span id="night">0</span>/3</div>
</div>
</header>
<canvas id="game"></canvas>
<div id="message">Welcome to the forest! Find some trees!</div>
<div id="help">WASD / Arrow keys to move ยท Walk near trees to collect 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>Berry</button>
</div>
<div id="start">
<div class="panel">
<div style="font-size:65px">๐ฒ๐ฆ๐ฒ</div>
<h2>Forest Friends 3D</h2>
<p>Welcome, explorer! Explore the forest, collect wood, and build a cozy campfire.</p>
<p>Watch out! A grumpy deer is roaming around. It wants to play tricks on you!</p>
<p><b>Move:</b> Use the arrow buttons or WASD.</p>
<p><b>Collect:</b> Walk near a tree and press Collect.</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;
let running=false,last=0;
let wood=0,berries=0,fuel=0,night=0;
let fireBuilt=false,fireX=0,fireZ=0;
let fences=[],trees=[],berryBushes=[];
let keys={},touch={},deerCaught=0;
let messageTimer=0;
let player={x:0,z:0,yaw:0};
let deer={x:5,z:9,speed:1.6,stun:0,cheer:0};
let soundOn=true,audio=null;
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=canvas.width;H=canvas.height;
ctx.setTransform(d,0,0,d,0,0);
W=r.width;H=r.height;
}
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 startGame(){
document.getElementById('start').style.display='none';
running=true;
say('Explore the forest! The grumpy deer is somewhere nearby...');
beep(600);
requestAnimationFrame(loop);
}
function rand(a,b){return a+Math.random()*(b-a)}
function distance(a,b){return Math.hypot(a.x-b.x,a.z-b.z)}
function newWorld(){
trees=[];berryBushes=[];fences=[];
for(let i=0;i<65;i++){
let x=rand(-24,24),z=rand(-24,24);
if(Math.hypot(x,z)<5)continue;
trees.push({x,z,alive:true,size:rand(.8,1.2)});
}
for(let i=0;i<18;i++){
berryBushes.push({x:rand(-22,22),z:rand(-22,22),alive:true});
}
}
newWorld();
function project(x,y,z){
// Camera follows the player from behind.
const yaw=player.yaw;
const fx=Math.sin(yaw),fz=Math.cos(yaw);
const rx=Math.cos(yaw),rz=-Math.sin(yaw);
const cx=player.x-fx*7,cz=player.z-fz*7;
const dx=x-cx,dz=z-cz;
const depth=dx*fx+dz*fz;
const side=dx*rx+dz*rz;
if(depth<.3)return null;
const focal=H*.85;
return {
x:W/2+side*focal/depth,
y:H*.48+(4.2-y)*focal/depth,
scale:focal/depth,
depth
};
}
function rect3(x,y,z,w,h,color){
const p=project(x,y,z);
if(!p)return;
const s=p.scale;
ctx.fillStyle=color;
ctx.fillRect(p.x-w*s/2,p.y-h*s,w*s,h*s);
}
function box3(x,y,z,w,h,d,color){
const p=project(x,y,z);
if(!p||p.depth<.3)return;
const s=p.scale;
const ww=w*s,hh=h*s,dd=d*s;
ctx.fillStyle=color;
ctx.fillRect(p.x-ww/2,p.y-hh,ww,hh);
ctx.fillStyle='rgba(0,0,0,.12)';
ctx.beginPath();
ctx.moveTo(p.x+ww/2,p.y-hh);
ctx.lineTo(p.x+ww/2+dd*.35,p.y-hh-dd*.35);
ctx.lineTo(p.x+ww/2+dd*.35,p.y-dd*.35);
ctx.lineTo(p.x+ww/2,p.y);
ctx.fill();
}
function drawGround(){
const sky=ctx.createLinearGradient(0,0,0,H);
if(night===0){
sky.addColorStop(0,'#83d4f0');sky.addColorStop(1,'#b6e99b');
}else{
sky.addColorStop(0,'#354f7c');sky.addColorStop(1,'#90a9bc');
}
ctx.fillStyle=sky;ctx.fillRect(0,0,W,H);
ctx.fillStyle=night===0?'#72bb59':'#4b794e';
ctx.fillRect(0,H*.48,W,H*.52);
// Ground grid
ctx.strokeStyle=night===0?'#65a950':'#436e47';
ctx.lineWidth=1;
for(let i=-12;i<=12;i++){
const a=project(i*3,0,-30),b=project(i*3,0,30);
if(a&&b){ctx.beginPath();ctx.moveTo(a.x,a.y);ctx.lineTo(b.x,b.y);ctx.stroke()}
const c=project(-40,0,i*3),d=project(40,0,i*3);
if(c&&d){ctx.beginPath();ctx.moveTo(c.x,c.y);ctx.lineTo(d.x,d.y);ctx.stroke()}
}
}
function drawTree(t){
const p=project(t.x,0,t.z);
if(!p)return;
const s=p.scale*t.size;
if(s<1)return;
// trunk
ctx.fillStyle='#87552e';
ctx.fillRect(p.x-7*s,p.y-38*s,14*s,38*s);
// blocky leafy top
ctx.fillStyle='#25854a';
ctx.fillRect(p.x-27*s,p.y-85*s,54*s,49*s);
ctx.fillStyle='#3da95c';
ctx.fillRect(p.x-18*s,p.y-100*s,36*s,30*s);
ctx.fillStyle='#66bd60';
ctx.fillRect(p.x-9*s,p.y-95*s,12*s,10*s);
}
function drawBush(b){
const p=project(b.x,0,b.z);
if(!p)return;
const s=p.scale;
if(s<1)return;
ctx.fillStyle='#397c43';
ctx.beginPath();ctx.arc(p.x,p.y-12*s,13*s,0,Math.PI*2);ctx.fill();
ctx.fillStyle='#e85e7c';
for(let i=0;i<3;i++){
ctx.beginPath();ctx.arc(p.x+(i-1)*7*s,p.y-16*s,3*s,0,Math.PI*2);ctx.fill();
}
}
function drawDeer(){
const p=project(deer.x,0,deer.z);
if(!p)return;
const s=p.scale;
if(s<1)return;
// Blocky deer body
ctx.fillStyle='#a66a3c';
ctx.fillRect(p.x-21*s,p.y-37*s,42*s,23*s);
// neck
ctx.fillRect(p.x+10*s,p.y-56*s,13*s,29*s);
// head
ctx.fillStyle='#b87b48';
ctx.fillRect(p.x+10*s,p.y-68*s,25*s,22*s);
// face
ctx.fillStyle='#30251d';
ctx.fillRect(p.x+28*s,p.y-60*s,5*s,5*s);
// legs
ctx.fillStyle='#79502e';
for(let i=0;i<4;i++)ctx.fillRect(p.x+(-16+i*10)*s,p.y-15*s,5*s,17*s);
// antlers
ctx.strokeStyle='#eee0b6';ctx.lineWidth=Math.max(2,3*s);
ctx.beginPath();
ctx.moveTo(p.x+17*s,p.y-67*s);ctx.lineTo(p.x+13*s,p.y-82*s);
ctx.lineTo(p.x+5*s,p.y-88*s);
ctx.moveTo(p.x+14*s,p.y-77*s);ctx.lineTo(p.x+20*s,p.y-87*s);
ctx.moveTo(p.x+29*s,p.y-67*s);ctx.lineTo(p.x+35*s,p.y-82*s);
ctx.lineTo(p.x+42*s,p.y-87*s);
ctx.stroke();
// angry eyebrows
ctx.strokeStyle='#35251b';ctx.lineWidth=Math.max(2,3*s);
ctx.beginPath();
ctx.moveTo(p.x+27*s,p.y-62*s);ctx.lineTo(p.x+34*s,p.y-58*s);
ctx.stroke();
ctx.fillStyle='#fff';
ctx.font=`bold ${Math.max(10,17*s)}px Arial`;
ctx.fillText('๐ ',p.x+5*s,p.y-87*s);
}
function drawPlayer(){
const p=project(player.x,0,player.z);
if(!p)return;
const s=p.scale;
if(s<1)return;
// Back of blocky explorer
ctx.fillStyle='#3c83bd';
ctx.fillRect(p.x-15*s,p.y-48*s,30*s,34*s);
ctx.fillStyle='#edbc83';
ctx.fillRect(p.x-12*s,p.y-68*s,24*s,22*s);
ctx.fillStyle='#65432d';
ctx.fillRect(p.x-13*s,p.y-72*s,26*s,9*s);
ctx.fillStyle='#284c75';
ctx.fillRect(p.x-12*s,p.y-15*s,9*s,16*s);
ctx.fillRect(p.x+3*s,p.y-15*s,9*s,16*s);
}
function drawFire(){
if(!fireBuilt)return;
const p=project(fireX,0,fireZ);
if(!p)return;
const s=p.scale;
ctx.fillStyle='#77502e';
ctx.fillRect(p.x-25*s,p.y-7*s,50*s,9*s);
ctx.fillRect(p.x-18*s,p.y-15*s,36*s,8*s);
if(fuel>0){
ctx.fillStyle='#ff8c28';
ctx.beginPath();ctx.moveTo(p.x,p.y-17*s);
ctx.quadraticCurveTo(p.x-24*s,p.y-40*s,p.x,p.y-48*s);
ctx.quadraticCurveTo(p.x+22*s,p.y-37*s,p.x,p.y-17*s);
ctx.fill();
ctx.fillStyle='#ffe35d';
ctx.beginPath();ctx.moveTo(p.x,p.y-20*s);
ctx.quadraticCurveTo(p.x-10*s,p.y-36*s,p.x,p.y-39*s);
ctx.quadraticCurveTo(p.x+11*s,p.y-30*s,p.x,p.y-20*s);
ctx.fill();
}
}
function drawFences(){
for(const f of fences){
const p=project(f.x,0,f.z);
if(!p)continue;
const s=p.scale;
ctx.fillStyle='#a97a47';
ctx.fillRect(p.x-17*s,p.y-27*s,34*s,6*s);
ctx.fillRect(p.x-17*s,p.y-13*s,34*s,5*s);
ctx.fillRect(p.x-15*s,p.y-33*s,5*s,34*s);
ctx.fillRect(p.x+10*s,p.y-33*s,5*s,34*s);
}
}
function render(){
drawGround();
let items=[];
for(const t of trees)if(t.alive)items.push({z:t.z,draw:()=>drawTree(t)});
for(const b of berryBushes)if(b.alive)items.push({z:b.z,draw:()=>drawBush(b)});
items.push({z:deer.z,draw:drawDeer});
items.push({z:player.z,draw:drawPlayer});
items.push({z:fireZ,draw:drawFire});
items.sort((a,b)=>b.z-a.z);
items.forEach(i=>i.draw());
drawFences();
}
function move(dt){
let forward=0,side=0;
if(keys['w']||keys['arrowup']||touch.up)forward++;
if(keys['s']||keys['arrowdown']||touch.down)forward--;
if(keys['a']||keys['arrowleft']||touch.left)side--;
if(keys['d']||keys['arrowright']||touch.right)side++;
if(forward||side){
const len=Math.hypot(forward,side);
forward/=len;side/=len;
const yaw=player.yaw;
player.x+=(Math.cos(yaw)*side+Math.sin(yaw)*forward)*5*dt;
player.z+=(-Math.sin(yaw)*side+Math.cos(yaw)*forward)*5*dt;
player.x=Math.max(-28,Math.min(28,player.x));
player.z=Math.max(-28,Math.min(28,player.z));
if(forward)player.yaw=Math.atan2(player.x-deer.x,player.z-deer.z)+Math.PI;
}
}
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>1.2){
const speed=deer.speed+(night*.25);
deer.x+=(player.x-deer.x)/d*speed*dt;
deer.z+=(player.z-deer.z)/d*speed*dt;
}
if(d<1.4){
deer.stun=2;
wood=Math.max(0,wood-1);
deerCaught++;
say('๐ฆ๐ GOTCHA! The grumpy deer stole a piece of wood! It does a silly dance!');
beep(170,.3);
updateStats();
}
}
function collect(){
if(!running)return;
let nearest=null,dist=3;
for(const t of trees){
if(!t.alive)continue;
let d=distance(player,t);
if(d<dist){nearest=t;dist=d}
}
if(nearest){
nearest.alive=false;wood+=2;
say('๐ณ Chopped a tree! You collected 2 wood!');
beep(520);
}else{
say('Walk closer to a tree before collecting wood!');
}
updateStats();
}
function pickBerry(){
let nearest=null,dist=3;
for(const b of berryBushes){
if(!b.alive)continue;
let d=distance(player,b);
if(d<dist){nearest=b;dist=d}
}
if(nearest){
nearest.alive=false;berries++;
say('๐ You found a delicious berry!');
beep(650);
}else say('Find a berry bush and walk closer!');
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;fireZ=player.z;
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 to make a fence!');return}
wood-=3;
fences.push({x:player.x+2,z:player.z+2});
closeCraft();say('๐ชต You built a wooden fence! Nice work!');
beep(500);updateStats();
}
function addFuel(){
if(!fireBuilt){say('Build a campfire first!');return}
if(wood<1){say('You need some wood to fuel your fire!');return}
wood--;fuel++;
say('๐ฅ Your campfire is glowing brighter!');
beep(600);updateStats();
}
function feedDeer(){
if(berries<1){say('You need a berry first!');return}
if(distance(player,deer)>5){say('Get closer to the deer to offer it a berry!');return}
berries--;deer.cheer=8;deer.stun=0;
wood+=2;
say('๐ The deer eats your berry! It is happy for a little while and gives you 2 wood!');
beep(800,.2);updateStats();
}
function updateStats(){
document.getElementById('wood').textContent=wood;
document.getElementById('berries').textContent=berries;
document.getElementById('fire').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;
say('๐ You survived three nights! The grumpy deer is your forest friend!');
document.getElementById('start').style.display='flex';
document.querySelector('#start .panel').innerHTML=
'<div style="font-size:65px">๐๐ฆ๐ฅ</div><h2>Forest Champion!</h2><p>You made it through three nights! The grumpy deer is impressed!</p><button onclick="resetGame()">๐ Play Again!</button>';
}else{
say('๐ Night '+night+' is over! Keep gathering wood for the next night.');
}
updateStats();
}
function resetGame(){
wood=0;berries=0;fuel=0;night=0;
fireBuilt=false;deerCaught=0;deer={x:5,z:9,speed:1.6,stun:0,cheer:0};
player={x:0,z:0,yaw:0};
newWorld();running=true;
document.getElementById('start').style.display='none';
document.querySelector('#start .panel').innerHTML=
'<div style="font-size:65px">๐ฒ๐ฆ๐ฒ</div><h2>Forest Friends 3D</h2><p>Explore the forest, collect wood, and build a cozy campfire.</p><p>Watch out for the grumpy deer!</p><button onclick="startGame()">START ADVENTURE</button>';
say('A fresh forest adventure begins!');
updateStats();
}
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 controls
window.addEventListener('keydown',e=>{
const k=e.key.toLowerCase();
keys[k]=true;
if(['arrowup','arrowdown','arrowleft','arrowright',' '].includes(k))e.preventDefault();
});
window.addEventListener('keyup',e=>keys[e.key.toLowerCase()]=false);
window.addEventListener('blur',()=>{keys={};touch={}});
// Touch movement
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=e=>{touch[k]=false};
btn.addEventListener('pointerup',stop);
btn.addEventListener('pointercancel',stop);
btn.addEventListener('lostpointercapture',stop);
});
// Action buttons
document.getElementById('collect').onclick=collect;
document.getElementById('craft').onclick=openCraft;
document.getElementById('fuelBtn').onclick=addFuel;
document.getElementById('berryBtn').onclick=pickBerry;
// Double tap deer message / berry button can be used to feed it
document.getElementById('berryBtn').addEventListener('contextmenu',e=>{
e.preventDefault();feedDeer();
});
document.getElementById('berryBtn').addEventListener('dblclick',feedDeer);
// Keyboard action keys
window.addEventListener('keydown',e=>{
if(e.repeat)return;
if(e.key.toLowerCase()==='e')collect();
if(e.key.toLowerCase()==='c')openCraft();
if(e.key.toLowerCase()==='f')addFuel();
if(e.key.toLowerCase()==='b')pickBerry();
if(e.key.toLowerCase()==='n')nextNight();
if(e.key.toLowerCase()==='q')feedDeer();
});
</script>
</body>
</html>
Game Source: Forest Friends 3D: Grumpy Deer
Creator: LunarFlare59
Libraries: none
Complexity: complex (591 lines, 17.6 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-3d-grumpy-deer-lunarflare59" to link back to the original. Then publish at arcadelab.ai/publish.