Search This Blog

Friday, 30 November 2012

How to make the numbers fall in console?

#include<iostream>
#include<windows.h>
using namespace std;
//Function to move the cursor.
void gotoxy(int x, int y)
{
HANDLE console_handle;
COORD cursor_coord;
cursor_coord.X=x;
cursor_coord.Y=y;
console_handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(console_handle,cursor_coord);
}

int main()
{
for (int row = 0; row<25; row++)
{
gotoxy(25,row);
cout<<"5";
Sleep(200);
gotoxy(25,row);
cout<<" ";
}
}
//=========================================================================
Now, it's easy if you want to change your column. Just add a simple condition.
I recommend, before you goto the following program, try to think, what the condition can be?

//Program to drop a number in different column.
#include<iostream>
#include<windows.h>
using namespace std;
//Function to move the cursor.
void gotoxy(int x, int y)
{
 HANDLE console_handle;
 COORD cursor_coord;
 cursor_coord.X=x;
 cursor_coord.Y=y;
 console_handle=GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleCursorPosition(console_handle,cursor_coord);
}
int main()
{
 int column = rand() % 80; //80 because Total columns are 0-79. i.e. less than 80.
 for (int row = 0; row<25; row++)
 {
  gotoxy(column,row);
  cout<<"5";
  Sleep(200);
  gotoxy(column,row);
  cout<<" ";
  //Now we add another condition so that our number again comes at first row and a different
  if (row == 24) // last line of console screen.
  {
   row = 0;
   column = rand() % 80;
  }
 }
}
//========================================================================

Similarly you can increase the number of Numbers falling downward.
//Program for falling two numbers in changing columns.
#include<iostream>
#include<windows.h>
using namespace std;
//Function to move the cursor.
void gotoxy(int x, int y)
{
 HANDLE console_handle;
 COORD cursor_coord;
 cursor_coord.X=x;
 cursor_coord.Y=y;
 console_handle=GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleCursorPosition(console_handle,cursor_coord);
}
int main()
{
 int column = rand() % 80; //80 because Total columns are 0-79. i.e. less than 80.
 int column1 = rand() % 80;
 for (int row = 0; row<25; row++) 
 {
  gotoxy(column,row);
  cout<<"2";//first number.
  gotoxy(column1,row);
  cout<<"9";
  Sleep(200);
  gotoxy(column,row);
  cout<<" ";
  gotoxy(column1,row);
  cout<<" ";
  //Now we add another condition so that our number again comes at first row and a different
  if (row == 24)
  {
   row = 0;
   column = rand() % 80;
   column1 = rand() % 80;
  }
 }
}

No comments:

Post a Comment