0

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!

Teki
  • 3
  • 4
  • because the variables defined inside of game are not accessible to the fight function. https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – epascarello Sep 14 '17 at 15:29
  • Do you use a javascript step by step debugger ? – GGO Sep 14 '17 at 15:30
  • Now I see where my mistake was! I didn't know about that. Thank you! @epascarello – Teki Sep 14 '17 at 15:34
  • No I don't. @GabrielGourrat – Teki Sep 14 '17 at 15:35
  • Open the developper tools on Chrome using "F12". > "Sources" tab > find your source javascript > click on the line number to set a debug breaker and play again your javascript code. The debugger will play your script and stop when the current line will be executed. – GGO Sep 14 '17 at 15:36
  • Alright. Thanks. – Teki Sep 14 '17 at 15:44

2 Answers2

0
var playerHealth = 100;
var playerDamage = 7;
var playerClass = "Farmer";
var playerGold = 50;
function Fight(){ ... };
function game() { ... };

Variables that are used by multiple functions must be declared globally and not directly in a function. Instead you can declare variable in functions if the variable is temporary used

GGO
  • 2,678
  • 4
  • 20
  • 42
0

I think I am a bit late answering, Just the JavaScript, hope this helps

      var playerName = prompt("What is your name?");
        var playerHealth = 100;
        var playerDamage = 7;
        var playerClass = "Farmer";
        var playerGold = 50;
    var enemyHealth=0;
        var enemyDamage ;
    var enemy;
                var enemyDropRate=0;

    function startGame() {
        var start = prompt("1. Start / 2. Exit");
        if (start == "1") {
            game();
        } else if (start == "2") {
            alert("Goodbye!");
            exit();
        }
    }


    function game() {

        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!");
                 enemy = "Bandit";
                 enemyHealth = Math.floor(Math.random() * 75) + 20;
                 enemyDamage = Math.floor(Math.random() * 25) + 5;
                 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() {
    }
vinay yadav
  • 106
  • 6
  • The best way to refact the game code is making it OOP (Oriented Object Programming). @Teki, if you're really interested of javascript, i advice to test it. It's a very good structuration code, and it's perfect for this type of application – GGO Sep 15 '17 at 07:16
  • Yeah, I am very interested in JavaScript and Web Development, I'm doing JavaScript all the day, watching tutorial etc... Once I learn it for good I will make maybe 2D game with `canvas` and other... Anyway thanks for advice! @GabrielGourrat – Teki Sep 15 '17 at 12:43