栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

基于C语言实现的扫雷游戏代码

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于C语言实现的扫雷游戏代码

本文详细讲述了基于C语言实现的扫雷游戏代码,代码中备有比较详细的注释,便于读者阅读和理解。希望对学习游戏开发的朋友能有一点借鉴价值。

完整的实例代码如下:


#include 
#include 
#include 
#include 
#include 
#include 
#include 

union REGS regs;
int size=15;
int pix,piy=50;
char b[2]="1";
int pan[30][16];
int pan1[30][16];
int tt;
int Eflags;

int Msinit();
void Draw(int x,int y,int sizex,int sizey);
void Facedraw(int x,int y,int sizel,int k);
void Dead(int sizel,int x,int y);
void Setmouse(int xmax,int ymax,int x,int y);
int Msread(int *xp,int *yp,int *bup,struct time t1,int k);
void Draw1(int x,int y);
int Open(int x,int y);
float Random();
void Have(int sum,int x,int y,int xx,int yy);
void Help();
void Coread();
void Ddraw2(int x,int y);



main()
{
 int mode=VGAHI,devices=VGA;
 char ams;  
 int xms,yms,bms;      
 int i,j,k,k1=0;      
 int x=9,y=9,flags=0;    
 int sum=10; 
 int x1=0,y1=0;
 int x11=0,y11=0;      
 int sizel=10;
 int cflags=1;   
 struct time t1={0,0,0,0}; 
 int co[3];   
 void far *Map;   
 char name[3][20];   
 FILE * p;   

 Msinit(); 
 
 initgraph(&devices,&mode,"C:\tc");   


if((Map=farmalloc(imagesize(0,0,20,20)))==NULL)
 {
 printf("Memory ererr!n");
 printf("Press any key to out!n");
 exit(1);
 }

 
 while((p = fopen("score.dat", "r")) == NULL) 
  {
 if((p = fopen("score.dat", "w")) == NULL)
 {
  printf("The file cannot open!n");
  printf("Presss any key to exit!n");
  getch();
    exit(1);
 }
 
 fprintf(p,"%d %d %d,%sn%sn%sn",999,999,999,"xiajia","xiajia","xiajia");
 fclose(p); 
 }
 
 fscanf(p,"%d %d %d,",&co[0],&co[1],&co[2]);
 for(i=0;i<3;i++)
 fscanf(p,"%sn",name[i]);
 fclose(p);

 Setmouse(1,1,615,460); 
 pix=250-x*size/2; 

bss:  b[0]='1';    
    flags=0; 
    k=0;
 Eflags=0;
 tt=0;
 cleardevice();
 setbkcolor(LIGHTCYAN);
    for(i=0;i<30;i++)    
    for(j=0;j<16;j++)
    {
 pan[i][j]=0;
     pan1[i][j]=0;
 }

 for(;;) 
  {
 i=(int)(x*Random());
   j=(int)(y*Random());
   if(pan[i][j]==0)
     {
  pan[i][j]=9;
  k=k+1;
     }
   if(k==sum) break;
  } 

for(i=0;i=0 && j-1>=0 && pan[i-1][j-1]==9) pan[i][j]++;
  if(j-1>=0 && pan[i][j-1]==9)   pan[i][j]++;
  if(i+1=0 && pan[i+1][j-1]==9) pan[i][j]++;
  if(i-1>=0 && pan[i-1][j]==9)   pan[i][j]++;
  if(i+1=0 && j+1=pix-10 && xms<=pix+26 && yms>=2 && yms<=14 && bms==1) || ams=='g' || ams=='G')
 {
  Draw(pix-10,14,100,88); 
  cflags=0;
  setcolor(1);
  outtextxy(pix-4,19,"Restar(R)");
  setcolor(4);
  outtextxy(pix-4,19,"    R");
  setcolor(1);
  outtextxy(pix-4,30,"Easy(E)");
  setcolor(4);
  outtextxy(pix-4,30,"   E");
  setcolor(1);
  outtextxy(pix-4,41,"Nomall(N)");
  setcolor(4);
  outtextxy(pix-4,41,"    N");
  setcolor(1);
  outtextxy(pix-4,52,"Hard(H)");
  setcolor(4);
  outtextxy(pix-4,52,"   H");
  setcolor(1);
  outtextxy(pix-4,63,"Corse(C)");
  setcolor(4);
  outtextxy(pix-4,63,"   C");
  setcolor(1);
  outtextxy(pix-4,74,"Help(L)");
  setcolor(4);
  outtextxy(pix-4,74,"   L");
  setcolor(1);
  outtextxy(pix-4,85,"Quit(Q)");
  setcolor(4);
  outtextxy(pix-4,85,"   Q");
  bms=0;
 }

 
 if(Eflags==2 && (bms!=0 || ams!=-1))
 {
   goto bss;
  }

 
 if(cflags==0)
 {
  if((xms>=pix-6 && xms<=pix+85 && yms>=17 && yms<=28 && bms==1)|| (ams=='r' || ams=='R'))
  {
  cflags=1;
  goto bss;
  }
  else
  if((xms>=pix-6 && xms<=pix+85 && yms>=28 && yms<=39 && bms==1) || ams=='e' || ams=='E')
  {
   cflags=1;
   x=9;
   y=9;
   sum=10;
   pix=250-x*size/2;
   goto bss;
  }
  else
   if((xms>=pix-6 && xms<=pix+85 && yms>=39 && yms<=50 && bms==1) || ams=='n' || ams=='N')
   {
   cflags=1;
   x=16;
   y=16;
   sum=40;
   pix=250-x*size/2;
   goto bss;
   }
   else
    if((xms>=pix-6 && xms<=pix+85 && yms>=50 && yms<=61 && bms==1) || ams=='h' || ams=='H')
   {
    cflags=1;
    x=30;
    y=16;
    sum=99;
    pix=250-x*size/2+25;
    goto bss;
   }
   else
    if((xms>=pix-6 && xms<=pix+85 && yms>=61 && yms<=72 && bms==1) || ams=='c' || ams=='C')
    {
    cflags=1;
    Coread();
    }
    else
     if((xms>=pix-6 && xms<=pix+85 && yms>=72 && yms<=83 && bms==1) || ams=='l' || ams=='L')
    {
    cflags=1;
    Help();
    }
    else
     if((xms>=pix-6 && xms<=pix+85 && yms>=83 && yms<=94 && bms==1) || ams=='q' || ams=='Q' || ams==27)
    {
     cflags=1;
     fclose(p);
     closegraph();
     farfree(Map);
     exit(1);
    }
    else
     if(bms!=0)
     {
     cflags=1;
     goto bss;
     }
 }
 
  if(cflags!=0 && xms >=250-sizel-2 && yms >= 30-sizel-2 && xms<=250+sizel+2 && yms<=30+sizel+2 && bms==1 )
 {
  goto bss;
 }
 
  if(Eflags==0 && cflags!=0 && xms-pix>=0 && yms-piy >=0 && xms=0 && y1-1>=0 && pan1[x1-1][y1-1]==2) i++;
  if(y1-1>=0 && pan1[x1][y1-1]==2)i++;
  if(x1+1=0 && pan1[x1+1][y1-1]==2) i++;
  if(x1-1>=0 && pan1[x1-1][y1]==2) i++;
  if(x1+1=0 && y1+1=0 && y1-1>=0 && pan1[x1-1][y1-1]!=2) 
   {
   if(pan[x1-1][y1-1]!=9) 
    Draw1(x1-1,y1-1);
   else 
    {
    Dead(sizel,x,y); 
     k=0;
    }
   }
   if(y1-1>=0 && pan1[x1][y1-1]!=2)
   {
   if(pan[x1][y1-1]!=9)
    Draw1(x1,y1-1);
   else 
    {
    Dead(sizel,x,y);
    k=0;
    }
   }
   if(x1+1=0 && pan1[x1+1][y1-1]!=2) 
   {
   if(pan[x1+1][y1-1]!=9) 
    Draw1(x1+1,y1-1);
   else 
    {
    Dead(sizel,x,y);
    k=0;
    }
   }
   if(x1-1>=0 && pan1[x1-1][y1]!=2) 
   {
   if(pan[x1-1][y1]!=9) 
    Draw1(x1-1,y1);
   else 
    {
     Dead(sizel,x,y);
     k=0;
    }
   }
   if(x1+1=0 && y1+1=0 && j-1>=0 && pan1[i-1][j-1]==0) Draw1(i-1,j-1); 
  if(j-1>=0 && pan1[i][j-1]==0) Draw1(i,j-1);    
  if(i+1=0 && pan1[i+1][j-1]==0) Draw1(i+1,j-1); 
  if(i-1>=0 && pan1[i-1][j]==0) Draw1(i-1,j);     
  if(i+1=0 && j+1

至此程序完成,直接编译运行即可!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/65613.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号