<!DOCTYPE html>
<html lang="en">
<meta charset="UTF8">
<meta name="viewport"content="width=device-width,intial-scale=1.0">
<title>Simple rpg Game</title>
<script src="youxi.js" type="text/javascript">
</script>
</head>
<body>
<h2>
角色游戏
</h2>
<div id="player">
<h3>角色</h3>
<p>攻击力<span id="playerAttack">10</span></p>
</div>
<div id="monster">
<h3>怪物</h3>
<p>生命值<span id="monsterHealth">100</span></p>
</div>
<div>玩家生命值;<span id="playerHealth">100</span></div>
<button οnclick="attackMonster()">攻击怪物</button>
</body>
</html>这是html
下面是js
function attackMonster(){
var monsterHealth=parseInt(document.getElementById("monsterHealth").innerText);
var newMonsterHealth=monsterHealth-10;
document.getElementById("monsterHealth").innerText=newMonsterHealth;
if(newMonsterHealth<0){
newMonsterHealth=0;
}
document.getElementById("moansterHealth").innerText=newMonsterHealth;
if(Math.random()<0.2){
monsterAttacks();}
if(newMonsterHealth<=0){
alert("怪物已被打败");
}
}
function monsterAttacks(){
var playerHealth=parseInt(document.getElementById("playerHealth").innerText);
var monsterAttackPower=
Math.floor(Math.random()*11)+5;
var newPlayerHealth=playerHealth-monsterAttackPower;
if{newPlayerHealth<0){
mewPlayerHealth=0;}
document.getElementById("playerHealth").innerText=newPlayerHealth;
if(newpPlayerHealth<=0){
alert("你被打败了!游戏结束。");
}
}