Windows编程6:扫雷游戏正式开发

扫雷游戏构思

上一篇关于这个的被打了回来,所以就病在这一篇了,首先我们要有什么?要有地皮,我们就可以用静态框来制作,让静态框显示文字,然后我们翻起那块地,那就用按钮,你点击它就隐藏,我们用按钮覆盖静态框,这样按钮隐藏了静态框也就出来了,然后下面就判断是否是雷,是雷提示,并且结束游戏,是空白,就用递归来把所有的、不是雷的、在它上下左右的全部翻出来,同时检查一下,如果这个场地上还有n个没有被翻起的地皮,n是雷的个数,则判断玩家赢了。

代码

#include <windows.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
HINSTANCE hIns;
int map[12][12],dir[3]={-1,0,1},used[12][12],flag=0,check[20][20];
HWND hButton[9][9];
void TurnOver(int x,int y)
{
 if(x<=0||x>=10||y<=0||y>=10) return;
 used[x][y]=1;
 check[x-1][y-1]=1;
 ShowWindow(hButton[x-1][y-1],SW_HIDE);
 if(map[x][y]!=0) return;
 for(int k=0;k<=2;k++)
  for(int l=0;l<=2;l++)
  {
   if(k==1&&l==1||used[x+dir[k]][y+dir[l]]) continue;
   if(map[x+dir[k]][y+dir[l]]!=-1)TurnOver(x+dir[k],y+dir[l]);
  }
 return;
}
int num=0,size=0,Money,Ball,Skill[1],sk=-1;
int Gift[6][2];
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
 static HWND hStatic[9][9];
 static HWND hFlagNumber;
 static HWND hBall;
 static HWND hMoney;
 static HWND hCtlButton;
 switch(Message) {
  case WM_CREATE: {
   freopen("F:\\C++\\DevC++\\项目\\Property.txt","r",stdin);
   scanf("%d%d",&Money,&Ball);
   fclose(stdin);
   char buf[10005];
   hCtlButton=CreateWindowEx(WS_EX_CLIENTEDGE,"Button","|>",WS_VISIBLE|WS_CHILD,270,0,90,150,hwnd,(HMENU)1082,hIns,NULL);
   hFlagNumber=CreateWindowEx(WS_EX_CLIENTEDGE,"Static","10",WS_VISIBLE|WS_CHILD|SS_CENTER,270,150,90,40,hwnd,NULL,hIns,NULL);
   for(int i=0;i<=8;i++)
    for(int j=0;j<=8;j++)
    {
     char buf[3];
     if(map[i+1][j+1]==-1)
     {
      strcpy(buf,"※");
      buf[2]='\0';
     }
     else
     {
      buf[0]=map[i+1][j+1]+'0';
      buf[1]='\0';
     }
     if(map[i+1][j+1]==0) buf[0]='\0';
     long long code=i*9+j;
     hStatic[i][j]=CreateWindowEx(WS_EX_CLIENTEDGE,"Static",buf,WS_VISIBLE|WS_CHILD|SS_CENTER,i*30,j*30,30,30,hwnd,NULL,hIns,NULL);
     hButton[i][j]=CreateWindowEx(WS_EX_CLIENTEDGE,"Button",NULL,WS_VISIBLE|WS_CHILD|BS_MULTILINE,i*30,j*30,30,30,hwnd,(HMENU)code,hIns,NULL);
    }
   break;
  }
  case WM_COMMAND: {
   int code=LOWORD(wParam);
   if(code==1082)
   {
    if(flag)
    {
     flag=0;
     SetWindowText(hCtlButton,"|>");
    }
    else
    {
     flag=1;
     SetWindowText(hCtlButton,"□");
    }
    break;
   }
   else
   {
    char buf[5];
    GetWindowText(hButton[code/9][code%9],buf,4);
    if(sk==0&&map[code/9+1][code%9+1]==-1)
    {
     SetWindowText(hButton[code/9][code%9],"|>");
     num--;
     sk=-1;
     break;
    }
    if(flag)
    {
     if(!strcmp("|>",buf)) SetWindowText(hButton[code/9][code%9],""),num++;
     else SetWindowText(hButton[code/9][code%9],"|>"),num--;
     memset(buf,0,sizeof(buf));
     sprintf(buf,"%d",num);
     SetWindowText(hFlagNumber,buf);
    }
    else
    {
     if(!strcmp("|>",buf)) break;
     if(map[code/9+1][code%9+1]==0)
     {
      TurnOver(code/9+1,code%9+1);
      int cnt=0,b=0;
      for(int i=0;i<=8;i++)
       for(int j=0;j<=8;j++)
        if(!check[i][j]) cnt++;
      if(cnt==10) if(IDYES==MessageBox(hwnd,"你想再来一遍吗?","再来",MB_YESNO)) SendMessage(hwnd,WM_CREATE,0,0);
     }
     else
     {
      ShowWindow(hButton[code/9][code%9],SW_HIDE);
      check[code/9][code%9]=1;
      if(map[code/9+1][code%9+1]==-1)
      {
       MessageBox(hwnd,"哎呀,碰到雷了,炸成--了","炸了",MB_OK);
       if(IDYES==MessageBox(hwnd,"你想再来一遍吗?","再来",MB_YESNO)) SendMessage(hwnd,WM_CREATE,0,0);
      }
      else
      {
       int cnt=0;
       for(int i=0;i<=8;i++)
        for(int j=0;j<=8;j++)
         if(!check[i][j]) cnt++;
       if(cnt==10) if(IDYES==MessageBox(hwnd,"你赢了!你想再来一遍吗?","再来",MB_YESNO)) SendMessage(hwnd,WM_CREATE,0,0);
      }
     }
     sk=-1;
    }
   }
   break;
  }
  case WM_DESTROY: {
   PostQuitMessage(0);
   break;
  }
  default:
   return DefWindowProc(hwnd, Message, wParam, lParam);
 }
 return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	hIns=hInstance;
	srand(time(0));
	POINT pos[10];
	while(num<=9)
	{
		int x=rand()%9+1,y=rand()%9+1;		int f=1;		for(int i=0;i<=size-1;i++)			if(map[x][y]==-1)			{				f=0;				break;			}		if(f)		{			pos[size++]=(POINT){x,y};			map[x][y]=-1;			num++;		}	}	for(int i=1;i<=9;i++)	{		for(int j=1;j<=9;j++)		{			if(map[i][j]==-1) continue;			for(int k=0;k<=2;k++)				for(int l=0;l<=2;l++)				{					if(k==1&&l==1) continue;					if(map[i+dir[k]][j+dir[l]]==-1) map[i][j]++;				}		}	}	num=10;		WNDCLASSEX wc;	HWND hwnd;	MSG Msg;	memset(&wc,0,sizeof(wc));	wc.cbSize		 = sizeof(WNDCLASSEX);	wc.lpfnWndProc	 = WndProc; /* insert window procedure function here */	wc.hInstance	 = hInstance;	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);	wc.lpszClassName = "WindowClass";	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* use "A" as icon name when you want to use the project icon */	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); /* as above */	if(!RegisterClassEx(&wc)) {		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);		return 0;	}	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","扫雷",WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,680,480,NULL,NULL,hInstance,NULL);	if(hwnd == NULL) {		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);		return 0;	}	while(GetMessage(&Msg, NULL, 0, 0) > 0) {		TranslateMessage(&Msg);		DispatchMessage(&Msg);	}	return Msg.wParam;}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值