🎮ArcadeLab

SNAKE WILD 100

by ChromeFlame24
409 lines8.4 KB
▶ Play

<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>SNAKE WILD 100</title>
<style>
*{box-sizing:border-box}
body{
 margin:0;background:#101b16;color:white;
 font-family:Arial,sans-serif;text-align:center;
 touch-action:manipulation;
}
header{padding:12px}
h1{color:#83e66b;margin:8px;font-size:27px}
.stats{
 display:flex;justify-content:center;gap:12px;
 flex-wrap:wrap;margin:10px;
}
.stat{
 background:#21382a;padding:10px 16px;
 border-radius:12px;border:1px solid #43664b;
}
canvas{
 display:block;margin:10px auto;
 width:min(94vw,500px);
 aspect-ratio:1;
 background:#397a43;
 border:5px solid #8acb68;
 border-radius:8px;
 image-rendering:pixelated;
}
button{
 border:0;border-radius:12px;padding:12px 19px;
 margin:5px;background:#76d85d;color:#102113;
 font-size:16px;font-weight:bold;
}
.controls{
 display:grid;grid-template-columns:repeat(3,60px);
 justify-content:center;gap:6px;margin:12px;
}
.controls button{height:52px;padding:0;font-size:23px}
.info{color:#c2d7bd;font-size:13px;margin:10px}
#message{color:#ffe58c;font-weight:bold;min-height:24px}
</style>
</head>
<body>

<header>
<h1>🐍 SNAKE WILD 100</h1>
<div>عالم الأفاعي البري 🌿</div>
</header>

<div class="stats">
 <div class="stat">النقاط: <span id="score">0</span></div>
 <div class="stat">الأفضل: <span id="best">0</span></div>
 <div class="stat">الأفعى: <span id="level">1</span>/100</div>
</div>

<canvas id="game" width="480" height="480"></canvas>

<div id="message">اجمع التفاح والأفاعي الصغيرة!</div>

<div>
<button onclick="start()">▶ ابدأ اللعب</button>
<button onclick="pause()">⏸ إيقاف</button>
<button onclick="mute()">🔊 الصوت</button>
</div>

<div class="controls">
 <span></span>
 <button onclick="turn(0,-1)">⬆️</button>
 <span></span>
 <button onclick="turn(-1,0)">⬅️</button>
 <button onclick="turn(0,1)">⬇️</button>
 <button onclick="turn(1,0)">➡️</button>
</div>

<div class="info">
استخدم الأسهم أو أزرار الهاتف للتحكم بالأفعى.
<br>كلما أكلت أكثر، أصبحت اللعبة أسرع!
</div>

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

const SIZE=24;
const CELL=20;
const TOTAL=SIZE*SIZE;

let snake,apple,babies,dir,nextDir;
let score=0,best=Number(localStorage.getItem("wildBest")||0);
let running=false,gameOver=false;
let timer=null,muted=false;
let terrain=0;
let skins=Array.from({length:100},(_,i)=>i);

document.getElementById("best").textContent=best;

const colors=[
 "#63d94f","#ffb52e","#55d9db","#d36bff",
 "#f46b6b","#e7df72","#64a4ff","#ff83c6",
 "#c6ec8b","#ef9148"
];

function sound(freq=500){
 if(muted)return;
 try{
  const a=new AudioContext();
  const o=a.createOscillator();
  const g=a.createGain();
  o.connect(g);g.connect(a.destination);
  o.frequency.value=freq;
  g.gain.value=.04;
  o.start();
  o.stop(a.currentTime+.09);
 }catch(e){}
}

function randomCell(){
 return {
  x:Math.floor(Math.random()*SIZE),
  y:Math.floor(Math.random()*SIZE)
 };
}

function occupied(p){
 return snake.some(s=>s.x===p.x&&s.y===p.y)||
 babies.some(b=>b.x===p.x&&b.y===p.y);
}

function newApple(){
 let p;
 do{p=randomCell()}while(occupied(p));
 return p;
}

function newBaby(){
 let p;
 do{p=randomCell()}while(occupied(p)||
 (apple.x===p.x&&apple.y===p.y));
 return {...p,dx:Math.random()<.5?1:-1,dy:0};
}

function start(){
 clearInterval(timer);
 snake=[
  {x:11,y:12},{x:10,y:12},{x:9,y:12}
 ];
 dir={x:1,y:0};
 nextDir={x:1,y:0};
 score=0;
 babies=[];
 apple=newApple();
 for(let i=0;i<3;i++)babies.push(newBaby());
 terrain=0;
 running=true;
 gameOver=false;
 updateUI();
 document.getElementById("message").textContent="انطلق في الغابة! 🌿";
 loop();
}

function pause(){
 if(gameOver)return;
 running=!running;
 if(running){
  document.getElementById("message").textContent="اللعب مستمر!";
  loop();
 }else{
  clearInterval(timer);
  document.getElementById("message").textContent="اللعبة متوقفة مؤقتاً";
 }
}

function mute(){
 muted=!muted;
 document.getElementById("message").textContent=
 muted?"الصوت مغلق":"الصوت يعمل";
}

function turn(x,y){
 if(x===-dir.x&&y===-dir.y)return;
 nextDir={x,y};
}

function updateUI(){
 document.getElementById("score").textContent=score;
 document.getElementById("best").textContent=best;
 document.getElementById("level").textContent=
 Math.min(100,Math.floor(score/5)+1);
}

function drawBackground(){
 let palettes=[
  ["#397b43","#438c4b"],
  ["#285c37","#397347"],
  ["#527c45","#658d4d"],
  ["#397d79","#4b9690"],
  ["#77734a","#918451"]
 ];
 let c=palettes[terrain%palettes.length];

 ctx.fillStyle=c[0];
 ctx.fillRect(0,0,480,480);

 for(let y=0;y<SIZE;y++){
  for(let x=0;x<SIZE;x++){
   let n=(x*17+y*31)%9;
   if(n===0){
    ctx.fillStyle=c[1];
    ctx.fillRect(x*CELL+3,y*CELL+3,4,4);
   }
   if(n===1){
    ctx.fillStyle="#ffffff12";
    ctx.fillRect(x*CELL+12,y*CELL+9,3,3);
   }
  }
 }
}

function drawApple(){
 let x=apple.x*CELL,y=apple.y*CELL;
 ctx.fillStyle="#ec3434";
 ctx.fillRect(x+4,y+5,12,12);
 ctx.fillStyle="#ff7474";
 ctx.fillRect(x+5,y+6,4,4);
 ctx.fillStyle="#65d45c";
 ctx.fillRect(x+12,y+1,6,5);
}

function drawSnake(){
 let skin=Math.min(99,Math.floor(score/5));
 let color=colors[skin%colors.length];

 snake.forEach((s,i)=>{
  let x=s.x*CELL,y=s.y*CELL;
  ctx.fillStyle=i===0?"#d6ff8c":color;
  ctx.fillRect(x+1,y+1,18,18);

  ctx.fillStyle="#173a20";
  if(i===0){
   ctx.fillRect(x+4,y+4,4,4);
   ctx.fillRect(x+12,y+4,4,4);
  }else{
   ctx.fillRect(x+5,y+5,3,3);
  }
  ctx.fillStyle="#ffffff";
  if(i===0){
   ctx.fillRect(x+5,y+5,2,2);
   ctx.fillRect(x+13,y+5,2,2);
  }
 });
}

function drawBabies(){
 babies.forEach(b=>{
  let x=b.x*CELL,y=b.y*CELL;
  ctx.fillStyle="#a9ecff";
  ctx.fillRect(x+4,y+4,12,12);
  ctx.fillStyle="#18465c";
  ctx.fillRect(x+6,y+7,2,2);
  ctx.fillRect(x+12,y+7,2,2);
 });
}

function draw(){
 drawBackground();
 drawApple();
 drawBabies();
 drawSnake();
}

function moveBabies(){
 babies.forEach(b=>{
  if(Math.random()<.35){
   let dirs=[
    {x:1,y:0},{x:-1,y:0},
    {x:0,y:1},{x:0,y:-1}
   ];
   let d=dirs[Math.floor(Math.random()*4)];
   b.dx=d.x;b.dy=d.y;
  }

  b.x=(b.x+b.dx+SIZE)%SIZE;
  b.y=(b.y+b.dy+SIZE)%SIZE;
 });
}

function step(){
 if(!running)return;

 dir=nextDir;

 let head={
  x:(snake[0].x+dir.x+SIZE)%SIZE,
  y:(snake[0].y+dir.y+SIZE)%SIZE
 };

 let ateApple=head.x===apple.x&&head.y===apple.y;

 let babyIndex=babies.findIndex(b=>
  b.x===head.x&&b.y===head.y
 );

 let grow=ateApple||babyIndex!==-1;

 let body=grow?snake:snake.slice(0,-1);

 if(body.some(s=>s.x===head.x&&s.y===head.y)){
  endGame();return;
 }

 snake.unshift(head);

 if(ateApple){
  score+=1;
  apple=newApple();
  sound(650);
  document.getElementById("message").textContent=
   "🍎 تفاحة لذيذة! +1";
 }else if(babyIndex!==-1){
  score+=3;
  babies.splice(babyIndex,1);
  babies.push(newBaby());
  sound(850);
  document.getElementById("message").textContent=
   "🐍 أكلت أفعى صغيرة! +3";
 }else{
  snake.pop();
 }

 if(score>best){
  best=score;
  localStorage.setItem("wildBest",best);
 }

 if(score>0&&score%10===0){
  terrain=Math.floor(score/10);
 }

 if(snake.length>250){
  snake.length=250;
 }

 updateUI();
 draw();

 clearInterval(timer);
 timer=setInterval(step,
 Math.max(65,150-score*1.3));
}

function loop(){
 clearInterval(timer);
 timer=setInterval(step,
 Math.max(65,150-score*1.3));
 draw();
}

function endGame(){
 running=false;
 gameOver=true;
 clearInterval(timer);
 sound(160);
 document.getElementById("message").textContent=
 "انتهت الجولة! نتيجتك: "+score+" 🏆";
 draw();
}

document.addEventListener("keydown",e=>{
 const keys={
 ArrowUp:[0,-1],w:[0,-1],
 ArrowDown:[0,1],s:[0,1],
 ArrowLeft:[-1,0],a:[-1,0],
 ArrowRight:[1,0],d:[1,0]
 };
 if(keys[e.key]){
  e.preventDefault();
  turn(...keys[e.key]);
 }
 if(e.key===" "){
  e.preventDefault();
  if(gameOver)start();else pause();
 }
});

let startX=0,startY=0;
canvas.addEventListener("touchstart",e=>{
 startX=e.touches[0].clientX;
 startY=e.touches[0].clientY;
},{passive:true});

canvas.addEventListener("touchend",e=>{
 let dx=e.changedTouches[0].clientX-startX;
 let dy=e.changedTouches[0].clientY-startY;
 if(Math.max(Math.abs(dx),Math.abs(dy))<20)return;
 if(Math.abs(dx)>Math.abs(dy))
  turn(dx>0?1:-1,0);
 else
  turn(0,dy>0?1:-1);
},{passive:true});

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

Game Source: SNAKE WILD 100

Creator: ChromeFlame24

Libraries: none

Complexity: complex (409 lines, 8.4 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: snake-wild-100-chromeflame24" to link back to the original. Then publish at arcadelab.ai/publish.