Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当你吃了一个苹果,我的蛇游戏崩溃了,苹果在你选择了一个新的位置(C+;+;,SDL)后在你体内产卵_C++_Sdl 2 - Fatal编程技术网

C++ 当你吃了一个苹果,我的蛇游戏崩溃了,苹果在你选择了一个新的位置(C+;+;,SDL)后在你体内产卵

C++ 当你吃了一个苹果,我的蛇游戏崩溃了,苹果在你选择了一个新的位置(C+;+;,SDL)后在你体内产卵,c++,sdl-2,C++,Sdl 2,我想我会尝试制作蛇,因为这是一个非常容易制作的游戏。我遇到了一个问题,苹果会在蛇体内繁殖,所以我想出了一个方法来防止这种情况发生: 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

我想我会尝试制作,因为这是一个非常容易制作的游戏。我遇到了一个问题,苹果会在蛇体内繁殖,所以我想出了一个方法来防止这种情况发生:

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;

}
void getRandomApplePos(){
//变量,告知while循环移动苹果是否成功
成功;
//变量,如果苹果在蛇体内,则该变量设置为false
布尔阿普列诺丁赛德;
//告诉碰撞停止碰撞测试,直到苹果成功移动
bool appleHasMoved;
//设置变量
成功=错误;
appleNotInside=false;
appleHasMoved=false;
//当苹果在蛇体内产卵时,它会不断产生新的位置
而(!成功){
//随机种子
srand((无符号整数)时间(NULL));
//获取一个随机位置
int randomX=rand()%769;
int randomY=rand()%673;
//如果此while循环再次运行,则重置两个变量
apple.delta_pos_x=0;
apple.delta_pos_y=0;
//检查苹果是否在相同的确切位置产卵
while(apple.delta_pos_x==0和apple.delta_pos_y==0){
//获取苹果的上一个点
apple.prevPos_x=apple.x;
apple.prevPos_y=apple.y;
//选择一个新的苹果位置
apple.x=圆形((随机数x)/32)*32;
苹果y=圆形((随机)/32)*32;
//获取新的苹果位置
apple.currentPos_x=apple.x;
apple.currentPos_y=apple.y;
//设置位置之间的差异,如果为0,则它已在相同的确切位置生成
apple.delta_pos_x=(float)(apple.currentPos_x-apple.prevPos_x);
apple.delta_pos_y=(float)(apple.currentPos_y-apple.prevPos_y);
}
//检查蛇的长度是否只有一个,以使列表不会超出索引
如果(snake.bodyLength==1){
//如果苹果恰好在长度为1的蛇体内产卵,它将向appleInSnake向量添加false,否则将添加true
if(apple.x==snakeBody[0][0]&&apple.y==snakeBody[0][1]){
appleNotInside=false;
appleInSnake.向后推(appleNotInside);
}
否则{
appleNotInside=真;
appleInSnake.向后推(appleNotInside);
}
}
否则{
//如果苹果恰好在当前比较的snakeBodyPosition内繁殖,它将向appleInSnake向量添加false,否则将添加true
对于(int i=0;i

因此,每当苹果最终在蛇体内产卵时,它就会完全崩溃。我怀疑有某种无限循环,但我无法解释为什么会发生这种情况。

您正在初始化循环中的随机数生成器。 请注意,RNG是确定性的。这意味着您将再次绘制与上一个循环相同的数字

在程序开始时初始化RNG一次。这样,在每个循环中绘制的数字可能会有所不同


您可能会想,粗暴地使用
time()
应该可以防止这种情况。
time()
的典型实现的粒度为秒。因此,您只希望返回值每秒更改一次,因此,您在循环中一次又一次地得到相同的初始化。

您正在循环中初始化随机数生成器。 请注意,RNG是确定性的。这意味着您将再次绘制与上一个循环相同的数字

在程序开始时初始化RNG一次。这样,在每个循环中绘制的数字可能会有所不同


您可能会想,粗暴地使用
time()
应该可以防止这种情况。
time()
的典型实现的粒度为秒。因此,您只希望返回值每秒更改一次,因此,在循环中一次又一次地得到相同的初始化。

在这里进行猜测,但这肯定是错误的
srand((unsigned int)time(NULL))。您应该在
main
中调用
srand
一次。因为
time
每秒只返回一个新值,所以代码根本不会生成随机数。你会在整整一秒钟内反复得到相同的两个随机数。不确定这是否解释了你的崩溃,但这肯定是你应该解决的问题。对不起,约翰,我看到了你的评论,这基本上是一个答案,太晚了。为什么不做一个呢?@ypnos没问题。但是,我将把它作为一个注释,因为我不确定这是否是OPs问题的真实解释。@Manifest启动调试器。哪一行