samedi 25 juillet 2020

How can I make my snake run or move continue moving in c++?

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.

Aucun commentaire:

Enregistrer un commentaire