1

I want to make snake in c++ but i don't know how to make the snake go in one direction until i change it , any ideas? I don't want something like Press escape to exit , i already made the whole code , i just want to know how to make it run and just change the direction and not to change the direction every step . Thanks in advice Here is my code if you can help me out:

#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';
    }
}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • 5
    This code is illegible. Your program won't run faster by squishing up the code and using short names. Edit : Fixed it a bit. – François Andrieux Oct 15 '18 at 14:12
  • One possible solution is to do game loop and keyboard input in separate threads. I once provided an answer concerning this: [SO: I/O in concurrent program](https://stackoverflow.com/a/48097134/7478597) which might provide an inspiration. – Scheff's Cat Oct 15 '18 at 14:20

1 Answers1

0

As said before, you need to add the following:

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

But you also have to initialize the variables: 'key' and 'n', because at the moment you start, there is 'garbage' in those variables. For example:

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

It works for me. If you need the whole code, let me know.
Good Luck!