Roguelike/RPG в 4Кб кода на JavaScript
by Vladimir
HTML
<canvas id="canvas" style="background:#eeeeee"></canvas>
CSS
* {
padding: 0;
margin: 0;
}
JavaScript
(function(elid,wi,he,exp,pot,gld,hp,lvl,cur_e,e_sz,lap,a,d){
var gen = function(){ evs=[];
for (var i=0;i<e_sz-1;i++)
evs.push( e_tp[Math.floor(Math.random()*Math.min(8,lap+2))].slice(0) );
evs.push( e_tp[8].slice(0) ); evs[Math.random()>0.7?e_sz-3:e_sz-2] = e_tp[0].slice(0);
for (var i=0;i<e_sz;i++) if (evs[i].length>8)
for (var j=8;j<12;j++) evs[i][j]=Math.floor(evs[i][j]*(lap+1)/2);}
var hit = function(){ evs[cur_e][8]-=lvl*5+a;
if (evs[cur_e][8]<=0){
alert("Monster is defeated and you got "+evs[cur_e][10]+"EXP and $"+evs[cur_e][11]);
exp+=evs[cur_e][10]; gld+=evs[cur_e][11];
while (exp>=lvl*10) { exp-=lvl*10; lvl++; alert("Level Up!") }
cur_e++; if (cur_e==e_sz) { alert("Victory! You have killed dragon!")
if (lap<7) alert("But your princess in another castle")
else if (lap==7) { alert("You saved the princess! Thank you!")
alert("But you can continue your journey and kill more dragons")
} else alert("What about one more dragon?")
cur_e=0; lap++; gen(); }
} else { hp-=Math.max(0,evs[cur_e][9]-lvl*2-d);
if(hp<=0) alert("Game Over!") } }
var use = function(){ if (pot>0){pot--;hp+=10;if(hp>100)hp=100} }
var nxt = function(){ cur_e++ }
var battle = ["Attack", "Use Potion +10HP", "Skip Battle", hit, use, nxt];
var canvas=document.querySelector(elid), ctx=canvas.getContext("2d");
canvas.width=wi; canvas.height=he; var evs=[], e_tp=[
["Shop", "Wizard provides his services", "Buy Potion $20", "Full Heal $100", "Leave",
function(){ if(gld>=20){gld-=20;pot++} },
function(){ if(gld>=100){gld-=100;hp=100} }, nxt ],
["Slime", "What the strange jelly monster?"].concat(battle,20,6,7,20),
["Goblin", "Green goblin wants to get your money"].concat(battle,50,10,15,35),
["Skeleton", "A terrible skeleton on your...