C ++运行直到按键

时间:2018-10-15 14:09:34

标签: c++ codeblocks

我想用C ++制作蛇,但是我不知道如何改变蛇的方向,直到我改变它,有什么想法吗? 我不想像Press escape这样的东西退出,我已经完成了整个代码,我只想知道如何运行它,只是改变方向,而不是每一步都改变方向。谢谢建议 这是我的代码,如果您可以帮助我的话:

#include <iostream>
#include <unistd.h>
#include <conio.h>
using namespace std;
int xs[100000], ys[100000];
int dx[5] = { 1,0,0,-1 };
int dy[5] = { 0,-1,1,0 };
int a[101][101];
int main()
{
    int z = 1, u, c, n, OK = 0, x, y, i, j, Repet = 0;
    char key;
    while (OK == 0)
    {
        x = rand() % 25 + 1;
        y = rand() % 25 + 1;
        if (a[x][y] == 0)
            OK = 1;
    }
    a[x][y] = 3;
    u = 1;
    xs[u] = x;
    ys[u] = y;
    c = 1;
    u = 1;
    for (i = 1; i <= 25; i++)
    {
        a[1][i] = 9;
        a[i][1] = 9;
        a[25][i] = 9;
        a[i][25] = 9;
    }
    OK = 0;
    while (OK == 0)
    {
        x = rand() % 25 + 1;
        y = rand() % 25 + 1;
        if (a[x][y] == 0)
            OK = 1;
    }
    a[x][y] = 5;
    for (i = 1; i <= 25; i++)
    {
        for (j = 1; j <= 25; j++)
        {
            if (a[i][j] == 0) cout << "  ";
            if (a[i][j] == 3) cout << "@ ";
            if (a[i][j] == 5) cout << "* ";
            if (a[i][j] == 9) cout << "# ";
        }
        cout << '\n';
    }
    while (z)
    {
        key = getch();
        if (key == 'w' || key == 'W') n = 8;
        if (key == 'a' || key == 'A') n = 4;
        if (key == 's' || key == 'S') n = 2;
        if (key == 'd' || key == 'D') n = 6;
        OK = 0;
        if (Repet != 1)
            c++;
        else Repet = 0;
        u++;
        xs[u] = xs[u - 1] + dx[n / 2 - 1];
        ys[u] = ys[u - 1] + dy[n / 2 - 1];
        a[xs[c - 1]][ys[c - 1]] = 0;
        if (a[xs[u]][ys[u]] == 9 || a[xs[u]][ys[u]] == 3)
        {
            cout << "YOU LOST";
            cout << "SCORE: " << u - c + 1;
            return 0;
        }
        a[xs[u]][ys[u]] = 3;
        a[xs[c - 1]][ys[c - 1]] = 0;
        if (xs[u] == x&&ys[u] == y) {
            Repet = 1;
            a[x][y] = 3;
            x = rand() % 25 + 1;
            y = rand() % 25 + 1;
            while (a[x][y] != 0)
            {
                x = rand() % 10 + 1;
                y = rand() % 10 + 1;
            }
            a[x][y] = 5;
        }
        usleep(1000);
        system("CLS");
        for (i = 1; i <= 25; i++)
        {
            for (j = 1; j <= 25; j++)
            {
                if (a[i][j] == 0) cout << "  ";
                if (a[i][j] == 3) cout << "@ ";
                if (a[i][j] == 5) cout << "* ";
                if (a[i][j] == 9) cout << "# ";
            }
            cout << '\n';
        }
        cout << "Your score is " << u - c + 1 << '\n';
    }
}

2 个答案:

答案 0 :(得分:0)

我相信这段代码会停止读取字符:

key = getch();

如果您不想在那里等待,可以将其替换为不停止读取字符的代码。例如:

if (kbhit()) {
    key = getch();
}

答案 1 :(得分:0)

如前所述,您需要添加以下内容:

while (1)
{
    if (kbhit())   // Check if any key pressed
        key = getch();
    if (key == 'w' || key == 'W') n = 8;
    ...

但是您还必须初始化变量:“ key”和“ n”,因为在您启动时,这些变量中有“垃圾”。例如:

int n = 8;         // 8 is up, you can choose another direction
char key = ' ';    // It doesn't care what char to initialize key

对我有用。如果您需要完整的代码,请告诉我。
祝你好运!