1

I am a beginner in c++ and i made my first game, a snake game. I made it without any graphics library.

So far the things were good, my snake was just running fine and eating the fruit and score is also increasing.

At the moment my snake just runs while the key is pressed, but now I want to run my snake continuously and just change its direction with keys as we have seen in old snake games.

So far I have tried many things from my side, like a loop and all, but the things just didn't work in the way I wanted it to be.

There is my code-

#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 n_Tail;
enum eDirection {STOP = 0, LEFT , RIGHT ,UP , DOWN};
eDirection dir;

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

void draw()
{
    system("cls");
    for(int i = 0 ;i < width+1; i++)
    {
        cout << "#"; //for Upper wall
    }
    cout << "\n";

    for (int i  = 0; i < height; i++)
    {
        for (int j = 0; j < width ; j++)
        {
            if (j==0)
            {
                cout <<"#";

            }
            if (i == y && j == x)
            {
                cout <<"0";
            }
             else if (i == fruitY && j == fruitX)
            {
                cout <<"f";
                width - 1;
            }
            else if (j== width -1)
            {
                cout << "#";            
            }
            else
            {
                bool print = false;
                for (int k = 0; k <n_Tail; k++)     
                {
                    if (tailX[k] == j && tailY[k] ==  i )
                    {
                        cout << "o";
                        print = true;
                    }
                }
                if (!print)
                {
                   cout <<" ";
                    
                }
            }
            
        }
        cout << "\n";
    }
                                    
    for (int i = 0; i < width+1; i++)
    {
        cout << "#"; //for lower wall 
    }
    cout <<"\n";
    cout << "Score = " << score;

}

void input()
{
    
        switch (_getch())
        {

            case 'a': dir = LEFT;
                break;

            case 'w': dir = UP;
                break;

            case 's': dir = DOWN;
                break;

            case 'd': dir = RIGHT;
                break;
        }
    

}

void logics()
{
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;

    for (int i = 1; i < n_Tail; 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:
        y--;



    }
        
    for (int i = 0; i < n_Tail; i++)
    {
        if (tailX[i] == x && tailY[i] == y)
        {
            gameOver = true;
        }
    }
    //if (x> width||x<0||y>height||y<0)
    //{
    //gameOver = true;
    //}
    if (x > width-2)x = 0; else if (x < 0)x = width - 2;
    if (y > height-1)y = 0; else if (y < 0)y = height - 1;
    {

    }


    if (x == fruitX && y == fruitY)
    {
        score = score + 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        n_Tail++;

    }
}
int main()
{
    setup();
    while (!gameOver)
    {
        draw();
        input();
        logics();
        Sleep(10);

    }
}

Game while running

someone please help me with this so i can continue learning c++ peacefully.

CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • Hi, and welcome! Please describe what you tried, what your reasoning was to try exactly that, and in what ways it failed. "it didn't work" is a bit vague, we can't really help you that way. Also, please read and follow the [guide how to debug small problems](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). This will not only help you to formulate a better question but can maybe actually solve your issue in the process. Thank you! – CherryDT Jul 25 '20 at 10:43
  • can you tell in more briefly how I can use this with all controls working? – Harsh Tripathi - blue Jul 25 '20 at 10:52
  • No because it would miss the mental process that happens when you follow the methodology outlined in the article. I can't change your synapses UKMonkey's answer below might help, but you see that the same article was linked there as well, so I urge you to give it a go. Because if you had followed it, you would have arrived at the question "why does my program stop at calling `_getch`" or similar (which you would then have been able to answer yourself by checking the docs of that function), and you'd have been on the right track to solve the first of your problems. – CherryDT Jul 25 '20 at 11:01
  • @CherryDT I can't change your synapses [challenge accepted](http://www.freakinawesomenetwork.net/wp-content/uploads/2014/12/basic1.png) – UKMonkey Jul 25 '20 at 11:12

1 Answers1

3

As I'm sure you noticed when you used your debugger. No? No debugger? This is your essential read!

You're using _getch to read the input. getch is blocking - meaning it will wait until you press a key. It's not really what you want.

This post explains how to make a non-blocking version.

UKMonkey
  • 6,941
  • 3
  • 21
  • 30