试图让我的游戏循环正确

时间:2016-11-10 21:09:25

标签: javascript

我正在尝试做我的第一个简单游戏。

我的问题是:为什么我的游戏循环不起作用?如果你看到代码,我试图将所有游戏代码放在if中。这个想法是:“如果游戏结束是假的,执行游戏,否则(当我的humanHungerBar达到0时)游戏结束了。”

你能在这帮我吗?非常感谢

<!doctype html>
<html>
    <head>
    <style>
    body {
    -webkit-user-select: none;
    }

    #screen {
    position: relative;
    left: 480px;
    top: 30px;
    border: 2px solid black;
    height: 500px;
    width: 400px;
    display: block;
    }

    #myCash {
    position: relative;
    width: 90px;
    height: 40px;
    top: 7px;
    left: 5px;
    border: 5px solid lightgreen;
    text-align: center;
    vertical-align: middle;
    line-height: 40px;
    color: green;
    font-weight: bolder;
    font-size: 20px;
    }

    #humanHunger {
    position: relative;
    width: 90px;
    height: 90px;
    top: 20px;
    left: 280px;
    border: 1px solid black;
    }

    #humanHungerContainer {
    position: relative;
    width: 100%;
    height: 20px;
    top: 20px;
    border: 1px solid black;
    background-color: red;
    }

    #humanHungerBar {
    position: absolute;
    width: 76%;
    height: 18px;
    border: 1px solid green;
    background-color: green;
    }

    #moneyMaker {
    position: relative;
    width: 300px;
    height: 450px;
    top: -850px;
    left: 100px;
    border: 3px solid green;
    background-image: url("moneyMakerBackground.png");
    }

    #jobInstructions {
    position: absolute;
    width: 250px;
    height: 50px;
    border: 3px solid orange;
    top: 20px;
    left: 22px;
    background-color: lightgreen;
    text-align: center;
    }

    #workingHours {
    position: absolute;
    width: 250px;
    height: 50px;
    border: 3px solid orange;
    top: 90px;
    left: 22px;
    background-color: lightgreen;
    text-align: center;
    vertical-align: middle;
    line-height: 50px;
    }

    #workCounter {
    position: absolute;
    width: 60px;
    height: 50px;
    border: 3px solid orange;
    top: 250px;
    left: 22px;
    background-color: lightgreen;
    text-align: center;
    }

    #clickingArea {
    position: absolute;
    width: 250px;
    height: 50px;
    border: 3px solid orange;
    top: 170px;
    left: 22px;
    background-color: lightgreen;
    filter: saturate(100%);
    }

    #clickingArea:hover {
    filter: saturate(190%);
    }

    #dollar {
    position: relative;
    left: 80px;
    top: 5px;
    }

    #nakedHuman {
    position: absolute;
    top: 25px;
    left: 120px;
    }

    #clothesScreen {
    position:relative;
    top: -400px;
    left: 900px;
    border: 2px solid black;
    width: 300px;
    height: 400px;
    overflow: auto;
    }

    #lumberShirt {
    position: absolute;
    top: 165px;
    left: 120px;
    display:none;
    }

    #coffeeStainedTShirt {
    position: absolute;
    top: 165px;
    left: 120px;
    display:none;
    }

    #regularJeans {
    position: absolute;
    top: 328px;
    left: 145px;
    display:none;
    }

    #lumberShirtMiniContainer {
    position: relative;
    top: 10px;
    left: 10px;
    }

    #coffeeStainedTShirtMiniContainer {
    position: relative;
    top: 30px;
    left: 10px;
    }

    #regularJeansMiniContainer {
    position: relative;
    top: 60px;
    left: 20px;
    }

    #burgerMiniContainer {
    position: relative;
    top: 90px;
    left: 10px;
    }

    #lumberShirtPrice {
    position: absolute;
    top: 20px;
    left: 100px;
    border: 3px solid orange;
    width: 50px;
    height: 20px;
    text-align: center;
    vertical-align: middle;
    line-height: 20px;
    background-color: orange;
    }

    #buyButtonLumber {
    position: absolute;
    top: 60px;
    left: 100px;
    border: 3px solid lightgreen;
    width: 30px;
    height: 15px;
    }

    #buyButtonCoffee {
    position: absolute;
    top: 60px;
    left: 100px;
    border: 3px solid lightgreen;
    width: 30px;
    height: 15px;
    }

    #buyButtonRegularJeans {
    position: absolute;
    top: 60px;
    left: 100px;
    border: 3px solid lightgreen;
    width: 30px;
    height: 15px;
    }

    #buyButtonBurger {
    position: absolute;
    top: 60px;
    left: 100px;
    border: 3px solid lightgreen;
    width: 30px;
    height: 15px;
    }
    </style>
    </head>
    <body>
        <div id="screen">
            <img id="nakedHuman" src="nakedHuman2.png" width="139.46" height="450">
            <img id="lumberShirt" src="lumberShirt.png" width="139.46" height="158.51">
            <img id="coffeeStainedTShirt" src="coffeeStainedTShirt.png" width="139.46" height="158.51">
            <img id="regularJeans" src="regularJeans.png" width="89" height="152.72">
            <div id="myCash"></div>
            <div id="humanHunger">
                <div id="humanHungerContainer">
                    <div id="humanHungerBar"></div>
                </div>
            </div>
        </div>
        <div id="clothesScreen">
            <div id="lumberShirtMiniContainer">
                <img id="lumberShirtMini" src="lumberShirt.png" width="70.38" height="80">
                <div id="lumberShirtPrice"></div>
                <div id="buyButtonLumber">Buy</div>
            </div>
            <div id="coffeeStainedTShirtMiniContainer">
                <img id="coffeeStainedTShirtMini" src="coffeeStainedTShirt.png" width="70.38" height="80">
                <div id="buyButtonCoffee">Buy</div>
            </div>
            <div id="regularJeansMiniContainer">
                <img id="regularJeansMini" src="regularJeans.png" width="46.62" height="80">
                <div id="buyButtonRegularJeans">Buy</div>
            </div>
            <div id="burgerMiniContainer">
                <img id="burger" src="burger.png" width="94.11" height="80">
                <div id="buyButtonBurger">Buy</div>
            </div>
        </div>
        <div id="moneyMaker">
            <div id="jobInstructions">You work on a click factory, so get to clickin'!!</div>
            <div id="workingHours"></div>
            <div id="clickingArea"><img src="dollar.png" id="dollar" width="82.55" height="42"></div>
            <div id="workCounter"></div>
        </div>
    <script>
    window.onload = function () {
        var gameOver = false;
        if (!gameOver) {
        var lumberShirtPrice = document.getElementById("lumberShirtPrice");
        lumberShirtPrice.innerHTML = 7;


        var myCash = document.getElementById("myCash");
        myCash.innerHTML = 45;

        var buyButtonLumber = document.getElementById("buyButtonLumber");
        buyButtonLumber.addEventListener("click", substractItemPriceFromMyCash);
        var negateFX = new Audio('negate1.wav');

        function substractItemPriceFromMyCash() {
            var a = parseInt(lumberShirtPrice.innerHTML);
            var b = parseInt(myCash.innerHTML);
            if (a > b) {
                negateFX.play();
            }
            else {
                myCash.innerHTML -= lumberShirtPrice.innerHTML;
                console.log("you bought the lumber shirt");
            }
        }

        var workingHoursScreen = document.getElementById("workingHours");
        workingHoursScreen.innerHTML = 0;
        var workCounter = document.getElementById("workCounter");
        workCounter.innerHTML = 0;
        var allowedToWork = false;
        var workingHoursChronometer = setInterval(incrementWorkingHoursChronometer, 1000);
        function incrementWorkingHoursChronometer () {
            var a = parseInt(workingHoursScreen.innerHTML);
            if(a < 10) {
                workingHoursScreen.innerHTML++;
            }
            else if (a == 10) {
                workingHoursScreen.innerHTML = 0;
                workCounter.innerHTML++;    
            } 
            var b = parseInt(workCounter.innerHTML);
            if (b == 4) {
            workCounter.innerHTML = 0;
            }
            if (b % 2 == 0) {
            allowedToWork = true;
            }
            else if (b % 2 == 1) {
            allowedToWork = false;
            }
        }



        var coinFX = new Audio('coin1.wav');
        var clickingAreaBox = document.getElementById("clickingArea");
        clickingAreaBox.addEventListener("click", giveMeMoney);

        function giveMeMoney() {
            if(allowedToWork) {
                myCash.innerHTML++;
                coinFX.play();
            }
            else {
                negateFX.play();
            }
        }

        var humanHungerBar = document.getElementById("humanHungerBar");
        var barWidth = 76; 
        humanHungerBar.style.width = barWidth + '%';
        var humanHungerBarDecrement = setInterval (decreaseHumanHungerBar, 700);
        function decreaseHumanHungerBar () {
            if (barWidth > 0) {
                humanHungerBar.style.width = barWidth + '%';
                barWidth--; 
            }
        }

        var buyButtonBurger = document.getElementById("buyButtonBurger");
        var burgerPrice = 15;
        buyButtonBurger.addEventListener("click", buyBurgerRestoreLifeAndDecreaseMoney);
        function buyBurgerRestoreLifeAndDecreaseMoney() {
            var a = parseInt(myCash.innerHTML);
            if (a >= burgerPrice){
                if(barWidth < 92) {
                    barWidth += 10;
                    myCash.innerHTML -=burgerPrice;
                }
                else if (barWidth == 1) {
                gameOver = true;
                console.log("bar is 1");
                }
                else {
                    negateFX.play();
                }   
            }
            else {
            negateFX.play();
            }
        }
        }
        else {
        document.getElementById("screen").style.display = 'none';
        }
    }
    </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

所以你编写了一个执行一次的脚本。它从头到尾,然后停止。所以你要做的就是编写一个反复重复的脚本,直到游戏结束。所以这是一个非常简短的例子,说明如何在javascript中执行此操作:

while (!gameOver) {
  // do game code
}

但等待!!!

因此while循环中的代码将一直持续发生,直到gameOver变量为true。但是,如果你尝试使用该代码,你的游戏可能会冻结!为什么?因为浏览器正在尽可能快地执行while循环内的代码。但是,如果您希望您的游戏以每秒帧速率运行,那么您可能希望使用javascript超时。所以尝试这样的事情:

setInterval(function() {
    // do game code
}, 1000/60);

这是您在技术&#34;游戏循环中所需的绝对最低 <#34;。但是,这并不是开始创建基于浏览器的游戏的推荐方法。尝试进行一些研究并查看thisthis等内容。