简体   繁体   English

Game Over条件简单的Snake C ++游戏

[英]Game Over condition simple Snake c++ game

I have basic knowledge of c++ and am trying to create a simple Snake game that runs in a while loop, as long as a "Game Over" condition evaluates to false. 我具有c ++的基本知识,并且正在尝试创建一个简单的Snake游戏,该游戏可以在while循环中运行,只要“ Game Over”条件的计算结果为false。 If it turns to "true" (when the snakes head goes out of bounds), "Game Over!" 如果变为“ true”(当蛇头超出范围时),则“ Game Over!” is printed on a lcd screen. 在液晶屏上打印。

For some reason, the code skips directly to the game over screen without running the game itself. 由于某种原因,该代码无需运行游戏本身即可直接在屏幕上跳过游戏。

My code involves a few classes, and in one of those I have a collision detection function that looks like this: 我的代码涉及几个类,在其中一个类中,我具有一个类似于以下内容的碰撞检测功能:

bool SnakeEngine::collision_detection()
{
    // check the snake coordinates so that the head doesn't go off screen
    if (_head_y < 1) {
        return 1;
    }
    if (_head_y > HEIGHT - 4) {
        return 1;
    }
    if (_head_x < 1) {
        return 1;
    }
    if (_head_x > WIDTH - 4) {
        return 1;
    } else {
        return 0;
    }

}

And in the main loop I have: 在主循环中,我有:

int main()
{
    snake.draw(lcd); // draw the initial game frame and the sprites
    lcd.refresh();

    while(!snake.collision_detection()) {

        snake.read_input(pad); // reads the user input
        snake.update(pad); // updates the sprites positions and calls the collision_detection() function
        render(); // clears the lcd, draws the sprites in the updated positions, refreshes the lcd
        wait(1.0f/fps);

    }
    lcd.printString("Game Over!",6,4);
    lcd.refresh();
}

How come is this not working? 这怎么不起作用? Thank you 谢谢

The collision detection is a suspect. 碰撞检测是可疑的。 If all specific conditions which you check do not return true ( 1 ) the final result should be false ( 0 ). 如果您检查的所有特定条件均未返回true1 ),则最终结果应为false0 )。

This condition is too restrictive: 此条件过于严格:

  if (_head_x > WIDTH - 4) {
        return 1;
    } else {
        return 0;
    }

It should be limited to: 它应限于:

  if (_head_x > WIDTH - 4) {
        return 1;
  }

  return 0;

The modified code with the use of bool types true and false looks like: 使用bool类型truefalse的修改后的代码如下所示:

bool SnakeEngine::collision_detection()
{
    // check the snake coordinates so that the head doesn't go off screen
    if (_head_y < 1) {
        return true;
    }

    if (_head_y > HEIGHT - 4) {
        return true;
    }

    if (_head_x < 1) {
        return true;
    }

    if (_head_x > WIDTH - 4) {
        return true;
    } 

    return false;
}

Try this, just guessing.I think when all the four conditions are false, than you should conclude that there is no collision. 尝试一下,只是猜测。我认为当所有四个条件都为假时,您应该得出结论,没有碰撞。 I think you did a blunder in the last else statement in your collision_detection() . 我认为您在crash_detection collision_detection()的最后else语句中犯了一个错误。

bool SnakeEngine::collision_detection()
{
    // check the snake coordinates so that the head doesn't go off screen
    if (  _head_y < 1 || _head_y > (HEIGHT - 4) || _head_x < 1 || _head_x > (WIDTH - 4) )
        return true;

       return false;
}

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

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