win32 简易版扫雷

初学Windows 编程,觉得挺有意思,突发奇想,尝试一下编写扫雷小游戏。因为对MFC和windows编程没有太多了解,因此采用很low的方法:利用鼠标在客户区域点击的不同位置响应事件。我还是小白,不喜勿批,若有不足,请指正

#include <windows.h>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<commctrl.h>
#define SIZE 10     //地图大小 
#define bomb 20   //炸弹个数 
HWND hwnd;
HDC hdc;
HPEN pen=CreatePen(PS_SOLID,1,RGB(0,0,0));
HBRUSH brushbomb=CreateSolidBrush(RGB(255,255,0));  //炸弹颜色
HBRUSH brushmap=CreateSolidBrush(RGB(255,255,255)); //地图背景色
char map1[SIZE][SIZE];     //存放每个格子周围炸弹的个数 
char map2[SIZE][SIZE];     //用来判断是否遍历过一个点
bool map3[SIZE][SIZE];    //判断是否右键过 
bool map4[SIZE][SIZE];    //判断是否左键过 
POINT pt={0,0};           //存放鼠标位置 
UINT IDC_BUTTON1=1;       //开始按按钮 
UINT IDC_BUTTON2=2;       //退出按钮 
char left[]="20";         //炸弹剩余个数 

void initial()            //初始化 
{ int i,j; 
  hdc=GetDC(hwnd); 
  memset(map1,'r',sizeof(map1));
  strcpy(left,"20");
  memset(map3,true,sizeof(map3));
  memset(map4,true,sizeof(map3));
  for(i=0;i<SIZE;i++)
   for(j=0;j<SIZE;j++)
   {SelectObject(hdc,pen);
    Rectangle(hdc,i*20,j*20,i*20+20,j*20+20);
    }                                            //画地图 
    
    srand(time(NULL));
    for(i=0;i<bomb;i++)
     {int a=rand()%SIZE;
      int b=rand()%SIZE;
      if(map1[a][b]!='#') map1[a][b]='#';
      else i--;
	 }                                           //产生雷 
	 
	memcpy(map2,map1,sizeof(map1));              //把map1复制到map2       
	SelectObject(hdc,brushmap);
	
	Rectangle(hdc,240,195,240+100*(SIZE/5.0),200+100*(SIZE/5.0));
    for(i=0;i<SIZE;i++)
     for(j=0;j<SIZE;j++)
      {char ch[]={map1[i][j],'\0'};
        TextOut(hdc,250+i*20,200+j*20,ch,1); 
      }                                            //绘制答案
      
    for( i=0;i<SIZE;i++)
      for( j=0;j<SIZE;j++)
        {
	        if(map1[i][j]!='#')
             {
			    int sum=0;
	            for(int dx=-1;dx<=1;dx++)
                 for(int dy=-1;dy<=1;dy++)
                   if(dx!=0||dy!=0)
                     { 
                        int x=i+dx;
                        int y=j+dy;
                        if(x<0||x>=SIZE||y<0||y>=SIZE) ;
			            else if(map1[x][y]=='#') sum++;
			         }
		           map1[i][j]=sum+'0';
	          } 
    
	   }                                           //求每个格子周围雷的个数 
	Rectangle(hdc,SIZE*20+95,108,SIZE*20+120,128); 
	TextOut(hdc,SIZE*20+30,110,"剩余雷数",8);
    TextOut(hdc,SIZE*20+100,110,left,2);                   //显示剩余雷数 
}

void win()
{int i,j,k=1;
 for(i=0;i<SIZE;i++)
   for(j=0;j<SIZE;j++) if(map2[i][j]=='r') k=0;
   if(k) MessageBox(hwnd,"你赢了","游戏结束",MB_OK);
}                                                      //判断输赢 

void deepsearch(int row,int col)
{ if(map2[row][col]!='r'||!map3[row][col]) return; 
  map2[row][col]=map1[row][col];
  //map3[row][col]=false;
  map4[row][col]=false; 
  char ch[]={map1[row][col],'\0'};
  TextOut(hdc,row*20+4,col*20+2,ch,1);
  if(map1[row][col]!='0') return;
  else
    {for(int dx=-1;dx<=1;dx++)
      for(int dy=-1;dy<=1;dy++)
        if(dx!=0||dy!=0)
          {int x=row+dx;
           int y=col+dy;
           if(x>=0&&x<SIZE&&y>=0&&y<SIZE) deepsearch(x,y);
		  }
	}
  
}                                                        //深搜,遍历'0'周围的格子 


LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
	switch(Message) {
		
	
		case WM_DESTROY: {
			PostQuitMessage(0);
			break;
		}
	    case WM_COMMAND:{
			switch(wParam)
			 {case 1:
			 	{initial();
			 	 break;
				 }
			   case 2:{
			   	PostQuitMessage(0);
				break;
			   }
			 }
			break;
		} 
		case WM_LBUTTONDOWN:{
			GetCursorPos(&pt);
			ScreenToClient(hwnd,&pt);
			SelectObject(hdc,brushmap);
			SelectObject(hdc,pen);
			if(pt.y<SIZE*20&&pt.x<SIZE*20)
			if(map4[pt.x/20][pt.y/20]&&map3[pt.x/20][pt.y/20])
			{
			Rectangle(hdc,pt.x/20*20,pt.y/20*20,pt.x/20*20+20,pt.y/20*20+20);
			if(map1[pt.x/20][pt.y/20]=='#')
                {
                     MessageBox(hwnd,"你输了","游戏结束",MB_OK);
                     for(int i=0;i<SIZE;i++)
	                  for(int j=0;j<SIZE;j++)
	                   { if(map1[i][j]=='#') 
		                  {char ch[]={'#','\0'};TextOut(hdc,i*20+4,j*20+2,ch,1);}
		              }
                }
             else 
			deepsearch(pt.x/20,pt.y/20);
		   }
			win(); 
			break; 
		}
		case WM_RBUTTONDOWN:{
			GetCursorPos(&pt);
			ScreenToClient(hwnd,&pt);
			if(pt.y<SIZE*20&&pt.x<SIZE*20&&map4[pt.x/20][pt.y/20])
			 {
			 if(map3[pt.x/20][pt.y/20])
			{SelectObject(hdc,brushbomb);
			 Rectangle(hdc,pt.x/20*20+2,pt.y/20*20+2,pt.x/20*20+18,pt.y/20*20+18);
			 if(left[1]!=' ') 
			 {
			 if(left[1]=='0'){left[0]=left[0]-1;left[1]='9';}
			 else left[1]=left[1]-1;
		     }
		     else left[0]=left[0]-1;
			 SelectObject(hdc,brushmap);
			 Rectangle(hdc,SIZE*20+95,108,SIZE*20+120,128);
			 TextOut(hdc,SIZE*20+100,110,left,2);
			 map3[pt.x/20][pt.y/20]=false;
			}
			else
			{SelectObject(hdc,brushmap);
			SelectObject(hdc,pen);
			 Rectangle(hdc,pt.x/20*20+1,pt.y/20*20+1,pt.x/20*20+19,pt.y/20*20+19);
			 if(left[1]!=' ')
			  {if(left[1]=='9') {left[1]='0';left[0]=left[0]+1;}
			   else left[1]=left[1]+1;
			  }
			 else left[0]=left[0]+1;
			 Rectangle(hdc,SIZE*20+95,108,SIZE*20+120,128);
			 TextOut(hdc,SIZE*20+100,110,left,2);
			 map3[pt.x/20][pt.y/20]=true;
			 
			}
		}
			break;
		}  
			
		
		
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{
	WNDCLASSEX wc; 
	MSG msg; 

	
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; 
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); 
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); 

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		640, /* width */
		480, /* height */
		NULL,NULL,hInstance,NULL);

	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}
	HWND hbuttonwnd1=CreateWindow(WC_BUTTON,"开始",WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE,SIZE*20+30,50,100,20,hwnd,(HMENU)IDC_BUTTON1,NULL,NULL);
	HWND hbuttonwnd2=CreateWindow(WC_BUTTON,"退出",WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE,SIZE*20+30,75,100,20,hwnd,(HMENU)IDC_BUTTON2,NULL,NULL);

	
	while(GetMessage(&msg, NULL, 0, 0) > 0) { 
		TranslateMessage(&msg); 
		DispatchMessage(&msg); 
	}
	return msg.wParam;
}
发布了2 篇原创文章 · 获赞 2 · 访问量 37

猜你喜欢

转载自blog.csdn.net/weixin_45867464/article/details/104413499
今日推荐