Tuesday 14 July 2015

"Click Me" Game in C

Run this program and try to click the button shown, you wont be able to do it. Try it. It's fun !!
Even try to manipulate the code and use your own innovative to make something more fun out of it :)
graphics click me program in C language

Note: This program uses mouse programming and I have defined the functions used by mouse with in this program.
If you want to use all the functions required in mouse programming here is the Mouse.C file, you can include it in your code( Copy the Mouse.C file in your library and use #include<Mouse.C>) and use all the functions (if you don't want to define these functions every time you use them.)
Secondly, this is something I have mentioned in every Program under this label, Don't forget to change the path in the initgraph() according to your system.

Here's the Code:


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

union REGS i, o;
int left = 265, top = 250;

void initialize_graphics_mode()
{
  int gd = DETECT, gm, error;

  initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

  error = graphresult();

  if (error != grOk)
  {
    perror("Error ");
    printf("Press any key to exit...\n");
    getch();
    exit(EXIT_FAILURE);
  }
}

void showmouseptr()
{
  i.x.ax = 1;
  int86(0x33,&i,&o);
}

void hidemouseptr()
{
  i.x.ax = 2;
  int86(0x33,&i,&o);
}

void getmousepos(int *x,int *y)
{
  i.x.ax = 3;
  int86(0x33,&i,&o);

  *x = o.x.cx;
  *y = o.x.dx;
}

void main_box()
{
  hidemouseptr();
  setfillstyle(SOLID_FILL,CYAN);
  bar(200,180,450,350);
  showmouseptr();
}

void button(int x, int y)
{
  hidemouseptr();
  setfillstyle(SOLID_FILL,RED);
  bar(x,y,x+100,y+30);
  moveto(x+5,y);
  setcolor(YELLOW);
  outtextxy(left+10,top+10,"Click me");
  showmouseptr();
}

void draw()
{
  setcolor(BLUE);
  setcolor(RED);
  outtextxy(180,370,"Try to Click the \"Click me\" button");
  outtextxy(210,390,"Press escape key to exit");
  setcolor(BLUE);
  showmouseptr();
  main_box();
  button(left,top);
}

void initialize()
{
  initialize_graphics_mode();

  if( !initmouse() )
  {
    closegraph();
    printf("Unable to initialize the mouse");
    printf("Press any key to exit...\n");
    getch();
    exit(EXIT_SUCCESS);
  }

  draw();
}

int initmouse()
{
  i.x.ax = 0;
  int86(0x33,&i,&o);
  return ( o.x.ax );
}

void getCursorInput()
{
  int x, y;

  while(1)
  {
    getmousepos(&x,&y);

    /* mouse pointer in left of button */

    if( x >= (left-3) && x<left+100&& y >=top && y <= (top+30) )
    {
      main_box();
      left = left + 4;

      if (left > 350)
left = 200;

      button(left,top);
    }

    /* mouse pointer inside button */


    /* mouse pointer in right of button */

    else if ( x>=left && x<=(left+100+3) && y>=(top) && y<=(top+30) )
    {
      main_box();
      left = left - 4;

      if (left < 200)
left = 350;

      button(left,top);
    }

    /* mouse pointer above button */

    else if(x>(left) && y>=(top-3) && y<(top+30) && x<= (left+100))
    {
      main_box();
      top = top + 4;

      if (top > 320)
top = 180;

      button(left,top);
    }

    /* mouse pointer below button */

    else if (x>(left)&& y>top && y<=(top+30+3)&&x<=(left+100))
    {
      main_box();
      top = top - 4;

      if (top < 180)
top = 320;

      button(left,top);
    }
    if (kbhit())
    {
      if (getkey() == 1 )
exit(EXIT_SUCCESS);
    }
  }
}

int getkey()
{
  i.h.ah = 0;
  int86(22,&i,&o);

  return( o.h.ah );
}

main()
{
  initialize();

  getCursorInput();
  return 0;
}

No comments:

Post a Comment