走迷宫

  走迷宫,搞了六关。不错不错,不错。不过还有一些东西不太懂,继续努力!代码写上:

 

 

#include "windows.h"
 #include<iostream>
 using namespace std;
 

//全局变量声明
 
HINSTANCE  hInst;
 HBITMAP  hbmp,htl;
 PAINTSTRUCT ps;
 void poto(HWND);
 void up( HWND );
 void down(HWND);
 void left( HWND );
 void right(HWND);
 HWND hWnd,hwnd;
 
int a[6][6][8]=
 {{{1,1,1,-1,1,1,1,1},
 {1,1,1,0,0,0,0,1},
 {2,0,0,1,1,0,1,1},
 {0,1,0,1,1,0,1,1},
{0,1,0,0,0,0,0,1},
 {1,1,1,1,1,1,1,1},
 },
 {
 {1,0,0,1,1,1,1,1},
 {1,1,0,0,0,0,0,-1},
 {2,0,0,1,1,0,0,1},
 {0,1,0,1,1,0,1,1},
 {1,1,0,0,1,0,0,1},
 {1,1,1,1,1,1,1,1}
 },
{
 {1,1,1,1,1,1,1,1},
 {1,0,1,1,0,0,1,1},
 {2,0,0,1,1,0,0,-1},
 {1,1,0,1,1,0,1,1},
 {1,1,0,0,0,0,0,1},
 {1,1,1,1,1,1,1,1}
 },
 {
 {1,1,1,1,1,1,1,1},
 {1,1,0,0,0,0,1,1},
 {2,0,0,1,1,0,0,1},
 {1,1,0,1,1,0,1,1},
 {1,1,0,1,0,0,0,-1},
 {1,1,1,1,1,1,1,1}
 },
 {
 {1,-1,1,1,1,1,1,1},
 {1,1,0,0,0,0,1,1},
 {2,0,0,1,1,0,0,1},
 {1,1,0,1,1,0,1,1},
{1,1,1,1,1,0,0,1},
 {1,1,1,1,1,1,1,1}
 },
 {
 {1,1,1,1,1,1,1,1},
 {0,0,0,0,0,0,1,1},
 {2,1,0,1,1,0,0,1},
 {0,1,0,1,1,0,1,1},
 {0,1,0,0,0,0,0,-1},
 {1,1,1,1,1,1,1,1}
 }},k,hang=2,lie=0,sum=0,add=0;
 
 
//全局函数的声明
 ATOM MyRegisterClass(HINSTANCE  hInstance);
BOOL  InitInstance(HINSTANCE,  int);
 LRESULT   CALLBACK WndProc(HWND,  UINT,  WPARAM,  LPARAM);
 
 
 
//**** Winmain函数,程序入口点函数**************************************
 int  APIENTRY WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance, LPSTR  lpCmdLine,
 int  nCmdShow)
 {
 
MSG  msg;
MyRegisterClass(hInstance);
 if (!InitInstance  (hInstance,  nCmdShow))
 {
 return FALSE;
 }
 

//消息循环
 while (GetMessage(&msg,  NULL,  0,  0))
 {
 TranslateMessage(&msg);
DispatchMessage(&msg);
 }
 

return msg.wParam;
 }
 

//****﹚设计一个窗口类,类似填空题,使用窗口结构体*************************
 ATOM  MyRegisterClass(HINSTANCE  hInstance)
 {
 WNDCLASSEX  wcex;
 

wcex.cbSize  =  sizeof(WNDCLASSEX);
 wcex.style =  CS_HREDRAW  | CS_VREDRAW;
 wcex.lpfnWndProc  =  (WNDPROC)WndProc;
wcex.cbClsExtra  =  0;
 
wcex.cbWndExtra  =  0;
wcex.hInstance  =  hInstance;
wcex.hIcon =  NULL;
wcex.hCursor  =  NULL;
 //wcex.hCursor =  LoadCursor(NULL,  IDC_ARROW);
 wcex.hbrBackground  =  (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName  =  NULL;
 wcex.lpszClassName =  "canvas";
 wcex.hIconSm =  NULL;
 

return RegisterClassEx(&wcex);
 }
 

//****初始化函数*************************************
 // 1.建立与窗口DC兼容的内存DC
 // 2.从文件加载位图并存至内存DC中
 BOOL  InitInstance(HINSTANCE  hInstance,  int nCmdShow)
 {
 HWND  hWnd;

 
 

hWnd  =  CreateWindow("canvas",  "这是迷宫"  , WS_POPUP, CW_USEDEFAULT,  0,  CW_USEDEFAULT,  0,  NULL,  NULL,  hInstance,  NULL);
 

if (!hWnd)
 {
 return FALSE;
 }
 

MoveWindow(hWnd,0,0,800,600,true);
 ShowWindow(hWnd,  nCmdShow);
UpdateWindow(hWnd);
 
 
 
 
 

return TRUE;
 }
 

//****自定义绘图函数*********************************
 

// void MyPaint(HDC  hdc)
 // {
// if(a[i][j]==1)
 // BitBlt(hdc,100*j,100*i,100,100,mdc,0,0,SRCCOPY);//采用BitBlt函数贴图
 // }
 

//****消息处理函数**********************************
 LRESULT   CALLBACK WndProc(HWND  hWnd,  UINT  message,  WPARAM  wParam,  LPARAM  lParam)
 {
 
 
 
switch (message)
 {
 HDC hdc;
 case   WM_PAINT:
 hdc=BeginPaint(hWnd,&ps);
 RECT rt;
 GetClientRect(hWnd,&rt);
 poto(hWnd);
 EndPaint(hWnd,&ps);
 break;
 case WM_KEYDOWN:
 switch (wParam)
 {
 
case VK_ESCAPE:  //按下【Esc】键
     if(IDYES==MessageBox(hwnd,"是否结束"," 提示",MB_YESNO))
  {
  PostQuitMessage(0);
     }
case VK_DOWN:  //按下【】键
  down(hWnd);
 break;
 case VK_UP:
  up(hWnd);
    break;
 case VK_LEFT:
  left(hWnd);
       break;
 case VK_RIGHT:
   right(hWnd);
          break;
 }//按下键盘消息
 
break;
 

case WM_DESTROY: //窗口结束消息
 

PostQuitMessage(0);
break;
 default: //其他消息
 return DefWindowProc(hWnd,  message,  wParam,  lParam);
 }
 return 0;
 }
 

void poto(HWND hWnd)
 {   if(add<6)
   sum=add;
 
HDC  hdc;
HDC mdc;
 hdc  =  GetDC(hWnd);
 mdc  =  CreateCompatibleDC(hdc);
 
htl= (HBITMAP)LoadImage(NULL,"res/by.bmp",IMAGE_BITMAP,100,100,LR_LOADFROMFILE);
 hbmp= (HBITMAP)LoadImage(NULL,"res/q.bmp",IMAGE_BITMAP,100,100,LR_LOADFROMFILE);
 for (int i=0;i<6;i++)
 {
  for (int j=0;j<8;j++)
  {
   if (a[sum][i][j]==2)
   {
            SelectObject(mdc,htl);
      BitBlt(hdc,100*j,100*i,100,100,mdc,0,0,SRCCOPY);
   }
   if (a[sum][i][j]==1)
   {
    SelectObject(mdc,hbmp);
    BitBlt(hdc,100*j,100*i,100,100,mdc,0,0,SRCCOPY);
   }
   if (a[sum][i][j]==0||a[sum][i][j]==-1)
   {
    SelectObject(mdc,hbmp);
    BitBlt(hdc,100*j,100*i,100,100,mdc,0,0,WHITENESS);
   }
  }
 }
 
ReleaseDC(hWnd,hdc);
 

 
 
}
 

void down( HWND hWnd)
 {
  HDC   hdc;

hdc=GetDC(hwnd); TextOut(hdc,0,0,"斌哥太聪明了",strlen("斌哥太聪明了"));

  
   if(a[sum][hang][lie]==2&&a[sum][hang+1][lie]!=1)
    {   k=a[sum][hang+1][lie];
 a[sum][hang][lie]=0;
   hang=hang+1;
 a[sum][hang][lie]=2;
 poto(hWnd);
 if(k==-1&&sum<5)
 { if(IDYES==MessageBox(NULL,"你通过了,是否继续","提示",MB_YESNO))
 {
  
  add=add+1;
  hang=2;
  lie=0;
  k=0;
  poto(hWnd);
 }
 else if(IDYES==MessageBox(NULL,"你是否关闭程序","提示",MB_YESNO))
 {
  PostQuitMessage(0);
 }
 

}
 if(k==-1&&sum==5)
 { if(IDYES==MessageBox(NULL,"你通全程,请退出","提示",MB_YESNO))
 {
 
  PostQuitMessage(0);
 }
 }
    }
 }

void up( HWND hWnd)
 {     
if(a[sum][hang][lie]==2&&a[sum][hang-1][lie]!=1)
    {   k=a[sum][hang-1][lie];
  
 a[sum][hang][lie]=0;
    hang=hang-1;
 a[sum][hang][lie]=2;
 poto(hWnd);
  if(k==-1&&sum<5)

 { if(IDYES==MessageBox(NULL,"你通过了,是否继续","提示",MB_YESNO))
 {
 
  add=add+1;
  hang=2;
  lie=0;
  k=0;
  poto(hWnd);
 }
 else if(IDYES==MessageBox(NULL,"你是否关闭程序","提示",MB_YESNO))
 {
  PostQuitMessage(0);
 }
 

}
 if(k==-1&&sum==5)
 { if(IDYES==MessageBox(NULL,"你通全程,请退出","提示",MB_YESNO))
 {
 
  PostQuitMessage(0);
 }
 }
 }
 }
 
 
 
void right( HWND )
 {
 

    if(a[sum][hang][lie]==2&&a[sum][hang][lie+1]!=1)
   {  k=a[sum][hang][lie+1];
       
 a[sum][hang][lie]=0;
 lie=lie+1;
 a[sum][hang][lie]=2;
 poto(hWnd);
 if(k==-1&&sum<5)
 { if(IDYES==MessageBox(NULL,"你通过了,是否继续","提示",MB_YESNO))
 {
 
add=add+1;
 hang=2;
 lie=0;
  k=0;
 poto(hWnd);
 }
 else if(IDYES==MessageBox(NULL,"你是否关闭程序","提示",MB_YESNO))
 {
  PostQuitMessage(0);
 }
 

}
 if(k==-1&&sum==5)
 { if(IDYES==MessageBox(NULL,"你通全程,请退出","提示",MB_YESNO))
 {
 
 PostQuitMessage(0);
 }
 }
 }
   
   }
 

void left( HWND )
 {
 if(a[sum][hang][lie]==2&&a[sum][hang][lie-1]!=1)
 {       k=a[sum][hang][lie-1];
   
 
  a[sum][hang][lie]=0;
  lie=lie-1;
  a[sum][hang][lie]=2;
  poto(hWnd);
  if(k==-1&&sum<5)
  { if(IDYES==MessageBox(NULL,"你通过了,是否继续","提示",MB_YESNO))
  {
  
   add=add+1;
   hang=2;
   lie=0;
   k=0;
   poto(hWnd);
  }
  else if(IDYES==MessageBox(NULL,"你是否关闭程序","提示",MB_YESNO))
  {
   PostQuitMessage(0);
  }
 
 
  }
  if(k==-1&&sum==5)
  { if(IDYES==MessageBox(NULL,"你通全程,请退出","提示",MB_YESNO))
  {
  
   PostQuitMessage(0);
  }
 }
    }
 }
 
 

 

不过代码还能简化,过两天写个加单的。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值