.. www.engineerscur.blogspot.com: Make simple GAME in turbo c

Make simple GAME in turbo c


Make simple GAME in turbo c

                  It is very simple to make simple games in c language.Only you need to know some basics of c language to understand this code and make your own.In case for beginners just go and understand basics of c language then it will be easy for you to understand the code.      :)

               

                     Game looks like above image after running it in c compiler in my case it is DOS-box.As I said before it is very simple Game but this game is just base rest of the things you can easily develop yourself.So enjoy making it and happy coding...

                    Working:

                I made it able to detect arrow keys(up,down,right&left) so that we can control anything with arrow keys when pressed.This code prints stars(*) when key is pressed in the arrow key's direction.When you will go back the same direction it will erase the line made by stars.
               Below the control keys and it's functions are given.

                                                  Key                       Function
                                        -------------------------------------------
                                             Arrow keys              For navigation
                                                    c                      To clear screen
                                                   q                             Exit
                                                   Esc                             Exit

                So copy below code play it !Modify it And enjoy coding........


//#include<graphics.h>           //Made by dharmesh vasoya //engineerscur.blogspot.in
#include<stdio.h>
#include<conio.h>
#include<dos.h>
static int store[81][31];
int x=2,y=2;
void canvas();
void main()
{
int a,dh,dk;

char c;

//int Gdriver=0,gmode;

/*for(dh=0;dh<=23;dh++)
{
for(dk=0;dk<=77;dk++)
{
store[dk][dh]=0;
}
} */

//initgraph(&Gdriver,&gmode ,"c:\\tc\\bgi");
canvas();
//setbkcolor(3);
while(1)
{
a=getch();


if(a==0 || a==224)
{
switch(getch())
{
case 72:  //up arrow
{if(y>2)
{
y=y-1;
}
else
{
y=y;
}
break;
}

case 75:  //left arrow
{if(x>2)
{
x=x-1;
}
else
{
x=x;
}
break;
}
case 77:  //right arrow
{if(x<78)
{
x=x+1;
}
else
{x=x;
}
break;
}
case 80: //down arrow
      { if(y<24)
{
y=y+1;
}
else
{y=y;
}
break;
}
}
}
{
if(store[x][y]!=1)
{
store[x][y]=1;
gotoxy(x,y);
printf("*");
}
else
{
store[x][y]=0;
gotoxy(x,y);
printf(" ");

}
}
if(a==113 || a==27)    /////113-->q-->exit(quit) 27-->Esc
{
goto ex;
}
if(a==99)    ///99--->c-->clear
{
canvas();
}
if(a==112)
{
gotoxy(5,5);
printf("%2d    %2d",x,y);
}
}
ex:
//closegraph();
}

void canvas()
{
int i,dh,dk;
clrscr();
for(dh=0;dh<=30;dh++)
{
for(dk=0;dk<=80;dk++)
{
store[dh][dk]=0;
}
}

//setbkcolor(3);
for(i=1;i<=79;i++)
{
y=1;
gotoxy(i,y);
printf("~");
}
for(i=1;i<=79;i++)
{
y=25;
gotoxy(i,y);
printf("~");
}
for(i=1;i<=25;i++)
{
x=1;
gotoxy(x,i);
printf("!");
}
for(i=1;i<=25;i++)
{
x=79;
gotoxy(x,i);
printf("!");
}
x=y=2;
gotoxy(x,y);

}