I'm learning JavaScript and I'm currently trying to make a simple text game on the alert
window. By just starting JavaScript I already know the basics like if else var function
and more... I typed my codes and everything works fine until there is a transition to another function that is fight()
and when it comes no more alert
windows shows up, nor the game continues. It may also be that I should add something to HTML, but I do not know what... I checked that I did not make a mistake and mistakenly wrote some code but I'm sure I wrote well.
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function startGame() {
var start = prompt("1. Start / 2. Exit");
if (start == "1") {
game();
} else if (start == "2") {
alert("Goodbye!");
exit();
}
}
function game() {
var playerName = prompt("What is your name?");
var playerHealth = 100;
var playerDamage = 7;
var playerClass = "Farmer";
var playerGold = 50;
alert("You are a normal farmer, dreamed a lot about adventuring! Now you want to become adventurer!");
alert("You start from nothing, and everyone hope you become something!");
alert("You have " + playerHealth + " health, " + playerDamage + " damage and " + playerGold + " gold!");
var choice = prompt("1. Walk / 2. Talk");
if (choice == "1") {
alert("You are walking around the town...");
var banditEvent = Math.floor(Math.random() * 100) + 1;
if (banditEvent => 70) {
alert("You were suddenly attacked by a bandit group!");
var enemy = "Bandit";
var enemyHealth = Math.floor(Math.random() * 75) + 20;
var enemyDamage = Math.floor(Math.random() * 25) + 5;
var enemyDropRate = Math.floor(Math.random() * 100) + 50;
fight();
}
} else if (choice == "2") {
alert("You wanted to talk with someone!");
// No code here until I fix the game...
}
}
function fight() {
if (playerHealth <= 0) {
alert("You died!");
startGame();
}
alert("Bandit: " + enemyHealth + " Health / " + enemyDamage + " Damage");
var fightChoice = prompt("1. Attack / 2. Do nothing");
if (fightChoice == "1") {
enemyHealth = enemyHealth - playerDamage;
alert("You attacked the " + enemy + " and dealt " + playerDamage + " damage!");
alert(enemy + " now have " + enemyHealth + " health left!");
playerHealth = playerHealth - enemyDamage;
alert(enemy + " attacked you!");
alert(enemy + " dealt " + enemyDamage + " damage to you!");
alert("You now have " + playerHealth + " health!");
fight();
} else if (fightChoice == "2") {
playerHealth = playerHealth - enemyDamage;
alert(enemy + " attacked you!");
alert(enemy + " dealt " + enemyDamage + " damage to you!");
alert("You now have " + playerHealth + " health!");
fight();
}
}
function exit() {
}
</script>
<style>
</style>
</head>
<body onload="startGame();">
</body>
I hope I explained it nicely. Thanks to all who help!