I am new to Dev C++ and i am creating a program of a game of snake.

In Dev C++ the graphics open in WINDOWS BGI screen whereas the text gets shown in Text console(Dos).
1)i want to know how can i clear the WINDOWS BGI screen ?
2)how can i Close WINDOWS BGI screen so as to return to the text console?

#include<iostream>

#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
 

using namespace std;
int snake(int);
int main()
{


 
cout<<"WELCOME TO SNAKE WORLD\n PRESENTED BY PRABHJIT,RUHI AND SAURAV OF CS-11";
cout<<"  RULES FOR THE GAME";
cout<<"KEYS---\n w- FORWARD\n a- LEFT TURN\n d- RIGHT TURN\n s- DOWN";
cout<<"  PRESS MOVE(a) KEY TO CONTINUE.....";
cout<<"ENTER YOUR  LEVEL\n";
cout<<"\n1.ROOKIE";
cout<<"\n2.CATCHING UP";
cout<<"\n3.MEDIUM";
cout<<"\n4.MASTER";
cout<<"\n5.THE KING\n";
int choice,prabh;
cin>>choice;
switch(choice)
{
              case 1:prabh=100;
              break;
              case 2:prabh=70;
              break;
              case 3:prabh=50;
              break;
              case 4:prabh=30;
              break;
              case 5:prabh=10;
              break;
              default:cout<<"Wrong option entered...Exiting";
              break;
              }
snake(prabh);              

		      return 0;
              }
int snake(int z)
{int prabh;
prabh=z;
int gdriver = DETECT, gmode, errorcode;
 initgraph(&gdriver, &gmode, "c://tc//");
 cleardevice();
 char ans;
 rand();
 int x=10,y=10,n=1,i;
 int a=0,b=0,x1,y1;
 loop:
 x1=rand()%620;
 y1=rand()%450;
 setbkcolor ( WHITE );
 



 
 again:
 while(!kbhit())
 {
  cleardevice();

              settextstyle(10 , 0 , 2);
  
  outtextxy(x1,y1,"~");
  for(i=1;i<=n;i++)
  {
     if(!kbhit())
   {
    x+=a;y+=b;
    if((x-x1)*(x1-x)>=-25 && (y-y1)*(y1-y)>=-25)
    {n++;goto loop;}

    setcolor ( RED );
    outtextxy(x,y,"");
    delay(prabh);
   }
   else
   {goto loop1;}
  }
 }
 loop1:
 ans=getch();
 if(ans=='w'||ans=='W' && b<=1 && b!=5)
 {a=0;b=-5;}
 if(ans=='s'||ans=='S' && b<=460 && b!=-5)
 {a=0;b=5;}
 if(ans=='a'||ans=='A' && a<=1 && a!=5)
 {a=-5;b=0;}
 if(ans=='d'||ans=='D' && a<=630 && a!=-5)
 {a=5;b=0;}
 if(ans=='q'||ans=='Q')
 {cout<<"GAME ABORTED";}
 if((x<0)|(y<0)|(x>640)|(y>480)) 
{cout<<"GAME ABORTED";        /* over here i want to write the code that can close the Windows BGI screen and return me to text console*/

}
 goto again;
 return 0;
  
}

#1: repaint the entire screen in the same color. Something like this:

RECT rect;
    HBRUSH hBrush;
<snip>
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        GetWindowRect(hWnd,&rect);
        hBrush = CreateSolidBrush(RGB(255,0,0));
        SelectObject(hdc,hBrush);
        FillRect(hdc, &rect, hBrush);
        EndPaint(hWnd, &ps);
        DeleteObject(hBrush);
        break;

#2: I know of no way to do that. The program is created for either text or windows, the two can not be mixed in the same program.

The program is created for either text or windows, the two can not be mixed in the same program.

AS U CAN SEE IN MY PROGRAMME first the text console opens and only after i enter a no according to difficulty level THE WINDOWS BGI screen opens.

NOW,the thing is i want the WINDOWS BGI SCREEn to terminate as soon as Snake crosses the windows border or the x,y position i give in the coding.Do u know any way of that

>> AS U CAN SEE IN MY PROGRAMME ...
Please don't SHOUT, thank you.

As for the problem, I believe closegraph() will do what you want.

I did't realize Dev-C++ could generate 16-bit code like Turbo C. dos.h is not supported with 32-bit compilers.

thanks for this code and i found my problem's solution in this code. got ride of the frustration. thank a lot for you ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.