0

I'm a C++ noob coding the snake game. The whole program draws the board, fruit and snake head perfectly, however I cannot seem to make the snake head coordinates change using a keyboard hit function. When going through the input function which gets keyboard strokes for snake movement, it appears that the if(kbhit) evaluates to false when running program,what is my error and how can I get the snake head to move?

Thanks.

#include <iostream>
#include <stdlib.h> 
#include <time.h>
#include <windows.h>
#include <conio.h>
using namespace std;

int X, Y, fruitX, fruitY;
enum eDir { STOP = 1, UP = 2, DOWN = 3, RIGHT = 4, LEFT = 5 }; //declare variables
eDir direction;
const int height = 20;
const int width = 20;
int board[height][width];
bool gameOver;

void setup()
{
    gameOver = false;
    srand(time(NULL));          // initilize game set up
    fruitX = (rand() % width);
    fruitY = (rand() % height);
    X = height / 2;
    Y = width / 2;
    direction = STOP;
    int board[height][width];
};

void draw()
{
    system("cls");

    for (int i = 0; i < width + 2; i++)
        cout << '#';

    cout << endl;

    for (int i = 0; i < height; i++)    //loop through matrix to draw board
    {
        for (int j = 0; j < width; j++)
        {

            if (j == 0)
                cout << '#';
            if (i == Y && j == X) // draw snake head
                cout << 'T';
            else if (i == fruitY && j == fruitX) // draw fruit
                cout << 'F';
            else
                cout << ' ';

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

    for (int i = 0; i < width + 2; i++)
        cout << '#';

    cout << endl;
};

void input()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'w':
            direction = UP;
            break;
        case 's':
            direction = DOWN;
            break;
        case 'a':
            direction = LEFT;
            break;
        case 'd':
            direction = RIGHT;
            break;
        default:
            break;
        }

    }
};

void logic()
{
    switch (direction)
    {
    case UP:
        Y--;
        break;
    case DOWN:
        Y++;
        break;
    case LEFT:
        X--;
        break;
    case RIGHT:
        X++;
        break;
    default:
        break;
    }
};

int main()
{
    setup();
    while (!gameOver)
    {
        draw();
        input();
        logic();
        return 0;
    }
};
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • How did you establish that the test evaluates to false? Did you add logging? You say it appears that it evaluates to false but don't tell us what you actually observed, just the (possibly incorrect) conclusion you drew from it. It's like telling your doctor, "Doc, my cortisone level appears to be low". If you don't tell him what symptoms lead you to think that, how can he figure out what's wrong with you? – David Schwartz Apr 22 '18 at 18:41
  • Also, your code doesn't make very much sense. You are constantly clearing the screen -- how is anyone supposed to see anything at all? – David Schwartz Apr 22 '18 at 18:42
  • You `return 0;` from `while` loop in `main`, this means it runs exactly 1 time before the program ends – Killzone Kid Apr 22 '18 at 18:44
  • @KillzoneKid That sounds like an answer to me – Yann Apr 22 '18 at 18:47
  • @Yann I haven't read the question, only formatted it and noticed that. – Killzone Kid Apr 22 '18 at 18:48
  • @KillzoneKid In that case, kudos for (probably) solving the problem with a cursory glance. You would get false from _kbhit if you didn't had a chance to press a key because you were waiting for the debugger to start and on your first frame you hit a breakpoint – Yann Apr 22 '18 at 18:52
  • @Yann i erased the return 0 however the program draws the board over and over again infinatly, also X and Y wont change – twillycodes Apr 22 '18 at 19:01
  • acctually X and Y now change, however it wont remain on the same game board as it keeps looping through draw – twillycodes Apr 22 '18 at 19:02
  • @Yann i got it thanks! – twillycodes Apr 22 '18 at 19:05
  • Yeah, with the return moved outside of the while, this runs fine on my machine. You'll definitely want to rate-limit it, as the snake currently moves as fast as your CPU goes. Also, in games engines you tend to run draw after the update otherwise the game is rendering last frames input which feels really unresponsive. Not a big deal in this game, but later on it'll be an issue. – Yann Apr 22 '18 at 19:09

1 Answers1

0

For one thing, you do return 0; inside your game loop which kills the program after only one iteration which I assume you did not intend.

I made snake using a similar method and mine looks like this:

bool play = true;

while(play) {
    while (_kbhit) {
        switch (_getch) {
        case 'w':
            //code
            break;
        case 's':
            //code
            break;
        case 'a':
            //code
            break;
        case 'd':
            //code
            break;
        }
    }
}

That method worked for me and I think the only difference is that I used a while loop instead of an if.

PR06GR4MM3R
  • 397
  • 2
  • 13