JS1k 2018

by Noncho Savov

HTML

<div id=ff style=text-align:center></div>
<p id=f style=text-align:center></p>

JavaScript

// JS1K 2018
// l:level, h:health, p:position, t:turns, g:health potion position, d:health modifier, f:display

m=(z,p)=>{//console.log(zz);
	var v = document.createElement("canvas");
  var s = (60-p*1.5), q=s/6, t=s-s/3;
  v.style.marginRight="4px";
  v.width = v.height = s;
  w = v.getContext("2d");
  var u = [
  	["1e1", , q, q, t, t],
  	["6f6", "arc", s/2,s/2,s/3,0,2*Math.PI],
  	["f88", , q, q, t, t],
  	["f00", , q, q, t, t],
  	["00f", , q, q, t, t],
    ["fff", , q, q, t, t]
  ]
  n=o=>{//draw
    w.fillStyle = "#"+o[0];
    w.beginPath();
    w[o[1]?o[1]:"rect"](o[2],o[3],o[4],o[5],o[6]);
    w.fill();
  }
  n([z?"ccc":"696", , 0, 0, s, s]);
  n(u[z]);//if(!z)n(["3c3", , s/3, s/3, s/2, s/2]);
  ff.appendChild(v);
}
a=e=>{
  k=k||e.keyCode;
  v="";d=i=0;
  ff.innerHTML="COIN: ";
  switch(k){
    case 27:v="Game Over!";break;//b(5);//restart
    case 38:p-=l*2;
    case 40:p+=l+1;
    case 37:p--;
    default:p--;
    case 39:
      t++;p++;if(p<1)p=1;
      k=Math.random();
      t%2&&p%2&&(v+="HIT ",d=-l*k>>0);
      p>l*l&&(l++,p=1,g=l+t*k>>0);
      p==g&&(g=0,d+=5+k*l>>0,v+="HP+");
      k<l/(99+l*5)?(k=l*k*5>>0,v="BAT "+v,d-=k,k=32):k=0;
      for(h+=d;i<=l*l;i++){
      	m((i==p?0:i==g?1:32==k?2:i%2?t%2?3:4:5),l);
        if(i/l==i/l>>0) ff.appendChild(document.createElement("br"));
      }
      if(h<=0)k=27
  }
  f.innerHTML=v+(d||"")+"<br>L:"+(l-4)+" M:"+t+" HP:"+h
}
b=c=>{
  l=c;
  p=t=k=0;
  g=h=13;
  onkeydown=a;
  a(0)
}
b(5)