程序有效,但在20-30秒后随机关闭

时间:2018-01-21 23:36:11

标签: c++

我正在写一个蛇游戏,我已经能够产生一个按计划运作的完整结果。

然而,游戏似乎随机崩溃。我点击了另一个命令,即使没有满足GameOver条件,游戏也会关闭。

我附上了代码:

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool GameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN};
Direction dir;

int Setup()

{
    GameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
    return 0;
}

int Draw()

{
        system("cls"); //system("clear");
        for (int i = 0; i < width+2; i++)
            cout << "#";
        cout << endl;

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                if (j == 0)
                    cout << "#";
                if (i == y && j == x)
                    cout << "O";
                else if (i == fruitY && j == fruitX)
                    cout << "F";
                else
                {
                    bool print = false;
                    for (int k = 0; k < nTail; k++)
                    {
                        if (tailX[k] == j && tailY[k] == i)
                        {
                            cout << "o";
                            print = true;
                        }
                    }
                    if (!print)
                        cout << " ";
                }


                if (j == width - 1)
                    cout << "#";
            }
            cout << endl;
        }

        for (int i = 0; i < width+2; i++)
            cout << "#";
        cout << "\n";
        cout << "Score:" << score << "\n";
        return 0;
}

int Input()

{
        if (_kbhit())
        {
            switch (_getch())
            {
            case 'a':
                dir = LEFT;
                break;
            case 'd':
                dir = RIGHT;
                break;
            case 'w':
                dir = UP;
                break;
            case 's':
                dir = DOWN;
                break;
            case 'x':
                gameOver = true;
                break;
            }
        }
    return 0;
}

int Logic()

{
        int prevX = tailX[0];
        int prevY = tailY[0];
        int prev2X, prev2Y;
        tailX[0] = x;
        tailY[0] = y;
        for (int i = 1; i < nTail; i++)
        {
            prev2X = tailX[i];
            prev2Y = tailY[i];
            tailX[i] = prevX;
            tailY[i] = prevY;
            prevX = prev2X;
            prevY = prev2Y;
        }
        switch (dir)
        {   
        case LEFT:
            x--;
            break;
        case RIGHT:
            x++;
            break;
        case UP:
            y--;
            break;
        case DOWN:
            y++;
            break;
        default:
            break;
        }

        for (int i = 0; i < nTail; i++)
            if (tailX[i] == x && tailY[i] == y)
                GameOver = true;

        if (x == fruitX && y == fruitY)
        {
            score += 10;
            fruitX = rand() % width;
            fruitY = rand() % height;
            nTail++;
        }
        return 0;
}

int main()

{
    Setup();
    while (!GameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(50);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

你的tailX / tailY数组的容量为100,但是你不会对nTail的大小有限制。因此,当nTail变得大于100时,程序的行为将变得不确定,并且可能会随机崩溃。

但实际上,您应该1)学习如何使用调试器,以及2)获得更现代的C ++编译器。这里很少能够编译或测试它。