Thursday, November 11, 2010

Magic Game with source Code written in C language

Select Any number from 1 to 60 in your mind and Computer will tell you which number you have selected.
Download this Cool game with source code and implement it.


After receiving many emails, finally i am writing the full steps that how to create and deploy simple graphical games in c language.
This tutorial will help to those who are learning C language and want to develope some Graphical Application.


Game: This is simple game which will tell the user that which number is in his Mind.

User Inputs: User just presses 'Y' only if the number which is in his mind is present in the list or not.
Logic: Adjusted the number between 1 to 60 in 6 different sheets. That if the number is presented in the list.
If number is presented in the sheet then the upper left number is added with previous one.

Code:
/*==================================================================*/
/* This is simple program to get number which is in mind of the user*/
/*Programmer : Vivek Muthal*/
/*Website : http://www.vivsoftware.co.cc*/

/*liabrary files*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int left,right,top,bottom;
int n;
int answer=0;
char resp;

void main();

void setborder()
{
 cleardevice();
 rectangle(left,top,right,bottom);
}
void printanswer()
{
  printf("\n\n\n\n\tMAGIC GAME");
  printf("\n\n\n\n\n\n\n\t\tThe number in you mind is %d",answer);
  printf("\n\n\n\n\n\tProgrammer : VIVEK MUTHAL");
  printf("\n\tWebsite : http://www.vivsoftware.co.cc");
  printf("\n\n\n\tDo you want to continue? Y-Yes  Anykeys-No");
  resp=getche();
  if(resp=='y'||resp=='Y')
  {
   resp='n';
   main();
  }
}
void screen6()
{
 setcolor(WHITE);
 outtextxy(80,70," 32 37 42 47 52 57");
 outtextxy(80,110," 33 38 43 48 53 58");
 outtextxy(80,150," 34 39 44 49 54 59");
 outtextxy(80,190," 35 40 45 50 55 60");
 outtextxy(80,230," 36 41 46 51 56 ");
 setcolor(RED);
 outtextxy(15,300,"Press 'Y' if number is present");
 outtextxy(15,340," the list else press any key");
 resp=getche();
 if(resp=='y'||resp=='Y')
 {
  answer+=32;
  resp='n';
 }
 setbkcolor(BLACK);
 setborder();
 printanswer();
}
void screen5()
{
 setcolor(WHITE);
 outtextxy(80,70," 16 21 26 31 52 57");
 outtextxy(80,110," 17 22 27 48 53 58");
 outtextxy(80,150," 18 23 28 49 54 59");
 outtextxy(80,190," 19 24 29 50 55 60");
 outtextxy(80,230," 20 25 30 51 56 ");
 setcolor(RED);
 outtextxy(15,300,"Press 'Y' if number is present");
 outtextxy(15,340," the list else press any key");
 resp=getche();
 if(resp=='y'||resp=='Y')
 {
  answer+=16;
  resp='n';
 }
 setbkcolor(LIGHTBLUE);
 setborder();
 screen6();
}
void screen4()
{
 setcolor(WHITE);
 outtextxy(80,70,"  8 13 26 31 44 57");
 outtextxy(80,110,"  9 14 27 40 45 58");
 outtextxy(80,150," 10 15 28 41 46 59");
 outtextxy(80,190," 11 24 29 42 47 60");
 outtextxy(80,230," 12 25 30 43 56 ");
 setcolor(RED);
 outtextxy(15,300,"Press 'Y' if number is present");
 outtextxy(15,340," the list else press any key");
 resp=getche();
 if(resp=='y'||resp=='Y')
 {
  answer+=8;
  resp='n';
 }
 setbkcolor(BLACK);
 setborder();
 screen5();
}
void screen3()
{
 setcolor(WHITE);
 outtextxy(80,70,"  4 13 22 31 44 53");
 outtextxy(80,110,"  5 14 23 36 45 54");
 outtextxy(80,150,"  6 15 28 37 46 55");
 outtextxy(80,190,"  7 20 29 38 47 60");
 outtextxy(80,230," 12 21 30 39 52 ");
 setcolor(RED);
 outtextxy(15,300,"Press 'Y' if number is present");
 outtextxy(15,340," the list else press any key");
 resp=getche();
 if(resp=='y'||resp=='Y')
 {
  answer+=4;
  resp='n';
 }
 setbkcolor(BLUE);
 setborder();
 screen4();
}
void screen2()
{
 setcolor(WHITE);
 outtextxy(80,70,"  2 11 22 31 42 51");
 outtextxy(80,110,"  3 14 23 34 43 54");
 outtextxy(80,150,"  6 15 26 35 46 55");
 outtextxy(80,190,"  7 18 27 38 47 58");
 outtextxy(80,230," 10 19 30 39 50 59");
 setcolor(RED);
 outtextxy(15,300,"Press 'Y' if number is present");
 outtextxy(15,340," the list else press any key");
 resp=getche();
 if(resp=='y'||resp=='Y')
 {
  answer+=2;
  resp='n';
 }
 setbkcolor(3);
 setborder();
 screen3();
}
void screen1()
{
 setcolor(WHITE);
 outtextxy(80,70," 1 11 21 31 41 51");
 outtextxy(80,110," 3 13 23 33 46 53");
 outtextxy(80,150," 5 15 25 35 45 55");
 outtextxy(80,190," 7 17 27 37 47 57");
 outtextxy(80,230," 9 19 29 39 49 59");
 setcolor(RED);
 outtextxy(15,300,"Press 'Y' if number is present");
 outtextxy(15,340," the list else press any key");
 resp=getche();
 if(resp=='y'||resp=='Y')
 {
  answer+=1;
  resp='n';
 }
 setbkcolor(MAGENTA);
 setborder();
 screen2();
}
void mainscreen()
{
 setcolor(RED);
 setbkcolor(BLACK);
 settextstyle(3,0,6);
 outtextxy(100,50,"Select any number");
 outtextxy(100,100," between 1 to 60");
 outtextxy(100,150,"  in your mind...");
 settextstyle(3,0,5);
 outtextxy(50,290," Press Enter to Continue...");
 getch();
 setbkcolor(GREEN);
 setborder();
 screen1();
}
void graphicsdo()
{
 /* initialize graphics and local variables */
  initgraph(&gdriver, &gmode, "");

 /* read result of initialization */
 errorcode = graphresult();
 if (errorcode != grOk)  /* an error occurred */
 {
   printf("Graphics error: %s\n", grapherrormsg(errorcode));
   printf("Press any key to halt:");
   getch();
   exit(1); /* terminate with an error code */
 }

 midx = getmaxx() / 2;
 midy = getmaxy() / 2;
 setcolor(1);
 left = 10;
 top = 10;
 right = getmaxx()-10;
 bottom = getmaxy()-10;

 /* draw a rectangle */
 setlinestyle(2,0,0);
 setborder();
}
void main()
{
 answer=0;
 graphicsdo(); /// this is graphics initialization function
 mainscreen();
 closegraph();
}

/*==================================================================*/

Many says that they develop the graphics in C but the game, application runs only on that directory and only on their machine, cause they forget to add additional files to run the application in GUI and with fonts.
There fore you have to add those files where your application (executable) is stored.

To deploy the application and make it full functional you have to add all those font files named as .CHR which fonts you have used in the application in this gamei only usedone font so that only copy pasted that font file in the directory and for all other details of files you can see the magic game folder. 

Download the application with Source Code and see which extra files are included to run the game on different machines.
 Also You can contact me for any information at any time i'll be there to help you.
Email : vmuthal.18@gmail.com  

Here i request all the peoples who want to publish these article on there blog, forum, website kindly please make a link back to the original post or to the website. 
i.e. http://vivekgamesinc.blogspot.com
or
to owner site http://www.vivsoftware.co.cc