Home > database >  system("cls") command closes the input terminal
system("cls") command closes the input terminal

Time:01-10

The problem is located under the Draw function, where I use the system() command.

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

bool gameover;
const int width = 60;
const int height = 30;
int x, y, FruitX, FruitY;
enum edirection { Stop = 0, Left, Right, Up, Down, };
edirection dir;

void Setup()
{
    gameover = true;
    dir = Stop;
    x = width / 2;
    y = height / 2;
    FruitX = rand() % width;
    FruitY = rand() % height;
    
}

void Draw()
{
    system("cls"); //Clears Screen, but is not working!!!

    //Top Line
    for (int i = 0; i <= width; i  )
        cout << "#";
        cout << endl;
    
    //Side Lines
    for (int i = 0; i < height; i  )
    {
        for (int j = 0; j < width; j  )
        {
            if (j == 0 || j == width - 1)
                cout << "#";
            
            if (i == y && j == x) 
                cout << "<";

            else if (i == FruitY && j == FruitX)
                cout << "@";
            
            else if (j > 0 || j != width - 1)
                cout << " ";
        } cout << endl;
    } 

    //Bottom Line
    for (int i = 0; i <= width; i  )
        cout << "#";
    cout << endl;
}

void Input()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'a':
            dir = Left;
            break;
        case 'd':
            dir = Right;
            break;
        case 'w':
            dir = Up;
            break;
        case 's':
            dir = Down;
            break;
        case 'x':
            gameover = true;
            break;
        }
    }
}

void Logic()
{
    switch (dir)
    {
    case Left:
        x--;
        break;
    case Right:
        x  ;
        break;
    case Up:
        y--;
        break;
    case Down:
        y  ;
        break;
    default:
        break;
    }
}

int main()
{
    Setup();
    while (!gameover);
    {
        Draw();
        Input();
        Logic();
    }
}

CodePudding user response:

I improved your code and now it works to some degree. However, I can't write the whole game for you since I have no info on how it should be written.

Take a look:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>


struct GameStatus
{
    static constexpr std::size_t width { 60 };
    static constexpr std::size_t height { 30 };

    std::size_t x { };
    std::size_t y { };
    std::size_t FruitX { };
    std::size_t FruitY { };
    enum edirection { Stop = 0, Left, Right, Up, Down, };
    edirection dir;

    bool gameover { };
};

void Setup( GameStatus& game_status )
{
    game_status.gameover = false;
    game_status.dir = game_status.Stop;
    game_status.x = game_status.width / 2;
    game_status.y = game_status.height / 2;

    std::srand( std::time( 0 ) );
    game_status.FruitX = std::rand( ) % game_status.width;
    game_status.FruitY = std::rand( ) % game_status.height;
}

void Draw( GameStatus& game_status )
{
    system( "cls" );

    //Top Line
    for ( std::size_t i { }; i <= game_status.width;   i )
    {
        std::cout << '#';
    }

    std::cout << '\n';

    //Side Lines
    for ( std::size_t i { }; i < game_status.height;   i )
    {
        for ( std::size_t j { }; j < game_status.width;   j )
        {
            if ( j == 0 || j == game_status.width - 1 )
                std::cout << '#';
            
            if ( i == game_status.y && j == game_status.x )
                std::cout << '<';

            else if ( i == game_status.FruitY && j == game_status.FruitX )
                std::cout << '@';
            
            else if ( j > 0 || j != game_status.width - 1 )
                std::cout << ' ';
        }

        std::cout << '\n';
    } 

    //Bottom Line
    for ( std::size_t i { }; i <= game_status.width;   i )
        std::cout << '#';

    std::cout << '\n';
}

void Input( GameStatus& game_status )
{
    if ( _kbhit( ) )
    {
        switch ( _getch( ) )
        {
            case 'a':
                game_status.dir = game_status.Left;
                break;
            case 'd':
                game_status.dir = game_status.Right;
                break;
            case 'w':
                game_status.dir = game_status.Up;
                break;
            case 's':
                game_status.dir = game_status.Down;
                break;
            case 'x':
                game_status.gameover = true;
                break;
        }
    }
}

void Logic( GameStatus& game_status )
{
    switch ( game_status.dir )
    {
        case game_status.Left:
            --game_status.x;
            break;
        case game_status.Right:
              game_status.x;
            break;
        case game_status.Up:
            --game_status.y;
            break;
        case game_status.Down:
              game_status.y;
            break;
        default:
            break;
    }
}


int main( )
{
    GameStatus game_status;

    Setup( game_status );

    while ( !game_status.gameover )
    {
        Draw( game_status );
        Input( game_status );
        Logic( game_status );
    }
}

You can see the struct GameStatus. All those dangerous globals are now encapsulated in an instance of this struct called game_status. All the functions can receive a reference to that object and then use it (read/modify).

  •  Tags:  
  • Related