蛇游戏 - 吃食物,但不会产生另一个(javascript)

时间:2017-01-30 02:37:58

标签: javascript node.js

我真的不知道该怎么称呼标题,但我使用nodejs和套接字在js上制作蛇游戏。

我希望食物如何发挥作用,当食物产生时,它有21/25的机会产生一个绿色的,3/25的红色和1/25的黄色。

我也希望如果阵列中有超过1种食物,如果玩家吃绿色食物,它就会消失

绿色= 10分并产生另一种食物,红色= 50分并产生3个绿色,黄色= 200并产生5个绿色,

它的工作正常,除非屏幕上有超过1种绿色食物而且我吃了一种,有时它们中的一些会消失,有时它们都会消失,我不知道为什么。它可能是非常简单的东西,但这是我的代码。

我知道它需要接受很多代码,但我已经添加了评论来帮助,任何建议都是适用的。

var FoodList = [];
SpawnFood(10, 1); //Spawn a default peice of food


function SpawnFood(Value, Amount) //Add a Food to the board
{
    for (var i = 0; i < Amount; i++) {
        var j = i;

        if (FoodList.length == 0) {
            FoodList[j] = new Food(); //If there is no food in the array, spawn one
            FoodList[j].init();
            FoodList[j].FoodValue = Value;
            j++
        } else { //if there is food in the array
            while (FoodList[j] !== undefined) { //choose the lowest empty place
                j++
            }
            FoodList[j] = new Food(); //And then spawn in
            FoodList[j].init();
            FoodList[j].FoodValue = Value;
        }
    }
}

//Collision detection below :

for (var i in SnakeList) //For each instance of snake
{
    var snake = SnakeList[i]; //Declare snake as the current snake

    for (var j in FoodList) //For each instance of Food
    {
        if (snake.hasColision(FoodList[j])) //If they have collided
        {
            snake.addLength(); //Removes the Food and adds length to the current snake
            snake.eatFood(FoodList[j]); //Rewards the player with score)

            var Value = FoodList[j].FoodValue; //Asking it what score value the food has
            delete FoodList[j]; //Deletes that food

            var count = 0;
            for (var s in FoodList) //For each instance of Food
            {
                count++;
            } //Counts how many peices of food are in the array (.length doesnt work)

            if (Value == 10) { //If the player ate a green food

                if (count < 1) { //And theres more than 1 food in the array

                    var RandomNumber = Math.round(Math.random() * (25 - 1) + 1); //Generate a random num

                    if ((RandomNumber >= 1) && (RandomNumber <= 21)) //common drop, green food
                    {
                        SpawnFood(10, 1); //Soawn green food
                    } else if ((RandomNumber >= 22) && (RandomNumber <= 24)) //rare red
                    {
                        SpawnFood(50, 1); //spawn red
                    } else {
                        SpawnFood(200, 1); //spawn very rare yellow
                    }
                }

            } else if (Value == 50) { //If the player ate a red, spawn 3 green
                SpawnFood(10, 3);
            } else if (Value == 200) { //If the player ate a yellow, spawn 5 green
                SpawnFood(10, 5);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

以下是你应该怎么做的:(我不知道SnakeList但它也应该是一个数组,如果它不是那么遵循与FoodList相同的方法

var FoodList = [];
SpawnFood(10, 1);


// remove food from index
function removeFood(index){
    // cut that element out of the array (Read more about splice)
    FoodList.splice(index, 1);
}

// spawn an amount of food by calling spawnOneFood an amount of times
function SpawnFood(Value, Amount) {
    for (var i = 0; i < Amount; i++) {
        // create a new food
        var f = new Food();
        f.init();
        f.FoodValue = Value;

        // add it to FoodList
        FoodList.push(f);
    }
}

// get random value (it good practice to split diferent logics into diferent function)
function getRandomValue(){
    var RandomNumber = Math.round(Math.random() * (25 - 1) + 1);

    // no need to check if it's greater than 1 (it is)
    if (RandomNumber <= 21)
        return 10;
    // no need to check if it's greater than 22 (it is)
    else if (RandomNumber <= 24)
        return 50;
    // no need for else (if we are here then the above tests failed) (if you get a yellow food go straight to a casino because you're lucky as hell)
    return 200;
}

// SnakeList should be an array too. (for(var i = 0; i < SnakeList.length; i++))
for (var i in SnakeList)
{
    var snake = SnakeList[i];

    for (var j = 0; j < FoodList.length; j++) //For each instance of Food
    {
        if (snake.hasColision(FoodList[j]))
        {
            snake.addLength();
            snake.eatFood(FoodList[j]);

            var Value = FoodList[j].FoodValue;
            removeFood(j);
            j--; // removeFood actually shrinks the array so we need to go backwards one step to handle all elements

            // This should work now
            var count = FoodList.length;

            if (Value == 10) {

                if (count < 1) {
                    var randomValue = getRandomValue();
                    SpawnFood(randomValue, 1);
                }

            } else if (Value == 50) {
                SpawnFood(10, 3);
            } else if (Value == 200) { // as I said: LUCKY AS HELL
                SpawnFood(10, 5);
            }
        }
    }
}