Source code for games using C++ Language
#include
#include
#include
#include
#include
#include"mouse.h"
#define MINE 9
#define COMPLETE 2
#define OVER 1
#define RUNNING 0
#define OPEN 1
#define CLOSED 0
#define UNMARKED 0
#define MARKED 1
#define RESET 10
#define SETUP 11
#define EXIT 12
void Mine_Setter();
void Mine_Field_Setter();
void Draw_Mine_Field();
int Play();
int Square_Detector(int,int);
int Square_opener();
void Screen();
void Marker(int,int);
void Setup();
int Mine_Field[600];
int Mine_Field_Status[600];
int Mine_Marker[600];
int Mines[600];
int x,y;
int Skin=5;
int Total_Cells=600;
int G_Col=30,G_Row=20;
int Top_Left=0,Top_Right=29,Bottom_Left=570,Bottom_Right=599;
int Total_Mines=80;
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int choice;
x=getmaxx()/2;
y=getmaxy()/2+20;
if(initmouse()==0)
{
closegraph();
restorecrtmode();
cout<<"\nMouse driver not loaded";
exit(1);
}
restrictmouseptr(1,1,getmaxx()-1,getmaxy()-1);
Screen();
while(1)
{
choice=Play();
if(choice==RESET)
{}
if(choice==SETUP)
{
Setup();
}
if(choice==EXIT)
{
closegraph();
exit(0);
}
setcolor(13);
outtextxy(x+180,y-220,"GAME OVER");
outtextxy(x+180,y-220,"GAME COMPLETE");
}
}
int Play()
{
int button,mx,my,mousetrap=0;
int game_status=0;
int theme=0;
int i;
Total_Cells=G_Col*G_Row;
Top_Left=0;
Top_Right=G_Col-1;
Bottom_Left=Total_Cells-G_Col;
Bottom_Right=Total_Cells-1;
showmouseptr();
hidemouseptr();
Mine_Setter();
Mine_Field_Setter();
Screen();
Draw_Mine_Field();
getmousepos(&button,&mx,&my);
showmouseptr();
while(1)
{
getmousepos(&button,&mx,&my);
if((button&1)==1)
{
hidemouseptr();
while((button&1)==1)
{
getmousepos(&button,&mx,&my);
mousetrap=1;
}
getmousepos(&button,&mx,&my);
if(mx>x-300&&mx
{
return RESET;
}
if(mx>x-230&&mx
{
return SETUP;
}
if(mx>x-160&&mx
{
return EXIT;
}
if(theme==0)
{
game_status=Square_Detector(mx,my);
if(game_status==COMPLETE)
{
Draw_Mine_Field();
setcolor(RED);
outtextxy(x+180,y-220,"GAME COMPLETE");
showmouseptr();
theme=1;
}
if(game_status==OVER)
{
setcolor(RED);
outtextxy(x+180,y-220,"GAME OVER");
showmouseptr();
theme=1;
}
Draw_Mine_Field();
}
showmouseptr();
}
if(theme==0)
{
if((button&2)==2)
{
hidemouseptr();
while((button&2)==2)
{
getmousepos(&button,&mx,&my);
mousetrap=1;
}
getmousepos(&button,&mx,&my);
Marker(mx,my);
showmouseptr();
}
}//theme
}
}
int Square_Detector(int mx,int my)
{
int Square=1000;
int xx=50,yy=50;
if(mx>=x-300&&mx
if(my>=y-200&&my
xx=xx-(30-G_Col)/2;
yy=yy-(20-G_Row)/2;
if(xx!=50 && yy!=50)
{
Square=G_Col*yy+xx;
}
else return 1000;
int i,game_status=RUNNING;
if(Mine_Field_Status[Square]==CLOSED)
{
if(Mine_Field[Square]==MINE)
{
Mine_Field_Status[Square]=OPEN;
for(i=0;i
if(Mine_Field[i]==MINE)
Mine_Field_Status[i]=OPEN;
Draw_Mine_Field();
game_status=OVER;
}
else if(Mine_Field[Square]==0)
{
Mine_Field_Status[Square]=OPEN;
Square_opener();
}
else
{
Mine_Field_Status[Square]=OPEN;
}
}
if(game_status!=OVER)
{
game_status=COMPLETE;
for(i=0;i
if(Mine_Field[i]!=MINE && Mine_Field_Status[i]==CLOSED)
game_status=RUNNING;
}
return game_status;
}
int Square_opener()
{
int i,o;
for(o=0;o<32;o++)
{
for(i=0;i
{
if(Mine_Field[i]==0 && Mine_Field_Status[i]==OPEN)
{
if(i%G_Col!=0)
Mine_Field_Status[i-1]=OPEN;
if((i+1)%G_Col!=0)
Mine_Field_Status[i+1]=OPEN;
if(i>Top_Right)
Mine_Field_Status[i-G_Col]=OPEN;
if(i
Mine_Field_Status[i+G_Col]=OPEN;
if(i>Top_Right && i%G_Col!=0)
Mine_Field_Status[i-G_Col-1]=OPEN;
if(i>Top_Right && (i+1)%G_Col!=0)
Mine_Field_Status[i-G_Col+1]=OPEN;
if(i
if(i
}
}
}
}
void Mine_Setter()
{
int ran1;
int i,num=0;
int do_again=1;
for(i=0;i<150;i++)
Mines[i]=1000;
for(i=0;i
{
Mine_Field[i]=0;
Mine_Field_Status[i]=CLOSED;
Mine_Marker[i]=UNMARKED;
}
randomize();
while(do_again==1)
{
do_again=0;
ran1=random(Total_Cells);
for(i=0;i
if(Mines[i]==ran1)
do_again=1;
if(do_again==0)
{
Mines[num]=ran1;
num++;
if(num!=Total_Mines)
do_again=1;
}
}
for(i=0;i
{
Mine_Field[ Mines[i] ] = MINE ;
}
}
void Mine_Field_Setter()
{
int o;
for(o=0;o
{
if(o==0)
{
if(Mine_Field[o]!=MINE)
{
if(Mine_Field[1]==MINE) Mine_Field[o]++;
if(Mine_Field[Top_Right+1]==MINE) Mine_Field[o]++;
if(Mine_Field[Top_Right+2]==MINE) Mine_Field[o]++;
}
}
else if(o==Top_Right)
{
if(Mine_Field[o]!=MINE)
{
if(Mine_Field[Top_Right-1]==MINE) Mine_Field[o]++;
if(Mine_Field[2*Top_Right]==MINE) Mine_Field[o]++;
if(Mine_Field[2*Top_Right+1]==MINE) Mine_Field[o]++;
}
}
else if(o==Bottom_Left)
{
if(Mine_Field[o]!=MINE)
{
if(Mine_Field[Bottom_Left+1]==MINE) Mine_Field[o]++;
if(Mine_Field[Bottom_Left-G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[Bottom_Left-G_Col+1]==MINE) Mine_Field[o]++;
}
}
else if(o==Bottom_Right)
{
if(Mine_Field[o]!=MINE)
{
if(Mine_Field[Bottom_Right-1]==MINE) Mine_Field[o]++;
if(Mine_Field[Bottom_Right-G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[Bottom_Right-G_Col-1]==MINE) Mine_Field[o]++;
}
}
else if(o>Top_Left&&o
{
if(Mine_Field[o]!=MINE)
{
if(Mine_Field[o-1]==MINE) Mine_Field[o]++;
if(Mine_Field[o+1]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col-1]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col+1]==MINE) Mine_Field[o]++;
}
}
else if( (o+1)%G_Col==0 )
{
if(Mine_Field[o]!=MINE)
{
if(Mine_Field[o-1]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col-1]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col-1]==MINE) Mine_Field[o]++;
}
}
else if(o%G_Col==0)
{
if(Mine_Field[o]!=MINE)
{
if(Mine_Field[o+1]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col+1]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col+1]==MINE) Mine_Field[o]++;
}
}
else if(o>Bottom_Left&&o
{
if(Mine_Field[o]!=MINE)
{
if(Mine_Field[o-1]==MINE) Mine_Field[o]++;
if(Mine_Field[o+1]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col+1]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col-1]==MINE) Mine_Field[o]++;
}
}
else
{
if(Mine_Field[o]!=MINE)
{
if(Mine_Field[o-1]==MINE) Mine_Field[o]++;
if(Mine_Field[o+1]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col+1]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[o-G_Col-1]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col-1]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col]==MINE) Mine_Field[o]++;
if(Mine_Field[o+G_Col+1]==MINE) Mine_Field[o]++;
}
}
}
}
void Draw_Mine_Field()
{
int i;
settextstyle(DEFAULT_FONT,0,1);
for(i=0;i
{
if(Mine_Field_Status[i]==OPEN)
{
if(Mine_Field[i]!=MINE&&Mine_Field[i]!=0)
{
setfillstyle(SOLID_FILL,8);
if(Skin==1||Skin==5)
bar(x-10*G_Col+20*(i%G_Col)+1,y-10*G_Row+20*(i/G_Col)+1,x-10*G_Col+20+20*(i%G_Col)-1,y-10*G_Row+20+20*(i/G_Col)-1);
else
floodfill(x-10*G_Col+20*(i%G_Col)+10,y-10*G_Row+20*(i/G_Col)+10,8);
}
if(Mine_Field[i]==0)
{
setfillstyle(SOLID_FILL,8);
bar(x-10*G_Col+20*(i%G_Col),y-10*G_Row+20*(i/G_Col),x-10*G_Col+20+20*(i%G_Col),y-10*G_Row+20+20*(i/G_Col));
}
else if(Mine_Field[i]==1)
{
setcolor(4);
outtextxy(x-10*G_Col+7+20*(i%G_Col),y-10*G_Row+7+20*(i/G_Col),"1");
}
else if(Mine_Field[i]==2)
{
setcolor(1);
outtextxy(x-10*G_Col+7+20*(i%G_Col),y-10*G_Row+7+20*(i/G_Col),"2");
}
else if(Mine_Field[i]==3)
{
setcolor(2);
outtextxy(x-10*G_Col+7+20*(i%G_Col),y-10*G_Row+7+20*(i/G_Col),"3");
}
else if(Mine_Field[i]==4)
{
setcolor(5);
outtextxy(x-10*G_Col+7+20*(i%G_Col),y-10*G_Row+7+20*(i/G_Col),"4");
}
else if(Mine_Field[i]==5)
{
setcolor(6);
outtextxy(x-10*G_Col+7+20*(i%G_Col),y-10*G_Row+7+20*(i/G_Col),"5");
}
else if(Mine_Field[i]==6)
{
setcolor(11);
outtextxy(x-10*G_Col+7+20*(i%G_Col),y-10*G_Row+7+20*(i/G_Col),"6");
}
else if(Mine_Field[i]==7)
{
setcolor(9);
outtextxy(x-10*G_Col+7+20*(i%G_Col),y-10*G_Row+7+20*(i/G_Col),"7");
}
else if(Mine_Field[i]==8)
{
setcolor(10);
outtextxy(x-10*G_Col+7+20*(i%G_Col),y-10*G_Row+7+20*(i/G_Col),"8");
}
else if(Mine_Field[i]==MINE)
{
setcolor(12);
settextstyle(DEFAULT_FONT,0,2);
outtextxy(x-10*G_Col+3+20*(i%G_Col),y-10*G_Row+3+20*(i/G_Col),"*");
settextstyle(DEFAULT_FONT,0,1);
}
}
else if(Mine_Field_Status[i]==CLOSED)
{
if(Skin==1)
{
setcolor(8);
rectangle(x-10*G_Col+20*(i%G_Col),y-10*G_Row+20*(i/G_Col),x-10*G_Col+20+20*(i%G_Col),y-10*G_Row+20+20*(i/G_Col));
setfillstyle(SOLID_FILL,7);
bar(x-10*G_Col+20*(i%G_Col)+2,y-10*G_Row+20*(i/G_Col)+2,x-10*G_Col+20+20*(i%G_Col)-2,y-10*G_Row+20+20*(i/G_Col)-2);
rectangle(x-10*G_Col,y-10*G_Row,x+10*G_Col,y+10*G_Row);
}
else if(Skin==2)
{
setcolor(8);
circle(x-10*G_Col+20*(i%G_Col)+10,y-10*G_Row+20*(i/G_Col)+10,10);
}
if(Skin==3)
{
setcolor(8);
circle(x-10*G_Col+20*(i%G_Col)+10,y-10*G_Row+20*(i/G_Col)+10,11);
}
if(Skin==4)
{
setcolor(8);
rectangle(x-10*G_Col+20*(i%G_Col)+1,y-10*G_Row+20*(i/G_Col)+1,x-10*G_Col+20+20*(i%G_Col)-1,y-10*G_Row+20+20*(i/G_Col)-1);
}
if(Skin==5)
{
setcolor(8);
line(x-10*G_Col+20*(i%G_Col)+2,y-10*G_Row+20*(i/G_Col)+2,x-10*G_Col+20*(i%G_Col)-2,y-10*G_Row+20+20*(i/G_Col)-2);
line(x-10*G_Col+20*(i%G_Col)+1,y-10*G_Row+20*(i/G_Col)+1,x-10*G_Col+20*(i%G_Col)-1,y-10*G_Row+20+20*(i/G_Col)-1);
line(x-10*G_Col+20*(i%G_Col)+2,y-10*G_Row+20*(i/G_Col)+2,x-10*G_Col+20+20*(i%G_Col)-2,y-10*G_Row+20*(i/G_Col)-2);
line(x-10*G_Col+20*(i%G_Col)+1,y-10*G_Row+20*(i/G_Col)+1,x-10*G_Col+20+20*(i%G_Col)-1,y-10*G_Row+20*(i/G_Col)-1);
rectangle(x-10*G_Col,y-10*G_Row,x+10*G_Col,y+10*G_Row);
}
}
if(Mine_Marker[i]==MARKED&&Mine_Field_Status[i]==CLOSED)
{
setfillstyle(SOLID_FILL,3);
bar(x-10*G_Col+20*(i%G_Col)+5,y-10*G_Row+20*(i/G_Col)+5,x-5-10*G_Col+20+20*(i%G_Col),y-10*G_Row+20+20*(i/G_Col)-5);
}
}
}
void Screen()
{
struct palettetype pal;
getpalette(&pal);
setrgbpalette(pal.colors[13],37,38,30);
setfillstyle(SOLID_FILL,13);
floodfill(x,y,1);
setcolor(8);
ellipse(x-270,y-230,0,360,30,15);
ellipse(x-200,y-230,0,360,30,15);
ellipse(x-130,y-230,0,360,30,15);
setfillstyle(SOLID_FILL,7);
floodfill(x-270,y-230,8);
floodfill(x-200,y-230,8);
floodfill(x-130,y-230,8);
setcolor(12);
settextstyle(7,0,3);
outtextxy(x-80,y-245,"Mine Detecter");
setcolor(14);
settextstyle(DEFAULT_FONT,0,1);
outtextxy(x-288,y-233,"Reset");
outtextxy(x-218,y-233,"Setup");
outtextxy(x-145,y-233,"Exit");
setfillstyle(SOLID_FILL,7);
bar(x-10*G_Col,y-10*G_Row,x+10*G_Col,y+10*G_Row);
setcolor(15);
for(int i=0;i<=G_Row;i++)
line(x-10*G_Col,y-10*G_Row+20*i,x+10*G_Col,y-10*G_Row+20*i);
for(i=0;i
line(x-10*G_Col+20*i,y-10*G_Row,x-10*G_Col+20*i,y+10*G_Row);
}
void Marker(int mx,int my)
{
int Square=1000;
int xx=50,yy=50;
if(mx>=x-300&&mx
if(my>=y-200&&my
xx=xx-(30-G_Col)/2;
yy=yy-(20-G_Row)/2;
if(xx!=50 && yy!=50)
{
Square=G_Col*yy+xx;
}
if(Square!=1000)
{
if(Mine_Field_Status[Square]==CLOSED)
{
if(Mine_Marker[Square]==UNMARKED)
Mine_Marker[Square]=MARKED;
else
Mine_Marker[Square]=UNMARKED;
}
if(Mine_Marker[Square]==MARKED)
{
setfillstyle(SOLID_FILL,3);
bar(x-10*G_Col+20*(Square%G_Col)+5,y-10*G_Row+20*(Square/G_Col)+5,x-5-10*G_Col+20+20*(Square%G_Col),y-10*G_Row+20+20*(Square/G_Col)-5);
}
else if(Mine_Field_Status[Square]==CLOSED)
{
setfillstyle(SOLID_FILL,7);
bar(x-10*G_Col+20*(Square%G_Col)+5,y-10*G_Row+20*(Square/G_Col)+5,x-5-10*G_Col+20+20*(Square%G_Col),y-10*G_Row+20+20*(Square/G_Col)-5);
}
}
}
void Setup()
{
int refresh=0;
union REGS ii,oo;
cleardevice();
settextstyle(DEFAULT_FONT,0,1);
setcolor(4);
outtextxy(x-160,y-230,"Use UP and DOWN arrow keys to change the values");
outtextxy(x-60,y-210,"Press ENTER to select");
setcolor(8);
settextstyle(DEFAULT_FONT,0,2);
outtextxy(x-150,y-100,"Columns (max 30)");
gotoxy(65,11);cout<
while(1)
{
if(kbhit())
{
ii.h.ah=0;
int86(22,&ii,&oo);
if(oo.h.ah==28)
{
break;
}
if(oo.h.ah==72)
{
if(G_Col<30)
G_Col++;
refresh=1;
}
if(oo.h.ah==80)
{
if(G_Col>8)
G_Col--;
refresh=1;
}
}
if(refresh==1)
{
gotoxy(65,11);
cout<
refresh=0;
}
}
outtextxy(x-150,y-38, "Rows (max 20)");
gotoxy(65,15);cout<
while(1)
{
if(kbhit())
{
ii.h.ah=0;
int86(22,&ii,&oo);
if(oo.h.ah==28)
{
break;
}
if(oo.h.ah==72)
{
if(G_Row<20)
G_Row++;
refresh=1;
}
if(oo.h.ah==80)
{
if(G_Row>8)
G_Row--;
refresh=1;
}
}
if(refresh==1)
{
gotoxy(65,15);
cout<
refresh=0;
}
}
outtextxy(x-150,y+26, "Mines");
if(Total_Mines
;
else
Total_Mines=G_Col*G_Row-1;
gotoxy(65,19);cout<
while(1)
{
if(kbhit())
{
ii.h.ah=0;
int86(22,&ii,&oo);
if(oo.h.ah==28)
{
break;
}
if(oo.h.ah==72)
{
if(Total_Mines
Total_Mines++;
refresh=1;
}
if(oo.h.ah==80)
{
if(Total_Mines>1)
Total_Mines--;
refresh=1;
}
}
if(refresh==1)
{
gotoxy(65,19);
cout<
refresh=0;
}
}
outtextxy(x-150,y+90,"Skin (1 to 5)");
gotoxy(65,23);cout<
while(1)
{
if(kbhit())
{
ii.h.ah=0;
int86(22,&ii,&oo);
if(oo.h.ah==28)
{
break;
}
if(oo.h.ah==72)
{
if(Skin<5)
Skin++;
refresh=1;
}
if(oo.h.ah==80)
{
if(Skin>1)
Skin--;
refresh=1;
}
}
if(refresh==1)
{
gotoxy(65,23);
cout<
refresh=0;
}
}
cleardevice();
}
Subscribe to:
Post Comments (Atom)
Translate
Network Blogs
Labels
Blog Archive
-
▼
2010
(81)
-
▼
May
(10)
- Beep codes for IBM, AMI and Phoenix BIOS
- Beep Code Manual
- "Spyware", "Mal-ware", "hijackers", "Dialers"
- Next Wave of Growth
- 3D game demo embedded for your website
- source code to create pacman type game using C++
- creating typing tutor using C++
- source code for games using C++
- i-phone game development
- Social Media Marketing Tips
-
▼
May
(10)
Some Useful WebSites
Tweet Pictures of virtualinfocom
Followers
Blog Archive
-
▼
2010
(81)
-
▼
May
(10)
- Beep codes for IBM, AMI and Phoenix BIOS
- Beep Code Manual
- "Spyware", "Mal-ware", "hijackers", "Dialers"
- Next Wave of Growth
- 3D game demo embedded for your website
- source code to create pacman type game using C++
- creating typing tutor using C++
- source code for games using C++
- i-phone game development
- Social Media Marketing Tips
-
▼
May
(10)
About Me
Search This Blog
virtualinfocom's Fan Box
virtualinfocom on Facebook
Comments (0)
Post a Comment