这两天去实训了,累死了,所以忘了更新博客。在加中这几天真的学到了很多很多,做出来了第一个游戏界面,虽然只有简简单单的两关,但是有了一个零的突破。
先写上代码:
#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[2][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,1,1,1,1,1,1},
{1,1,1,1,0,0,1,1},
{2,0,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==1)
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)
{
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==0)
{ 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==1)
{ 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==0)
{ 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==1)
{ 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==0)
{ 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==1)
{ 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==0)
{ 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==1)
{ if(IDYES==MessageBox(NULL,"你通全程,请退出","提示",MB_YESNO))
{
PostQuitMessage(0);
}
}
}
}
不写了,睡觉去啦。累死了》》》》》》》》》》》》》》