0

The terminal windows keeps flickering when i run the snake game, below i will share my draw function I'm not sure if its a windows issue because of 'cls' or i made a mistake somewhere

void Draw (){
    system("cls");
    for (int i=0; i<width+2; i++){
        cout<<"#";
    }cout<< endl;



    for (int i=0; i < height; i++ ){
        for (int j =0; j <width; j++){
            if (j == 0)
                cout << "#";
            if ( i ==y && j ==x)
                cout << "O";
            else if ( i == fruitY && j == fruitX)
                cout << "F";
            else{
                bool print = false; 
                for (int k =0; k <nTail; k++){

                    if (tailx[k] == j && taily[k] == i)
                    {
                        cout<<"-";
                        print = true; 
                    }

                }
                if (!print)
                        cout << " ";

            }
            if(j == width -1)
                cout << "#";     
        }
        cout << endl;
    }
    for (int i=0; i<width+2; i++){
        cout<<"#";
    }cout<< endl;
    cout << "Score: "<< score <<endl;
    cout << "To Exit press 'x'"<<endl;

}

i was wondering if there is a way to fix it

  • 1
    Console graphics are not made for games! There may be some botchy ascii art hack for your project but it isn’t ideal. – FShrike Mar 02 '20 at 17:31
  • 1
    You're using a lot of `endl` in your code. `endl` is NOT synonymous with `\n`; https://en.cppreference.com/w/cpp/io/manip/endl `endl` will flush the output buffer, which takes time. Removing all of those `endl`s (except the last one) is a good first start, but there's probably still some underlying buffering magic you can weave beyond that if this doesn't work well enough. – JohnFilleau Mar 02 '20 at 17:33
  • Games typically use two buffers, a backbuffer and a front buffer. One to render the current state the backbuffer to prepare to render the next state. This prevents flickering. Console does not do that. You can update the state without redrawing the console though. Just remove your CLS, clear any redundant pixels draw your new state pixels. – Irelia Mar 02 '20 at 17:47
  • 1
    `system("cls");` causes the flicker. – drescherjm Mar 02 '20 at 17:58

0 Answers0