Rpg2
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Realm of Eldoria</title>
<style>
*{box-sizing:border-box}
body{margin:0;background:#101522;color:white;font-family:Arial;overflow:hidden}
canvas{display:block;width:100vw;height:100vh;background:#243d32}
#hud{position:absolute;top:10px;left:10px;background:#000b;padding:12px;border:1px solid #c9a45d;border-radius:10px;width:190px;font-size:13px}
.bar{height:10px;background:#333;margin:4px 0 9px;border-radius:10px;overflow:hidden}
.fill{height:100%;width:100%}
#hp{background:#e34b4b}#mp{background:#3999ec}#xp{background:#e8c54d}
#quest{position:absolute;right:10px;top:10px;background:#000b;border:1px solid #c9a45d;border-radius:10px;padding:12px;width:200px;font-size:13px}
#message{position:absolute;bottom:100px;left:50%;transform:translateX(-50%);background:#000c;padding:12px 20px;border-radius:8px;display:none;text-align:center}
#buttons{position:absolute;bottom:12px;left:50%;transform:translateX(-50%);display:flex;gap:7px;flex-wrap:wrap;justify-content:center;width:95%}
button{background:#644522;color:white;border:1px solid #d9b16b;padding:12px;border-radius:8px;font-weight:bold}
#screen{position:absolute;inset:0;background:#07101dee;display:flex;align-items:center;justify-content:center;text-align:center}
.panel{background:#151e2b;border:2px solid #c9a45d;border-radius:15px;padding:25px;width:min(90%,450px)}
h1{color:#f1ca75}
#death{display:none}
#hint{position:absolute;bottom:75px;right:12px;font-size:11px;text-align:right}
</style>
</head>
<body>
<canvas id="game"></canvas>
<div id="hud">
<b>HERO OF ELDORIA</b>
<p>Level: <span id="level">1</span></p>
Health <span id="healthText">100/100</span>
<div class="bar"><div id="hp" class="fill"></div></div>
Mana <span id="manaText">50/50</span>
<div class="bar"><div id="mp" class="fill"></div></div>
XP <span id="xpText">0/100</span>
<div class="bar"><div id="xp" class="fill"></div></div>
Gold: <span id="gold">0</span>
<br>Crystals: <span id="crystals">0</span>/5
</div>
<div id="quest">
<b style="color:#f1ca75">QUEST: LOST CRYSTALS</b>
<p id="questText">Find the elder near the village.</p>
<hr>
Explore Eldoria, defeat monsters and discover treasures.
</div>
<div id="message"></div>
<div id="hint">
WASD: Move<br>
Mouse: Look<br>
SPACE: Attack<br>
Q: Magic<br>
E: Interact
</div>
<div id="buttons">
<button onclick="attack()">⚔ ATTACK</button>
<button onclick="magic()">✨ MAGIC</button>
<button onclick="interact()">INTERACT</button>
<button onclick="save()">SAVE</button>
<button onclick="load()">LOAD</button>
</div>
<div id="screen">
<div class="panel">
<h1>REALM OF ELDORIA</h1>
<h3>The Lost Kingdom</h3>
<p>A fantasy adventure awaits.</p>
<p>Explore the kingdom, defeat monsters, collect five crystals and save the village.</p>
<p>WASD to move | Mouse to look</p>
<p>SPACE to attack | Q for magic | E to interact</p>
<button onclick="start()">ENTER KINGDOM</button>
</div>
</div>
<div id="death">
<div class="panel">
<h1>You Have Fallen</h1>
<p>Your adventure is not over.</p>
<button onclick="respawn()">RESPAWN</button>
</div>
</div>
<script>
const canvas=document.getElementById("game");
const ctx=canvas.getContext("2d");
let W,H;
function resize(){
W=canvas.width=innerWidth;
H=canvas.height=innerHeight;
}
resize();
window.addEventListener("resize",resize);
let started=false,dead=false;
let health=100,mana=50,xp=0,level=1,gold=0,crystals=0;
let yaw=0;
let keys={};
let messageTimeout;
const player={x:0,z:0};
const elder={x:3,z:9};
let enemies=[];
let items=[];
let chests=[];
let particles=[];
function random(min,max){return Math.random()*(max-min)+min}
function createWorld(){
enemies=[];
items=[];
chests=[];
for(let i=0;i<16;i++){
enemies.push({
x:random(-35,35),
z:random(-45,45),
hp:50,
alive:true,
cooldown:0
});
}
for(let i=0;i<10;i++){
items.push({
x:random(-35,35),
z:random(-40,40),
collected:false
});
}
for(let i=0;i<7;i++){
chests.push({
x:random(-35,35),
z:random(-40,40),
opened:false
});
}
}
createWorld();
function say(text){
const m=document.getElementById("message");
m.textContent=text;
m.style.display="block";
clearTimeout(messageTimeout);
messageTimeout=setTimeout(()=>m.style.display="none",2500);
}
function start(){
started=true;
document.getElementById("screen").style.display="none";
say("Welcome to Eldoria, hero!");
}
document.addEventListener("keydown",e=>{
keys[e.code]=true;
if(e.code==="Space"){e.preventDefault();attack()}
if(e.code==="KeyQ")magic();
if(e.code==="KeyE")interact();
});
document.addEventListener("keyup",e=>keys[e.code]=false);
canvas.addEventListener("click",()=>{
if(started && !dead)canvas.requestPointerLock?.();
});
document.addEventListener("mousemove",e=>{
if(document.pointerLockElement===canvas){
yaw+=e.movementX*.003;
}
});
function updateHUD(){
document.getElementById("healthText").textContent=Math.ceil(health)+"/100";
document.getElementById("manaText").textContent=Math.floor(mana)+"/50";
document.getElementById("xpText").textContent=xp+"/"+(level*100);
document.getElementById("level").textContent=level;
document.getElementById("gold").textContent=gold;
document.getElementById("crystals").textContent=crystals;
document.getElementById("hp").style.width=health+"%";
document.getElementById("mp").style.width=mana*2+"%";
document.getElementById("xp").style.width=(xp/(level*100)*100)+"%";
if(crystals>=5){
document.getElementById("questText").textContent="Return to the elder to complete the quest!";
}
}
function attack(){
if(!started||dead)return;
let hit=false;
enemies.forEach(e=>{
if(!e.alive)return;
let dx=e.x-player.x;
let dz=e.z-player.z;
let distance=Math.hypot(dx,dz);
let angle=Math.atan2(dx,dz);
let difference=Math.atan2(Math.sin(angle-yaw),Math.cos(angle-yaw));
if(distance<6&&Math.abs(difference)<1.1){
e.hp-=25;
hit=true;
if(e.hp<=0){
e.alive=false;
xp+=35;
gold+=10;
say("Monster defeated! +35 XP and 10 gold.");
if(xp>=level*100){
xp-=level*100;
level++;
health=100;
mana=50;
say("LEVEL UP! Level "+level);
}
}
}
});
if(!hit)say("Your sword missed!");
updateHUD();
}
function magic(){
if(!started||dead)return;
if(mana<10){say("Not enough mana!");return}
mana-=10;
let target=null,best=15;
enemies.forEach(e=>{
if(!e.alive)return;
let d=Math.hypot(e.x-player.x,e.z-player.z);
if(d<best){
best=d;
target=e;
}
});
if(target){
target.hp-=50;
if(target.hp<=0){
target.alive=false;
xp+=35;
gold+=10;
say("Magic defeated the monster!");
}else{
say("Magic hit the monster!");
}
}else{
say("Your magic flew into the distance!");
}
updateHUD();
}
function interact(){
if(!started||dead)return;
if(Math.hypot(player.x-elder.x,player.z-elder.z)<5){
if(crystals>=5){
gold+=100;
xp+=100;
crystals=0;
say("Quest completed! The elder rewards you with 100 gold!");
document.getElementById("questText").textContent="Quest completed! You saved the village.";
}else{
say("Elder: Please collect five magical crystals!");
}
updateHUD();
return;
}
for(let c of chests){
if(!c.opened&&Math.hypot(c.x-player.x,c.z-player.z)<4){
c.opened=true;
gold+=25;
say("Treasure found! +25 gold!");
updateHUD();
return;
}
}
say("Nothing nearby to interact with.");
}
function save(){
try{
localStorage.setItem("eldoriaOffline",JSON.stringify({
player,health,mana,xp,level,gold,crystals
}));
say("Game saved!");
}catch(e){say("Unable to save game.");}
}
function load(){
try{
let data=JSON.parse(localStorage.getItem("eldoriaOffline"));
if(!data){say("No saved game!");return}
Object.assign(player,data.player);
health=data.health;
mana=data.mana;
xp=data.xp;
level=data.level;
gold=data.gold;
crystals=data.crystals;
updateHUD();
say("Game loaded!");
}catch(e){say("Unable to load save.");}
}
function respawn(){
health=100;
mana=50;
player.x=0;
player.z=0;
dead=false;
enemies.forEach(e=>{
if(!e.alive){e.alive=true;e.hp=50}
});
document.getElementById("death").style.display="none";
updateHUD();
say("You have returned!");
}
function move(dt){
let speed=10*dt;
let forward=0,side=0;
if(keys.KeyW||keys.ArrowUp)forward++;
if(keys.KeyS||keys.ArrowDown)forward--;
if(keys.KeyA||keys.ArrowLeft)side--;
if(keys.KeyD||keys.ArrowRight)side++;
let fx=-Math.sin(yaw);
let fz=-Math.cos(yaw);
let rx=Math.cos(yaw);
let rz=-Math.sin(yaw);
player.x+=(fx*forward+rx*side)*speed;
player.z+=(fz*forward+rz*side)*speed;
player.x=Math.max(-48,Math.min(48,player.x));
player.z=Math.max(-48,Math.min(48,player.z));
}
function updateEnemies(dt){
enemies.forEach(e=>{
if(!e.alive)return;
let dx=player.x-e.x;
let dz=player.z-e.z;
let d=Math.hypot(dx,dz);
if(d<18&&d>1.5){
e.x+=(dx/d)*dt*2;
e.z+=(dz/d)*dt*2;
}
e.cooldown-=dt;
if(d<2&&e.cooldown<=0){
health-=8;
e.cooldown=1;
if(health<=0){
health=0;
dead=true;
document.getElementById("death").style.display="flex";
}
updateHUD();
}
});
}
function updateItems(){
items.forEach(i=>{
if(i.collected)return;
if(Math.hypot(i.x-player.x,i.z-player.z)<2){
i.collected=true;
crystals++;
gold+=5;
say("Crystal collected! +5 gold.");
updateHUD();
}
});
}
function project(x,z,height=0){
let dx=x-player.x;
let dz=z-player.z;
let rx=dx*Math.cos(yaw)-dz*Math.sin(yaw);
let rz=dx*Math.sin(yaw)+dz*Math.cos(yaw);
if(rz<0.5)return null;
let scale=Math.min(H, W)*.8/rz;
return {
x:W/2+rx*scale,
y:H*.48-height*scale,
scale:scale,
depth:rz
};
}
function polygon(points,color){
ctx.fillStyle=color;
ctx.beginPath();
points.forEach((p,i)=>{
if(i===0)ctx.moveTo(p.x,p.y);
else ctx.lineTo(p.x,p.y);
});
ctx.closePath();
ctx.fill();
}
function drawGround(){
let horizon=H*.45;
let sky=ctx.createLinearGradient(0,0,0,horizon);
sky.addColorStop(0,"#4c80a2");
sky.addColorStop(1,"#b3c5a0");
ctx.fillStyle=sky;
ctx.fillRect(0,0,W,horizon);
ctx.fillStyle="#426e3c";
ctx.fillRect(0,horizon,W,H-horizon);
polygon([
{x:W*.43,y:horizon},
{x:W*.57,y:horizon},
{x:W*.85,y:H},
{x:W*.15,y:H}
],"#907c55");
// Horizon mountains
for(let i=0;i<12;i++){
let x=i*W/10;
polygon([
{x:x-100,y:horizon},
{x:x+60,y:horizon-100-random(0,40)},
{x:x+200,y:horizon}
],"#65746a");
}
}
function drawObject(x,z,height,color,size,shape="circle"){
let p=project(x,z,height);
if(!p)return;
let s=Math.max(2,Math.min(180,size*p.scale*.02));
ctx.fillStyle=color;
if(shape==="tree"){
ctx.fillStyle="#624329";
ctx.fillRect(p.x-s*.12,p.y,s*.24,s*1.1);
ctx.fillStyle="#17552c";
ctx.beginPath();
ctx.arc(p.x,p.y-s*.25,s*.7,0,Math.PI*2);
ctx.fill();
}else{
ctx.beginPath();
ctx.arc(p.x,p.y,s,0,Math.PI*2);
ctx.fill();
}
}
function drawEnemy(e){
if(!e.alive)return;
let p=project(e.x,e.z,0);
if(!p)return;
let s=Math.max(3,Math.min(100,150/p.depth*5));
ctx.fillStyle="#491b22";
ctx.fillRect(p.x-s*.35,p.y-s*1.5,s*.7,s*1.5);
ctx.fillStyle="#ad3434";
ctx.beginPath();
ctx.arc(p.x,p.y-s*1.7,s*.45,0,Math.PI*2);
ctx.fill();
ctx.fillStyle="#fff";
ctx.fillRect(p.x-s*.2,p.y-s*1.8,s*.1,s*.1);
ctx.fillRect(p.x+s*.1,p.y-s*1.8,s*.1,s*.1);
ctx.fillStyle="#222";
ctx.fillRect(p.x-s*.5,p.y-s*2.3,s,s*.12);
ctx.fillStyle="#e44";
ctx.fillRect(p.x-s*.5,p.y-s*2.3,s*(e.hp/50),s*.12);
}
function drawElder(){
let p=project(elder.x,elder.z,0);
if(!p)return;
let s=Math.max(4,Math.min(80,150/p.depth*5));
ctx.fillStyle="#d6a82e";
ctx.fillRect(p.x-s*.3,p.y-s*1.5,s*.6,s*1.5);
ctx.fillStyle="#e9b78c";
ctx.beginPath();
ctx.arc(p.x,p.y-s*1.7,s*.35,0,Math.PI*2);
ctx.fill();
ctx.fillStyle="#fff";
ctx.font="bold 12px Arial";
ctx.fillText("ELDER",p.x-20,p.y-s*2.2);
}
function drawItems(){
items.forEach(i=>{
if(i.collected)return;
let p=project(i.x,i.z,1);
if(!p)return;
let s=Math.max(3,Math.min(30,120/p.depth*4));
ctx.fillStyle="#5bf1ff";
ctx.shadowBlur=15;
ctx.shadowColor="#00eaff";
polygon([
{x:p.x,y:p.y-s},
{x:p.x+s*.6,y:p.y},
{x:p.x,y:p.y+s},
{x:p.x-s*.6,y:p.y}
],"#55eaff");
ctx.shadowBlur=0;
});
}
function drawChests(){
chests.forEach(c=>{
let p=project(c.x,c.z,0);
if(!p)return;
let s=Math.max(3,Math.min(35,100/p.depth*5));
ctx.fillStyle=c.opened?"#555":"#a76a29";
ctx.fillRect(p.x-s/2,p.y-s,s,s*.7);
ctx.strokeStyle="#f3d16d";
ctx.strokeRect(p.x-s/2,p.y-s,s,s*.7);
});
}
function drawVillage(){
for(let i=0;i<5;i++){
let x=(i%2===0?-12:12);
let z=-10+Math.floor(i/2)*12;
let p=project(x,z,0);
if(!p)continue;
let s=Math.max(4,Math.min(120,250/p.depth*5));
ctx.fillStyle="#b68b5b";
ctx.fillRect(p.x-s/2,p.y-s*1.5,s,s*1.5);
polygon([
{x:p.x-s*.65,y:p.y-s*1.5},
{x:p.x,y:p.y-s*2.2},
{x:p.x+s*.65,y:p.y-s*1.5}
],"#773a2a");
}
}
function draw(){
drawGround();
drawVillage();
// Trees around the world
for(let i=0;i<55;i++){
let angle=i*2.399;
let radius=20+(i%8)*5;
drawObject(Math.cos(angle)*radius,Math.sin(angle)*radius,0,"#18552c",2,"tree");
}
drawChests();
drawItems();
drawElder();
enemies.sort((a,b)=>{
return Math.hypot(b.x-player.x,b.z-player.z)-
Math.hypot(a.x-player.x,a.z-player.z);
});
enemies.forEach(drawEnemy);
// Player hands and weapon
ctx.fillStyle="#263e91";
ctx.fillRect(W/2-18,H*.72,36,70);
ctx.fillStyle="#e5b48b";
ctx.fillRect(W/2-35,H*.76,15,30);
ctx.fillStyle="#dce4ed";
ctx.save();
ctx.translate(W/2+28,H*.76);
ctx.rotate(-.25);
ctx.fillRect(-4,-55,8,75);
ctx.restore();
// Crosshair
ctx.strokeStyle="#ffffffaa";
ctx.beginPath();
ctx.moveTo(W/2-7,H/2);
ctx.lineTo(W/2+7,H/2);
ctx.moveTo(W/2,H/2-7);
ctx.lineTo(W/2,H/2+7);
ctx.stroke();
}
let last=performance.now();
function loop(now){
let dt=Math.min((now-last)/1000,.05);
last=now;
if(started&&!dead){
move(dt);
updateEnemies(dt);
updateItems();
mana=Math.min(50,mana+dt*2);
updateHUD();
}
draw();
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
</script>
</body>
</html>
Featured
Advertisement
Category
Action