简体   繁体   English

当你吃一个苹果时我的 Snake 游戏崩溃了,苹果在你采摘一个新的 position (C++, SDL) 后会在你体内生成

[英]My game of Snake crashes when you eat an apple, and the apple spawns inside your body after picking a new position (C++, SDL)

I thought I'd attempt to make Snake since it is a pretty easy game to make.我想我会尝试制作Snake ,因为它是一款非常容易制作的游戏。 I was having an issue where the apple would spawn inside the snakes body, and so I came up with a way to prevent that from happening:我遇到了苹果会在蛇体内产卵的问题,所以我想出了一种方法来防止这种情况发生:

void getRandomApplePos() {
    // variable that tells the while loop whether the moving of the apple was successful
    bool success;

    // variable that is set to false if the apple is inside the snakes body
    bool appleNotInside;

    // Tells the collision to stop testing for collision until the apple has successfully moved
    bool appleHasMoved;

    // sets the variables 
    success = false;
    appleNotInside = false;
    appleHasMoved = false;

    // while the apple spawns inside the snake, it keeps generating new positions 
    while (!success) {
        // random seed
        srand((unsigned int)time(NULL));

        // gets a random position
        int randomX = rand() % 769;
        int randomY = rand() % 673;

        // resets the two variables if this while loop as ran again
        apple.delta_pos_x = 0;
        apple.delta_pos_y = 0;
    
    // checks to see if the apple has spawned in the same exact position
        while (apple.delta_pos_x == 0 && apple.delta_pos_y == 0) {
            // gets the previous poition of the apple
            apple.prevPos_x = apple.x;
            apple.prevPos_y = apple.y;

            // picks a new apple position
            apple.x = round((randomX) / 32) * 32;
            apple.y = round((randomY) / 32) * 32;

            // gets the new apple position
            apple.currentPos_x = apple.x;
            apple.currentPos_y = apple.y;

            // sets the difference between the positions, if it's 0, then it has spawned in the same exact location
            apple.delta_pos_x = (float)(apple.currentPos_x - apple.prevPos_x);
            apple.delta_pos_y = (float)(apple.currentPos_y - apple.prevPos_y);
        }

        // checks to see if the snake length is only one, as to make the list not go out of index
        if (snake.bodyLength == 1) {
            // if the apple happens to spawn inside the snake with a length of 1, it will add false to the appleInSnake vector, else it adds true
            if (apple.x == snakeBody[0][0] && apple.y == snakeBody[0][1]) {
                appleNotInside = false;
                appleInSnake.push_back(appleNotInside);
            }
            else {
                appleNotInside = true;
                appleInSnake.push_back(appleNotInside);
            }
        }
        else {
            // if the apple happens to spawn inside the currently compared snakeBodyPosition, it will add false to the appleInSnake vector, else it adds true
            for (int i = 0; i < snakeBody.size(); i++) {
                if (apple.x == snakeBody[i][0] && apple.y == snakeBody[i][1]){
                    appleNotInside = false;
                    appleInSnake.push_back(appleNotInside);
                }
                else {
                    appleNotInside = true;
                    appleInSnake.push_back(appleNotInside);
                }
            }
        }

        // if false appears inside the appleInSnake vector at all, it sets success to false and goes through the loop again. Else it breaks out.
        if (std::find(appleInSnake.begin(), appleInSnake.end(), false) != appleInSnake.end()) {
            success = false;
        }
        else {
            success = true;
        }

        //clears appleInSnake so that it can take in a new comparision
        appleInSnake.clear();
    }

    // tells the collision to start back up again
    appleHasMoved = true;

}

So, whenever the apple does end up spawning inside of the snakes body, it crashes, just outright.因此,每当苹果最终在蛇体内产卵时,它就会彻底崩溃。 I suspect some kind of infinite loop, but I can't put my finger on why this happens.我怀疑某种无限循环,但我无法弄清楚为什么会发生这种情况。

You are initializing your random number generator within your loop.您正在循环中初始化随机数生成器。 Note that the RNG is deterministic .请注意,RNG 是确定性的。 It means that you will end up drawing the same numbers all over again as in the previous loop.这意味着您最终将再次绘制与前一个循环相同的数字。

Initialize the RNG once at the start of your program.在程序开始时初始化一次 RNG。 This way, the numbers drawn may be expected to be different within every loop.这样,在每个循环中抽取的数字可能会有所不同。

You might wonder, that the crude use of time() should prevent this.您可能想知道,粗略地使用time()应该可以防止这种情况发生。 A typical implementation of time() will have the granularity of seconds. time()的典型实现将具有秒的粒度。 So you would only expect the return value to change once a second, hence, you get the same initialization over and over again in your loop.因此,您只会期望返回值每秒更改一次,因此,您会在循环中一遍又一遍地获得相同的初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM