Chewy the Crispy Crunch
Crunch, not Crotch
Hey, I'm Crunch.
So a few weeks ago I started writing a game with JavaScript. The basic idea is, you and a CPU player fight against each other. You and the enemy can attack or heal each turn, and sometimes, you'll get a critical hit. If the CPU's HP is below 100, it can go into Berserk Mode and do more damage.
Now the problem is this: for some reason, the code for when the enemy tries to heal itself doesn't work. Here's the HTML code:
and here's the JavaScript:
So a few weeks ago I started writing a game with JavaScript. The basic idea is, you and a CPU player fight against each other. You and the enemy can attack or heal each turn, and sometimes, you'll get a critical hit. If the CPU's HP is below 100, it can go into Berserk Mode and do more damage.
Now the problem is this: for some reason, the code for when the enemy tries to heal itself doesn't work. Here's the HTML code:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>
The Game
</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
body {
font-family:"Calibri";
background:#9a92ff;
text-align:center;
font-size:30px;
}
#left {
width:370px;
height:200px;
margin-left:300px;
margin-top:250px;
background:#9a92ee;
border:5px inset #c0c0c0;
float:left;
overflow:auto;
text-align:left;
}
#right {
width:370px;
height:200px;
margin-right:300px;
margin-top:250px;
background:#9a92ee;
border:5px inset #c0c0c0;
float:right;
overflow:auto;
text-align:right;
}
</style>
</head>
<body>
<div id="left">
<span id="playerHealth"></span>
<br />
<span id="playerName"></span>
<hr />
<span id="playerItems"></span>
</div>
<div id="right">
<span id="enemyHealth"></span>
<br />
<span id="enemyName"></span>
<hr />
<span id="enemyItems"></span>
</div>
<p id="choices">
<a onclick="playerAttack();">Attack</a> <a onclick="playerHeal();">Heal</a> <a onclick="location.reload(1);">Restart</a>
</p>
<script src="theGame.js">
</script>
</body>
</html>
The HTML file for the game is here.playerName = prompt("Please choose a name for yourself.","Your Name Here");
enemyName = prompt("Please choose a name for the enemy.","Enemy Name Here");
playerItems = parseInt(prompt("Please insert number of potions for you."));
enemyItems = parseInt(prompt("Please insert number of potions for the enemy."));
playerHealth = 500;
enemyHealth = 500;
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
document.getElementById("enemyHealth").innerHTML = enemyHealth + "/500";
document.getElementById("playerName").innerHTML = playerName;
document.getElementById("enemyName").innerHTML = enemyName;
document.getElementById("playerItems").innerHTML = " Potions (" + playerItems + ")";
document.getElementById("enemyItems").innerHTML = "Potions (" + enemyItems + ") ";
function playerAttack() {
alert("It is your turn!");
playerDamage = Math.ceil(100 * Math.random());
playerCrit = Math.ceil(8 * Math.random());
if (playerCrit == 8)
{
playerDamage = 2 * playerDamage;
alert("Critical hit!");
}
alert(playerName + " did " + playerDamage + " damage!");
enemyHealth = enemyHealth - playerDamage;
if (enemyHealth <= 0)
{
playerWin ();
}
else
{
document.getElementById("enemyHealth").innerHTML = enemyHealth + "/500";
enemyAttack();
}
}
function playerWin() {
document.getElementById("enemyHealth").innerHTML = "0/500";
alert("You win!");
document.getElementById("choices").innerHTML = "You win!<br \/><a onclick=\"location.reload(1);\">Play again?<\/a>";
}
function enemyWin() {
document.getElementById("playerHealth").innerHTML = "0/500";
alert("You lose!");
document.getElementById("choices").innerHTML = "You lose!<br \/><a onclick=\"location.reload(1);\">Play again?<\/a>";
}
function playerHeal() {
alert("It is your turn!");
if (playerItems == 0)
{
alert(playerName + " is out of potions!");
enemyAttack();
}
else
{
playerPotion = Math.ceil(100 * Math.random());
alert(playerName + " healed " + playerPotion + " health!");
playerItems = playerItems - 1;
document.getElementById("playerItems").innerHTML = " Potions (" + playerItems + ")";
playerHealth = playerHealth + playerPotion;
if (playerHealth >= 500)
{
playerHealth = 500;
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
enemyAttack();
}
else
{
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
enemyAttack();
}
}
}
function enemyAttack() {
alert("It is the enemy's turn!");
enemyHeal = Math.ceil(2 * Math.random());
enemyDamage = Math.ceil(100 * Math.random());
enemyCrit = Math.ceil(8 * Math.random());
enemyBerserk = Math.ceil(2 * Math.random());
if (enemyHeal == 2)
{
enemyHeal();
}
else
{
if (enemyHealth <= 100)
{
if (enemyBerserk == 2)
{
enemyDamage = 5 * enemyDamage;
alert(enemyName + " went berserk!");
if (enemyCrit == 8)
{
enemyDamage = 2 * enemyDamage;
alert("Critical hit!");
}
alert(enemyName + " did " + enemyDamage + " damage!");
playerHealth = playerHealth - enemyDamage;
if (playerHealth <= 0)
{
enemyWin();
}
else
{
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
}
}
else
{
if (enemyCrit == 8)
{
enemyDamage = 2 * enemyDamage;
alert("Critical hit!");
}
alert(enemyName + " did " + enemyDamage + " damage!");
playerHealth = playerHealth - enemyDamage;
if (playerHealth <= 0)
{
enemyWin();
}
else
{
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
}
}
}
else
{
if (enemyCrit == 8)
{
enemyDamage = 2 * enemyDamage;
alert("Critical hit!");
}
alert(enemyName + " did " + enemyDamage + " damage!");
playerHealth = playerHealth - enemyDamage;
if (playerHealth <= 0)
{
enemyWin();
}
else
{
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
}
}
}
}
function enemyHeal() {
if (enemyItems == 0)
{
alert(enemyItems + " is out of potions!");
}
else
{
enemyPotion = Math.ceil(100 * Math.random());
alert(enemyName + "healed " + enemyPotion + " health!");
enemyItems = enemyItems - 1;
document.getElementById("enemyItems").innerHTML = "Potions (" + enemyItems + ") ";
enemyHealth = enemyHealth + enemyPotion;
if (enemyHealth >= 500)
{
enemyHealth = 500;
document.getElementById("enemyHealth").innerHTML = enemyHealth + "/500";
}
else
{
document.getElementById("enemyHealth").innerHTML = enemyHealth + "/500";
}
}
}