Mdc游戏
# include & lt堆栈& gt
使用命名空间std
const int rows = 8,cols = 8;
HINSTANCE hInst
HBITMAP球;
HDChdc,mdc,bufdc
HWNDhWnd
DWORDtPre,tNow
char * str
intnowPos,prePos
boolfind
堆栈& ltint & gt路径;
int mapIndex[rows*cols] = { 0,2,0,0,0,0,0,0,//材料1 &;#59049;
0,1,0,1,1,1,1,0, ?//素材2 &;#59049;
0,1,0,1,0,1,1,0, ?//素材3 &;#59049;
0,1,0,0,0,1,1,0, ?//素材4 &;#59049;
0,1,1,1,1,1,1,0, ?//素材5 &;#59049;
0,1,0,0,0,0,1,0, ?//素材6 &;#59049;
0,0,1,1,1,1,1,0, ?//素材7 &;#59049;
0,0,0,0,0,0,3,0 };//素材8 &;#59049;
int?记录[行*列];
ATOMMyRegisterClass(h instance h instance);
BOOLInitInstance(h instance,int);
LRESULT callbackndproc(HWND,UINT,WPARAM,LPARAM);
void mypaint(HDC HDC);
int API entry WinMain(h instance h instance,
?HINSTANCE hPrevInstance,
?LPSTR lpCmdLine,
?int?nCmdShow)
{
味精味精;
MyRegisterClass(h instance);
如果(!InitInstance (hInstance,nCmdShow))?
{
返回FALSE
}
while( msg.message!=WM_QUIT)
{
if(PeekMessage(& amp;msg,NULL,0,0,PM_REMOVE))
{
翻译消息(& amp味精);
dispatch message(amp;味精);
}
其他
{
tNow = GetTickCount();
if(tNow-tPre & gt;= 100)
my paint(hdc);
}
}
返回msg.wParam
}
//* * * *注册窗口* * * * * * * * * * * * * * * * * * * * * *
ATOM my register class(h instance h instance)
{
WNDCLASSEX wcex
wcex . CB size = sizeof(WNDCLASSEX);?
wcex . style = CS _ HREDRAW | CS _ VREDRAW;
wcex . lpfnwndproc =(WNDPROC)WNDPROC;
wcex . cbclsextra = 0;
wcex . cbwndextra = 0;
wcex . h instance = h instance;
wcex.hIcon = NULL
wcex.hCursor = NULL
wcex . hcursor = load cursor(NULL,IDC _ ARROW);
wcex . hbrbackground =(HBRUSH)(COLOR _ WINDOW+1);
wcex.lpszMenuName = NULL
wcex . lpsz class name = " canvas ";
wcex . hi consm = NULL;
return register classex(& amp;wcex);
}
//* * * *初始化* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
BOOL InitInstance(h instance h instance,int nCmdShow)
{
HBITMAP bmp
hInst = hInstance
HWnd = CreateWindow("canvas "," maze ",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
如果(!hWnd)
{
返回FALSE
}
MoveWindow(hWnd,10,10,430,450,true);
ShowWindow(hWnd,nCmdShow);
更新窗口(hWnd);
hdc = GetDC(hWnd);
MDC = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
bmp = CreateCompatibleBitmap(hdc,cols*50,rows * 50);
SelectObject(mdc,BMP);
HBITMAP平铺;
int rowNum,colNum
int i,x,y;
tile = (HBITMAP)LoadImage(NULL," tile.bmp ",IMAGE_BITMAP,50,50,LR _ LOADFROMFILE);
ball = (HBITMAP)LoadImage(NULL," ball.bmp ",IMAGE_BITMAP,50,50,LR _ LOADFROMFILE);
for(I = 0;我& lt行数*列数;i++)
{
record[I]= mapIndex[I];
rowNum = I/cols;
colNum = i % cols
x = colNum * 50
y = rowNum * 50
SelectObject(bufdc,tile);
如果(!mapIndex[i])
BitBlt(mdc,x,y,50,50,bufdc,0,0,src copy);
其他
{
if(mapIndex[i] == 2)
{
nowPos = I;
path.push(一);
记录[I]= 0;
}
BitBlt(mdc,x,y,50,50,bufdc,0,0,白度);
}
}
prePos = cols * rows+1;
my paint(hdc);
返回TRUE
}
//* * * *核心代码* * * * * * * * * * * * * * * * * * * * * * * * *
void MyPaint(HDC hdc)
{
int rowNum,colNum
int x,y;
int上、下、左、右;
rowNum = prePos/cols;
colNum = prePos % cols
x = colNum * 50
y = rowNum * 50
SelectObject(bufdc,ball);
BitBlt(mdc,x,y,50,50,bufdc,0,0,白度);
rowNum = nowPos/cols;
colNum = nowPos % cols
x = colNum * 50
y = rowNum * 50
SelectObject(bufdc,ball);
BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY);
如果(!查找)
{
Str = "迷宫入口";
up = nowPos-cols;
下来?= nowPos+cols;
向左?= now pos-1;
右= nowPos+1;
if(up & gt;= 0 & amp& amp记录下来)
{
path.push(向上);
记录[向上]= 0;
prePos = nowPos
nowPos = up
if(mapIndex[nowPos] == 3)
find = true
}
else if(down & lt;=列*行-1 & amp;& amp记录[下来])?
{
path.push(下);
记录[向下]= 0;
prePos = nowPos
nowPos = down
if(mapIndex[nowPos] == 3)
find = true
}
else if(left & gt;= rowNum * cols & amp& amp记录[左])?
{
path.push(左);
记录[左]= 0;
prePos = nowPos
nowPos = left
if(mapIndex[nowPos] == 3)
find = true
}
else if(right & lt;=(rowNum+1)* cols-1 & amp;& amp记录[右])?
{
path.push(右);
记录[右]= 0;
prePos = nowPos
nowPos = right
if(mapIndex[nowPos] == 3)
find = true
}
其他
{
if(path . size()& lt;= 1)//& amp;#59076;& amp#59343;& amp#58864;& amp#58892;
str = " xxxxx
其他
{
path . pop();
prePos = nowPos
now pos = path . top();
}
}
}
其他
{
Str = "找到出口";
}
TextOut(mdc,0,0,str,strlen(str));
BitBlt(hdc,10,10,cols*50,rows*50,mdc,0,0,src copy);
tPre = GetTickCount();
}
//* * * *消息功能* * * * * * * * * * * * * * * * * * * * * * * * * *
LRESULT回调WndProc(HWND hWnd,UINT消息,WPARAM wParam,LPARAM lParam)
{
开关(消息)
{
案例WM_KEYDOWN:
if(wParam==VK_ESCAPE)
PostQuitMessage(0);
打破;
案例WM_DESTROY:
DeleteDC(MDC);
DeleteDC(bufdc);
DeleteObject(球);
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
打破;
默认值:
返回DefWindowProc(hWnd,message,wParam,lParam);
}
返回0;
}
// ?它能跑吗?请采纳
有不明白的可以联系我。
这是标准的c++?这就是结果。
这是源代码?