简单写了个扫雷游戏,有问题的话请多指教
作者:互联网
//扫雷游戏
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFTPRESS 0xff01
#define LEFTCLICK 0xff10
#define LEFTDRAG 0xff19
#define MOUSEMOVE 0xff08
struct
{
int num;
int roundnum;
int flag;
}Mine[10][10];
int gameAGAIN=0;
int gamePLAY=0;
int mineNUM;
char randmineNUM[3];
int Keystate;
int MouseExist;
int MouseButton;
int MouseX;
int MouseY;
void Init(void);
void MouseOn(void);
void MouseOff(void);
void MouseSetXY(int,int);
int LeftPress(void);
int RightPress(void);
void MouseGetXY(void);
void Control(void);
void GameBegain(void);
void DrawSmile(void);
void DrawRedflag(int,int);
void DrawEmpty(int,int,int,int);
void GameOver(void);
void GameWin(void);
int MineStatistics(int,int);
int ShowWhite(int,int);
void GamePlay(void);
void Close(void);
void main(void)
{
Init();
Control();
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"D://tc20//BGI");
}
void Close(void)
{
closegraph();
}
void MouseOn(void)
{
_AX=0x01;
geninterrupt(0x33);
}
void MouseOff(void)
{
_AX=0x02;
geninterrupt(0x33);
}
void MouseSetXY(int x,int y)
{
_CX=x;
_DX=y;
_AX=0x04;
geninterrupt(0x33);
}
int LeftPress(void)
{
_AX=0x03;
geninterrupt(0x33);
return(_BX&1);
}
int RightPress(void)
{
_AX=0x03;
geninterrupt(0x33);
return(_BX&2);
}
void MouseGetXY(void)
{
_AX=0x03;
geninterrupt(0x33);
MouseX=_CX;
MouseY=_DX;
}
void Control(void)
{
int gameFLAG=1;
while(1)
{
if(gameFLAG)
{
GameBegain();
GamePlay();
if(gameAGAIN==1)
{
gameAGAIN=0;
continue;
}
}
MouseOn();
gameFLAG=0;
if(LeftPress())
{
MouseGetXY();
if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85)
{
gameFLAG=1;
continue;
}
}
if(kbhit())
break;
}
MouseOff();
}
void DrawSmile(void)
{
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(290,75,10,10);
setcolor(YELLOW);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(285,75,2,2);
fillellipse(295,75,2,2);
setcolor(BLACK);
bar(287,80,293,81);
}
void DrawRedflag(int i,int j)
{
setcolor(7);
setfillstyle(SOLID_FILL,RED);
bar(198+j*20,95+i*20,198+j*20+5,95+i*20+5);
setcolor(BLACK);
line(198+j*20,95+i*20,198+j*20,95+i*20+10);
}
void DrawEmpty(int i,int j,int mode,int color)
{
setcolor(color);
setfillstyle(SOLID_FILL,color);
if(mode==0)
bar(200+j*20-8,100+i*20-8,200+j*20+8,100+i*20+8);
else
if(mode==1)
bar(200+j*20-7,100+i*20-7,200+j*20+7,100+i*20+7);
}
void GameBegain(void)
{
int i,j;
cleardevice();
if(gamePLAY!=1)
{
MouseSetXY(290,70);
MouseX=290;
MouseY=70;
}
gamePLAY=1;
mineNUM=0;
setfillstyle(SOLID_FILL,7);
bar(190,60,390,290);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
DrawEmpty(i,j,0,8);
setcolor(7);
DrawSmile();
randomize();//__page_break__
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
Mine[i][j].num=random(8);
if(Mine[i][j].num==1)
mineNUM++;
else
Mine[i][j].num=2;
Mine[i][j].flag=0;
}
sprintf(randmineNUM,"%d",mineNUM);
setcolor(1);
settextstyle(0,0,2);
outtextxy(210,70,randmineNUM);
mineNUM=100-mineNUM;
MouseOn();
}
void GameOver(void)
{
int i,j;
setcolor(0);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(Mine[i][j].num==1)
{
DrawEmpty(i,j,0,RED);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(200+j*20,100+i*20,7,7);
}
}
void GameWin(void)
{
setcolor(11);
settextstyle(0,0,2);
outtextxy(230,30,"YOU WIN!");
}
int MineStatistics(int i,int j)
{
int nNUM=0;
if(i==0&&j==0)
{
if(Mine[0][1].num==1)
nNUM++;
if(Mine[1][0].num==1)
nNUM++;
if(Mine[1][1].num==1)
nNUM++;
}
else
if(i==0&&j==9)
{
if(Mine[0][8].num==1)
nNUM++;
if(Mine[1][9].num==1)
nNUM++;
if(Mine[1][8].num==1)
nNUM++;
}
else
if(i==9&&j==0)
{
if(Mine[8][0].num==1)
nNUM++;
if(Mine[9][1].num==1)
nNUM++;
if(Mine[8][1].num==1)
nNUM++;
}
else
if(i==9&&j==9)
{
if(Mine[9][8].num==1)
nNUM++;
if(Mine[8][9].num==1)
nNUM++;
if(Mine[8][8].num==1)
nNUM++;
}
else if(j==0)
{
if(Mine[i][j+1].num==1)
nNUM++;
if(Mine[i+1][j].num==1)
nNUM++;
if(Mine[i-1][j].num==1)
nNUM++;
if(Mine[i-1][j+1].num==1)
nNUM++;
if(Mine[i+1][j+1].num==1)
nNUM++;
}
else if(j==9)
{
if(Mine[i][j-1].num==1)
nNUM++;
if(Mine[i+1][j].num==1)
nNUM++;
if(Mine[i-1][j].num==1)
nNUM++;
if(Mine[i-1][j-1].num==1)
nNUM++;
if(Mine[i+1][j-1].num==1)
nNUM++;
}
else if(i==0)
{
if(Mine[i+1][j].num==1)
nNUM++;
if(Mine[i][j-1].num==1)
nNUM++;
if(Mine[i][j+1].num==1)
nNUM++;
if(Mine[i+1][j-1].num==1)
nNUM++;
if(Mine[i+1][j+1].num==1)
nNUM++;
}
else if(i==9)
{
if(Mine[i-1][j].num==1)
nNUM++;
if(Mine[i][j-1].num==1)
nNUM++;
if(Mine[i][j+1].num==1)
nNUM++;
if(Mine[i-1][j-1].num==1)
nNUM++;
if(Mine[i-1][j+1].num==1)
nNUM++;
}
else
{
if(Mine[i-1][j].num==1)
nNUM++;
if(Mine[i-1][j+1].num==1)
nNUM++;
if(Mine[i][j+1].num==1)
nNUM++;
if(Mine[i+1][j+1].num==1)
nNUM++;
if(Mine[i+1][j].num==1)
nNUM++;
if(Mine[i+1][j-1].num==1)
nNUM++;
if(Mine[i][j-1].num==1)
nNUM++;
if(Mine[i-1][j-1].num==1)
nNUM++;
}//__page_break__
return (nNUM);
}
int ShowWhite(int i,int j)
{
if(Mine[i][j].flag==1||Mine[i][j].num==0)
return;
mineNUM--;
if(Mine[i][j].roundnum==0&&Mine[i][j].num!=1)
{
DrawEmpty(i,j,1,7);
Mine[i][j].num=0;
}
else
if(Mine[i][j].roundnum!=0)
{
DrawEmpty(i,j,0,8);
sprintf(randmineNUM,"%d",Mine[i][j].roundnum);
setcolor(RED);
outtextxy(195+j*20,95+i*20,randmineNUM);
Mine[i][j].num=0;
return ;
}
if(i!=0&&Mine[i-1][j].num!=1)
ShowWhite(i-1,j);
if(i!=0&&j!=9&&Mine[i-1][j+1].num!=1)
ShowWhite(i-1,j+1);
if(j!=9&&Mine[i][j+1].num!=1)
ShowWhite(i,j+1);
if(j!=9&&i!=9&&Mine[i+1][j+1].num!=1)
ShowWhite(i+1,j+1);
if(i!=9&&Mine[i+1][j].num!=1)
ShowWhite(i+1,j);
if(i!=9&&j!=0&&Mine[i+1][j-1].num!=1)
ShowWhite(i+1,j-1);
if(j!=0&&Mine[i][j-1].num!=1)
ShowWhite(i,j-1);
if(i!=0&&j!=0&&Mine[i-1][j-1].num!=1)
ShowWhite(i-1,j-1);
}
void GamePlay(void)
{
int i,j,Num;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
Mine[i][j].roundnum=MineStatistics(i,j);
while(!kbhit())
{
if(LeftPress())
{
MouseGetXY();
if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85)
{
MouseOff();
gameAGAIN=1;
break;
}
if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)
{
j=(MouseX-190)/20;
i=(MouseY-90)/20;
if(Mine[i][j].flag==1)
continue;
if(Mine[i][j].num!=0)
{
if(Mine[i][j].num==1)
{
MouseOff();
GameOver();
break;
}
else
{
MouseOff();
Num=MineStatistics(i,j);
if(Num==0)
ShowWhite(i,j);
else
{
sprintf(randmineNUM,"%d",Num);
setcolor(RED);
outtextxy(195+j*20,95+i*20,randmineNUM);
mineNUM--;
}
MouseOn();
Mine[i][j].num=0;
if(mineNUM<1)
{
GameWin();
break;
}
}
}
}
}
if(RightPress())
{
MouseGetXY();
if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)
{
j=(MouseX-190)/20;
i=(MouseY-90)/20;
MouseOff();
if(Mine[i][j].flag==0&&Mine[i][j].num!=0)
{
DrawRedflag(i,j);
Mine[i][j].flag=1;
}
else
if(Mine[i][j].flag==1)
{
DrawEmpty(i,j,0,8);
Mine[i][j].flag=0;
}
}
MouseOn();
sleep(1);
}
}
}
标签:nNUM,游戏,int,void,Mine,指教,++,num,扫雷 来源: https://blog.csdn.net/gvjjg/article/details/121587845