Showing posts with label c programming. Show all posts
Showing posts with label c programming. Show all posts

Thursday, 21 March 2013

Mouse Programming in C

This is simple tutorial to show you How you can  use Mouse  in your C program.
I have Attached a source file. This program is not developed by me. I found it on internet somewhere, But as this program is easy to understand I am using this program.

See Source code here.


To Enable Mouse in C program you need to generate Interrupt.
The interrupt number for Mouse input related service is 33h.



Files to include -> dos.h
Function from dos.h to be use ->int int86(int intno,union REGS *input , union Regs *output)
This Function Generates Software interrupts

union - REGS is defined in dos.h file
it is defined as

union REGS{
struct WORDREGS   x;
struct BYTEREGS     y;
};

WORDREGS  & BYTEREGS are defined as

struct WORDREGS{
unsigned int ax,bx,cx,dx;
unsigned int si,di,cflag,flags;
};

struct BYTEREGS{
unsigned int al,ah,bl,bh;
unsigned int cl,ch,dl,dh;
};


You don't need to remember all this stuff. This is just for understanding.
If you have studied any  Micro Controller subject then you can easily identify that these ax,bx... indicates Registers.

So, let me first tell  what we exactly gonna do.

For every type of event there are Software interrupt number are defined.
for example using keyboard, sound, mouse.
Here for mouse interrupt number is 33h.
and for all types of operation like show mouse pointer, initialize mouse, restrict mouse pointer on screen etc.. we have different Service numbers.

Generally service number will be provided in
ax register,
so here we will write something like  i.x.ax

now all the input information will be provided in   variable of type REGS, and we will get out put in similar union varible

In our program we have declared
union i,o;
we will use
 i as input 
o as output, 

so this are some of service numbers for mouse



0~ Mouse Reset/Get Mouse Installed Flag
1~ Show Mouse Cursor
2~ Hide Mouse Cursor
3~ Get Mouse Position and Button Status
4~ Set Mouse Cursor Position
5~ Get Mouse Button Press Information
6~ Get Mouse Button Release Information
7~ Set Mouse Horizontal Min/Max Position
8~ Set Mouse Vertical Min/Max Position

This is example who you  can use this  service.

To initialize mouse

i.x.ax=0;
int86(33h,&i,&o);

done !!


to Restrict mouse pointer between square (10,10) to (20,20)

        i.x.ax=7;
 i.x.cx=10;
 i.x.dx=20;
 int86(0x33,&i,&o);

 i.x.ax=8;
 i.x.cx=10;
 i.x.dx=20;
 int86(0x33,&i,&o);

very simple right?
now Go through source code and  try your self.

Sunday, 17 March 2013

Keeper - A two player game in C

This is tutorial for creating a Two player game in C.

The name of game is keeper. It is very simple game and very easy to implement. Even if you are novice to Graphics programming in c, you will be able to grasp everything.

Download Source code from  Here





Required Structures :
  • pad : This structure will keep track of location of pad and size of pad. 
  • ball : This structure will keep track of ball size, position and direction of ball
  • limit : This structure consist the coordinates of boundary of  game area.
  • game_data : This structure will keep track of score.

struct pad
{
int x,y,l;
}pd1={100,200,30},pd2={440,200,30};

struct game_data
{
int score;
};
struct game_data p1={0},p2={0};

struct limit
{
int lx1,ly1,lx2,ly2;
};
struct limit l={90,90,450,400};

struct ball
{
int x,y;
int dir;
int size;
}ball={250,250,7,5};



There are several functions like game,draw, get_dir, mv_ball  etc.. all of them are self explainatory. comments are provided in the source code.

draw function will draw whole screen including boundaries, ball, pads.

draw(void)
{

rectangle(l.lx1,l.ly1,l.lx2,l.ly2);
circle(ball.x,ball.y,ball.size);
rectangle(pd1.x-5,pd1.y-30,pd1.x+5,pd1.y+30);
rectangle(pd2.x-5,pd2.y-30,pd2.x+5,pd2.y+30);
delay(15);
while(!kbhit()){goto e;}
k=getche();
if(k=='w' || k=='s'||k=='e'){key1=k;}
else if(k==72||k==80||k==77||k=='o'){key2=k;}
e:
cleardevice();

}


mv_ball will provide new coordinates of ball according to it's direction

void mv_ball(void)
{
switch(ball.dir)
{
case 0 : ball.y-=2; break;
case 8 : ball.y+=2; break;
case 2 : ball.x+=2; break;
case 6 : ball.x-=2; break;
case 1 : ball.x+=2; ball.y-=2; break;
case 3 : ball.x-=2; ball.y+=2; break;
case 7 : ball.x+=2; ball.y+=2; break;
case 5 : ball.x-=2; ball.y-=2; break;
}
}




game function is main function for game logic. It will check that whether ball hits boundary, pad , any key is pressed or not etc...


void game(void)
{
draw();
while(1)
{
      if(ball.y-l.ly1<=ball.size||l.ly2-ball.y<ball.size)
      {
        d=0;
        get_dir();
      }

      if((ball.x-105<=ball.size && abs(ball.y-pd1.y)<=32 )||(435-ball.x<=ball.size&& abs(ball.y-pd2.y)<=32))
      {
        d=1;
        get_dir();
      }



if(ball.x<l.lx1||l.lx2<ball.x)
{

if(ball.x<l.lx1)
{p2.score=p2.score+100;}
else
{p1.score=p1.score+100;}

ball.x=250;//+random(245);
ball.y=150;//+random(245);
pd1.y=250;
pd2.y=250;
      // ball.dir=random(7);
while(!kbhit()){ draw();}

}

sswitch(key1)
{
 case 'w':   if(pd1.y-pd1.l-l.ly1>10) pd1.y-=2;  break;
 case 's':   if(l.ly2-(pd1.y+pd1.l)>10) pd1.y+=2;  break;
 default : break;
}
switch(key2)
{
 case 72:    if(pd2.y-pd2.l-l.ly1>10) pd2.y-=2;  break;
 case 80:    if(l.ly2-(pd2.y+pd2.l)>10) pd2.y+=2;  break;
 case 'o' : gameover();
 default  : break;
}


      draw();

      mv_ball();


   }

}

When 'o' is pressed ,The game will be over and score of each player will be displayed.

If you have any query or suggestion then leave comment !





Thursday, 14 March 2013

SNAKE game using concept of Linked list - Graphics in C

This is tutorial for  creating a game using graphics in c.
If you know some concept of linked list then it will be very easy to understand. This is not standard way to create this particular. But, I have made this program to implement concept of Linked list !!
This is logical right?  when you need to increase length of snake in the game you can just add one item to the linked list !! this is it.
Thi
Download source code from here
If you you are working on windows 7.then you will need DosBox to run this program.

Structures :


struct loc
{
int x,y;
};

struct snake
{
struct loc sloc;
struct snake *link;
char dir;
};

struct game_data
{
int score;
int no_food;

};
struct game_data gd={0,0};

struct limit
{
int lx1,ly1,lx2,ly2;
};
struct limit l={96,96,404,404};


struct food
{
struct loc floc;
};



  • Structure "loc" have 2 integer variables to save x and y coordinates.
  • Structure "snake" is basically a linked list , new node of this structure will be created when snake will increase in length.
  • "game_data" will store score and number of food consumed by snake
  • "limit" defines the boundary of rectangle in which snake can move. crossing this limits will result in game over.
  • "food" contains coordinates of food to be displayed
  



Functions :

  • "draw" : This function will draw whole screen. which includes,
    1. Rectangular Boundary limits
    2. snake
    3. food
    draw(struct snake *head,struct food *f)
    {
    
    struct snake *temp;
    temp=head;
    
    rectangle(96,96,404,404);
    rectangle(98,98,402,402);
    rectangle(100,100,400,400);
    setfillstyle(9,13);
    bar(temp->sloc.x-6,temp->sloc.y-6,temp->sloc.x+6,temp->sloc.y+6);
    temp=temp->link;
    setfillstyle(9,2);
    while(temp->link!=NULL)
    {
    
    bar(temp->sloc.x-5,temp->sloc.y-5,temp->sloc.x+5,temp->sloc.y+5);
    temp=temp->link;
    }
    
    bar(temp->sloc.x-5,temp->sloc.y-5,temp->sloc.x+5,temp->sloc.y+5);
    
    circle(f->floc.x-2,f->floc.y-2,5);
    circle(f->floc.x+2,f->floc.y+2,5);
    circle(f->floc.x-2,f->floc.y+2,5);
    circle(f->floc.x+2,f->floc.y-2,5);
    
    delay(20);
    while(!kbhit()){goto e;}
    key=getche();
    e:
    
          cleardevice();
    
    }
    
  • "gameover": This function will draw screen when game is over, it includes score and number of food consumed.
  • gameover(void)
    {
    cleardevice();
    outtextxy(100,100,"----------------- GAME OVER ----------------------");
    
    printf("\n\n\n\n\n\n\n\n");
    printf("\t\t# Score      - %d",gd.score);
    printf("\n\t\t# No of food - %d", gd.no_food);
    s1:
    sound(300);delay(300);sound(450);delay(150);sound(500);delay(150);
    sound(300);delay(200);sound(450);delay(100);sound(450);delay(200);
    while(!kbhit()){goto s1;}
    nosound();
    
          exit(0);
    }
    
  • "game" : This is main logic of our snake game. It does following tasks in sequence within a while loop.
    (while loop checks whether 'p' (for pause) is pressed or not.)
    1. checking condition whether snake hits boundary. if it hits boundary then call "gameover" function
    2. check whether snake consumes food or not.
      if it consumes food then generate new food at new location, increase score and food count.
      and create new node of snake structure to increase the length of snake.
    3. check whether any key is pressed to change direction of snake. according to that set the direction of snakes head.
    4. update direction and location of all node of snake body.
    void game(struct snake *head,struct food *f)
    {
         struct snake *temp,pre,nxt;
         temp=head;
    
         while(key!='p')
         {
    if(head->sloc.x==l.lx1||head->sloc.x==l.ly2||head->sloc.y==l.ly1||head->sloc.y==l.ly2)
    {gameover();}
    
    
    
    if(head->sloc.x>=f->floc.x-5&&head->sloc.x<=f->floc.x+5&&head->sloc.y>=f->floc.y-5&&head->sloc.y<=f->floc.y+5)
    {
    temp=head;
    sound(420);
    
    f->floc.x=150+random(245);
    f->floc.y=150+random(245);
    gd.score+=100;
    gd.no_food+=1;
    n=n+1;
    
    
    while(temp->link!=NULL){temp=temp->link;}
    temp->link=(struct snake *)malloc(sizeof(struct snake));
    temp->link->link=NULL;
    temp->link->sloc.x= temp->sloc.x;
    temp->link->sloc.y= temp->sloc.y;
    temp->link->dir=temp->dir;
    n=0;
    
    
      }
    
    switch(key) //this key varible is set in draw() function
    {
     case 'a': if(head->dir!='d'){head->dir='a';  head->sloc.x-=2; } else {key=head->dir;} break;
     case 'w': if(head->dir!='s'){head->dir='w';  head->sloc.y-=2; } else {key=head->dir;} break;
     case 'd': if(head->dir!='a'){head->dir='d';  head->sloc.x+=2; } else {key=head->dir;} break;
     case 's': if(head->dir!='w'){head->dir='s';  head->sloc.y+=2; } else {key=head->dir;} break;
    
    }
          draw(head,f);
          nosound();
          temp=head;
          pre=*temp;
    
          while(temp->link!=NULL)
    {
    nxt.sloc.x=temp->link->sloc.x;
    nxt.sloc.y=temp->link->sloc.y;
    nxt.dir=temp->link->dir;
    temp->link->sloc.x=pre.sloc.x;
    temp->link->sloc.y=pre.sloc.y;
    temp->link->dir=pre.dir;
    temp=temp->link;
    pre=nxt;
    }
       }
    
    }
    
Now you can download the source code of the game and run on your own pc.
make changes and try to modify this game.
If you have any query please leave a comment.






Wednesday, 13 March 2013

Tower Of Hanoi - Graphics in C

Hello Friends,
This is simple Tutorial about Graphics in C with example program  "Tower of Hanoi" problem.
Hope you all know about tower of Hanoi problem. If you don't know , google it :)
or visit this wikipedia page :  Tower Of Hanoi

Here is link to source file to c program : Click here

Note : program of c graphics works on 16-bit pc.
it will work on Windows xp. If you have windows 7 then you will need Dosbox to run this program.

If you traverse the program from main() function , then it is self explanatory , if you need help go through the explanation given below.





















Structures :
  • loc    : containg to integers x, y  for storing coordinates of particular object
  • struct loc
    {
    int x;
    int y;
    };
    
  • bar   : It contains two variables of loc structure. one is for permanent location of bar called barloc, other is avloc which is to store on which location a ring can b put on bar.
  • struct bar
    {
    struct loc barloc;
    struct loc avloc;
    };
    
  • ring   : This structure have variable to store location of particular ring and the size of ring (top most ring have smaller size, bottom most ring have biggest size)
  • struct ring
    {
    struct loc rloc;
    int size;
    };
    
Functions :
  • draw : this function will draw whole screen,
     first it will put all the lables on the screen
    then it will draw all bars on the screen
    then all the rings will be drawn
    void draw(struct bar a,struct bar b,struct bar c,struct ring r[],int no)
    {    int i;
        outtextxy(250,30, "TOWER OF HANOI");
        outtextxy(60, 330, "SOURCE");
        outtextxy(240,330, "DESTINATION");
        outtextxy(470,330, "TEMPORARY");
        setfillstyle(8,13);
        bar(a.barloc.x-2, a.barloc.y-200, a.barloc.x+1, a.barloc.y);
        bar(b.barloc.x-2, b.barloc.y-200, b.barloc.x+1, b.barloc.y);
        bar(c.barloc.x-2, c.barloc.y-200, c.barloc.x+1, c.barloc.y);
    
    
    for(i=1;i<=no;i++)
    {
      setfillstyle(8+(i%2),10 /*getmaxcolor()*/);
      bar(r[i].rloc.x-r[i].size,r[i].rloc.y-20,r[i].rloc.x+r[i].size,r[i].rloc.y);
    
    }
    getch();
    cleardevice();
    
    };
  • towers : This function is recursive function. to under stand the logic go through this wikipedia page
  • void towers (int n,struct bar *from,struct bar *to,struct bar *aux,struct ring r[],int no)
    {
    int i=1;
    if(n==1)
    {
    (r[1].rloc.x)= to->avloc.x;
    (r[1].rloc.y)= to->avloc.y;
    if(from->avloc.y!=300){from->avloc.y+=20; }
    to->avloc.y-=20;
    draw(*from,*to,*aux,r,no);
    return;
    }
    
    towers(n-1,from,aux,to,r,no);
    
    while(from->avloc.x!=r[i].rloc.x){i=i+1;}
    
    (r[i].rloc.x)=to->avloc.x;
    (r[i].rloc.y)=to->avloc.y;
    if(from->avloc.y!=300){from->avloc.y+=20; }
    to->avloc.y-=20;
    
    draw(*from,*to,*aux,r,no);
    
    towers(n-1,aux,to,from,r,no);
    
    }
    
I hope this will help.
If you have any query then leave comment, and suggestions are always welcome on this blog !