Number Catcher Game in C++

By
Advertisement
Number catcher is a advance game in c programming. Random numbers are falling down and you have a robot to collect that numbers .Move robot right or left with arrow keys .You have three minutes so, score as much as you can. Zero can reset your score avoid catching zero. Develop by the students of  FAST university .
Comments are with every function so you can easily understand how this game works if you have any query you can comment below.




Note : Please do not edit copy right tag.
#include<iostream>
#include<iomanip>
#include<conio.h>  //support rand function
#include <windows.h> //support different color commands
#include<ctime>  
using namespace std;
int mx=40;   //"mx" defines from which column robot is going to start
int my=21;   //"my" deines from which row robot is going to start 
int i, j;
int m;  //m for min
int s;  //s for second
char grid[24][80];//2D grid of 24x80
int rNumber;
int rPosition;
int score;  //stores score 
void gotoxy(int x, int y) //move the cursor to x y coordinate
{
 HANDLE hConsoleOutput;
 COORD Cursor = {x,y};
 hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
 SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
void robot()
/* structure i-e body of robot at the mx & my original coordinates  */
{
 grid[my][mx] = 220;  //robot foot
 grid[my][mx+3] = 220;  //robot foot
 grid[my][mx+1] = '|';  //legs
 grid[my][mx+2] = '|';  //legs
 grid[my-1][mx+1] = '_';  //body
 grid[my-1][mx+2] = '_';  //body
 grid[my-1][mx+4] = '|';  //robot arm
 grid[my-2][mx+3] = '_';  //robot neck
 grid[my-2][mx+2] = '|';  //robot neck
 grid[my-2][mx+1] = '_';  //robot shoulder
 grid[my-2][mx+4] = '_';  //robot shoulder
 grid[my-2][mx] = '|';  //robot arm
 grid[my-3][mx] = '|';  //robot arm
 grid[my-3][mx+2] = 2;  //robot head
 grid[my-3][mx+1] = 216;  //robot horns
 grid[my-3][mx+3] = 216;  //robot horns
 grid[my-4][mx-2] = '_';  //tray
 grid[my-4][mx] = '_';  //tray
 grid[my-4][mx-1] = '_';  //tray
 grid[my-4][mx+1] = '_';  //tray
 grid[my-4][mx-2] = '\\'; //tray end
 grid[my-4][mx+2] = '/';  //tray end
}
void moverobot()
/* make the boody of robot to the new coordinates every time when robot moves  */
{
 grid[my][mx] = 220;  //robot foot
 grid[my][mx+3] = 220;  //robot foot
 grid[my][mx+1] = '|';  //legs
 grid[my][mx+2] = '|';  //legs
 grid[my-1][mx+1] = '_';  //body
 grid[my-1][mx+2] = '_';  //body
 grid[my-1][mx+4] = '|';  //robot arm
 grid[my-2][mx+3] = '_';  //robot neck
 grid[my-2][mx+2] = '|';  //robot neck
 grid[my-2][mx+1] = '_';  //robot shoulder
 grid[my-2][mx] = '_';
 grid[my-2][mx+4] = '_';  //robot shoulder
 grid[my-2][mx] = '|';  //robot arm
 grid[my-3][mx] = '|';  //robot arm
 grid[my-3][mx+2] = 2;  //robot head
 grid[my-3][mx+1] = 216;  //robot horns
 grid[my-3][mx+3] = 216;  //robot horns
 grid[my-4][mx-2] = '_';  //tray
 grid[my-4][mx] = '_';  //tray
 grid[my-4][mx-1] = '_';  //tray
 grid[my-4][mx+1] = '_';  //tray
 grid[my-4][mx-2] = '\\'; //tray end
 grid[my-4][mx+2] = '/';  //tray end 
}
void updatescores(char sc)    //Q. it is open for int and long as well but not for decimal number data type.
{
 switch(sc)         /*Q. every action is associated with const integral expression
                       (combination of char or int const that evaluate to const int value)*/
      {                                                                                     
  case '0':
   score = score * 0; //multiply current score with zero
  break;
  case '1':
   score = score + 1; //add 1 to current score
  break;
  case '2':
   score = score + 2; //add 2 to current score
  break;
  case '3':
   score = score + 3; //add 3 to current score
  break;
  case '4':
   score = score + 4; //add 4 to current score
  break;
  case '5': 
   score = score + 5; //add 5 to current score
  break;
  case '6':
   score = score + 6; //add 6 to current score
  break;
  default:
   break;
 }
}
int pickRandom(int min, int max)      //Q. since % only supports int operands for only int data type is accepted  
{ 
 //srand(time(NULL));  
 
    int num = rand() % (max - min + 1) + min;  //it is the range of rand function
 
 return num;
}
void setrobotspace()
/* set spaces to the body of robot and to the new coordinates every time when robot moves */ 
{
 grid[my][mx] = ' ';      
 grid[my][mx+3] = ' ';
 grid[my][mx+1] = ' ';
 grid[my][mx+2] = ' ';
 grid[my-1][mx+1] = ' ';
 grid[my-1][mx+2] = ' ';
 grid[my-1][mx+4] = ' ';
 grid[my-2][mx+3] = ' ';
 grid[my-2][mx+2] = ' ';
 grid[my-2][mx+1] = ' ';
 grid[my-2][mx] = ' ';
 grid[my-2][mx+4] = ' ';
 grid[my-2][mx] = ' ';
 grid[my-3][mx] = ' ';
 grid[my-3][mx+2] = ' ';
 grid[my-3][mx+1] = ' ';
 grid[my-3][mx+3] = ' ';
 grid[my-4][mx] = ' ';
 grid[my-4][mx-1] = ' ';
 grid[my-4][mx+1] = ' ';
 grid[my-4][mx-1] = ' ';
 grid[my-4][mx+2] = ' ';
 grid[my-4][mx-2] = ' ';
 grid[my-4][mx-2] = ' ';
}
void setrobotvspace()
/* set spaces to the body of robot and to the new coordinates every time when robot moves */ 
{
 grid[my-3][mx] = ' ';  
 grid[my-3][mx-1] = ' ';  
 grid[my-3][mx+1] = ' ';  
 grid[my-3][mx-2] = ' '; 
 grid[my-3][mx+2] = ' ';
 grid[my-3][mx+3] = ' ';
 grid[my-3][mx+4] = ' ';
 grid[my-2][mx] = ' ';  
 grid[my-2][mx-1] = ' ';  
 grid[my-2][mx+1] = ' ';  
 grid[my-2][mx-2] = ' '; 
 grid[my-2][mx+2] = ' ';
 grid[my-2][mx+3] = ' ';
 grid[my-2][mx+4] = ' ';
 grid[my-1][mx] = ' ';  
 grid[my-1][mx-1] = ' ';  
 grid[my-1][mx+1] = ' ';
 grid[my-1][mx+3] = ' ';
 grid[my-1][mx+4] = ' ';
 grid[my-1][mx-2] = ' '; 
 grid[my-1][mx+2] = ' ';
 grid[my][mx] = ' ';  
 grid[my][mx-1] = ' ';  
 grid[my][mx+1] = ' ';  
 grid[my][mx-2] = ' '; 
 grid[my][mx+2] = ' ';
 grid[my][mx+3] = ' ';
 grid[my][mx+4] = ' ';
 grid[my+1][mx] = ' ';  
 grid[my+1][mx-1] = ' ';  
 grid[my+1][mx+1] = ' ';  
 grid[my+1][mx-2] = ' '; 
 grid[my+1][mx+2] = ' ';
 grid[my+1][mx+3] = ' ';
 grid[my+1][mx+4] = ' ';
} 
void loadgrid()  //loads the grid
{
 for (i=0; i<24; i++)
 {
  for (j=0; j<79; j++)
  {
   grid[i][j]=' ';  //prints spaces from 0 to 22nd row and 0 to 78th column
  }
 }
}
void randomgridnumber(int number, int position)
{
 for(int k = 1; k<78; k++)
    {
  grid[1][k] = ' ';
 }
 grid[1][position] = number;
 for( int k = 22; k>1 ; k--)
    {
  for(int l = 1; l<78; l++){
   if(k == my - 4)
            {
    if(l == mx || l == mx - 1 || l == mx + 1 || l == mx + 2 || l == mx - 2)
                {     updatescores(grid[k-1][l]);
    }
   }
   grid[k][l] = grid[k-1][l];
  }
 }
 setrobotvspace();
 robot();
}
void printgrid()
{   
 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleTextAttribute(hConsole, ( FOREGROUND_BLUE | FOREGROUND_RED|FOREGROUND_GREEN));
 cout<<"COPY RIGHTS:";
 cout<<" cpluspluspoint.blogspot.com";
    
 SetConsoleTextAttribute(hConsole, ( FOREGROUND_BLUE |FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_BLUE)); //used to display BLUE COLOR with WHITE COLORED text
 gotoxy(0,0); 
 for (i=0; i<19; i++)
 {
  for (j=0; j<79; j++)
 {
  cout<<grid[i][j];
 }
 cout<<endl;
 }
 SetConsoleTextAttribute(hConsole, ( FOREGROUND_BLUE |FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_GREEN)); //used to display GREEN COLOR with WHITE COLORED text
 for (i=19; i<22; i++)
 {
  for (j=0; j<79; j++)
 {
  cout<<grid[i][j];
 }
 cout<<endl;
 }
 SetConsoleTextAttribute(hConsole, (FOREGROUND_BLUE |FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_INTENSITY |  BACKGROUND_RED | BACKGROUND_GREEN)); //used to display intense YELLOW COLOR with WHITE COLORED text
 for (i=22; i<23; i++)
 {
  for (j=0; j<79; j++)
 {
  cout<<grid[i][j];
 }
 cout<<endl;
 }
 //---------FOR LEAF BORDERS---------
 for (i=0;i<24;i++)
 { grid[i][0]=(char)6;  //displays leaf border to the left side of the screen
 }
 for (i=0;i<24;i++)
 { grid[i][78]=(char)6; //displays leaf border to the right side of the screen
 }
 for (j=0;j<78;j++)
 { grid[0][j]=(char)6;  //displays leaf border to the top of the screen 
    }
 SetConsoleTextAttribute(hConsole, ( BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE |  FOREGROUND_GREEN | FOREGROUND_INTENSITY ));
    for (int j=0;j<79;j++)
    {
     cout<<grid[23][j];
     }
 gotoxy(7,23);
 cout<<"Score :"<<score; 
 gotoxy(57,23);
 cout<<"Time Remaining "<<m<<":"<<s;  //displays time starting from 3minutes
 cout<<endl;
}
int main()
{
 int choice;
 do
 {   // do start
     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleTextAttribute(hConsole, ( BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE |  FOREGROUND_GREEN | FOREGROUND_INTENSITY ));
 char a[24][80]={0};
for (int q=0;q<24;q++)
{
 for(int w=0;w<79;w++)
 {
  cout<<a[q][w];
 }
 cout<<endl;
}
  gotoxy(6,3); //diplays text in MENU 
  cout<<"\t\n 1 - Start Game\n"<< " 2 - About\n"<< " 3 - Instructions\n" << " 4 - Exit\n"<<"\n\n\nWarning:\n\tCoping and reproducing in whole or in part is strictly prohibited.\n\t    This game is registered trademark of its rightful owners.";
  gotoxy(29,10);
        cout<< " Enter your choice : ";
  cin >> choice;
        gotoxy(30,17);
     cout<<"Loading As Selected";
     for(int i=0;i<16;i++)
{       gotoxy(8+i,18);
        cout<<"\n\t\t\t\t ________________";
        gotoxy(32,20);
        cout<<char(179);
        gotoxy(33+i,20);
        cout<<char(178);
        gotoxy(23+i,20);
        cout<<"\n\t\t\t\t ================";
        gotoxy(49,20);
        cout<<char(179);
        Sleep(400);
}
  switch (choice)
  {
  case 1:
  score = 0;
  rNumber = pickRandom(48,54); 
        // for x cordinates;
  rPosition = pickRandom(1,76);
  loadgrid();
     robot();
        printgrid();
 for (m=2;m>=0;m--) //mints
 { 
  for (s=59;s>=0;s--)   //seconds
  
  {
   for(int mili = 10; mili > 0; mili--)         // if this loop was ignored then numbers drop rate= sleep(1000)
            {  //miliseconds 10mili sec are equal to one sec
    randomgridnumber(rNumber, rPosition);
    Sleep(1);               
    if (GetAsyncKeyState(VK_LEFT) < 0){
       if(mx > 3)   //prevents the ROBOT from going outside the grid // hearthgrid\_(3) // grid disturbs
     {
      setrobotspace();
      mx=mx-1;
      moverobot();
     }
    }
    if (GetAsyncKeyState(VK_RIGHT) < 0){
       if(mx < 73)  //prevents the ROBOT from going outside the grid
     {
      setrobotspace();
      mx=mx+1;
      moverobot();
     }
     }
    printgrid();
    rNumber = pickRandom(48,54); 
    rPosition = pickRandom(1,76);
   }
  }
 }
  break;
  case 2:
  cout<<"\n\n\Shared by on fb/cpluspluspoint";
  break;
  case 3:
  cout << "\nMove the robot LEFT or RIGHT to collect the falling numbers. \nYou have three minutes so, score as much as you can.\nZero can reset your score avoid catching zero. \n";
  break;
  case 4:
  cout<<"Exit\n";
  default:
                gotoxy(28,15);
  cout << "Not a Valid Choice. \n";
  break;
  }
 } //do end
 
 
 while (choice!=4);
return 0;          
system("pause");
}



Number Catcher in C++



0 comments:

Post a Comment