#include "stdafxh"
#include <stack>
using namespace std;
const int rows = 8,cols = 8;
HINSTANCE hInst;
HBITMAP ball;
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow;
char str;
int nowPos,prePos;
bool find;
stack<int> path;
int mapIndex[rowscols] = { 0,2,0,0,0,0,0,0, //材1
0,1,0,1,1,1,1,0, //材2
0,1,0,1,0,1,1,0, //材3
0,1,0,0,0,1,1,0, //材4
0,1,1,1,1,1,1,0, //材5
0,1,0,0,0,0,1,0, //材6
0,0,1,1,1,1,1,0, //材7
0,0,0,0,0,0,3,0 }; //材8
int record[rowscols];
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
while( msgmessage!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
tNow = GetTickCount();
if(tNow-tPre >= 100)
MyPaint(hdc);
}
}
return msgwParam;
}
//注册窗口
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcexcbSize = sizeof(WNDCLASSEX);
wcexstyle = CS_HREDRAW | CS_VREDRAW;
wcexlpfnWndProc = (WNDPROC)WndProc;
wcexcbClsExtra = 0;
wcexcbWndExtra = 0;
wcexhInstance = hInstance;
wcexhIcon = NULL;
wcexhCursor = NULL;
wcexhCursor = LoadCursor(NULL, IDC_ARROW);
wcexhbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcexlpszMenuName = NULL;
wcexlpszClassName = "canvas";
wcexhIconSm = NULL;
return RegisterClassEx(&wcex);
}
//初始化
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HBITMAP bmp;
hInst = hInstance;
hWnd = CreateWindow("canvas", "迷宫" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
MoveWindow(hWnd,10,10,430,450,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
bmp = CreateCompatibleBitmap(hdc,cols50,rows50);
SelectObject(mdc,bmp);
HBITMAP tile;
int rowNum,colNum;
int i,x,y;
tile = (HBITMAP)LoadImage(NULL,"tilebmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
ball = (HBITMAP)LoadImage(NULL,"ballbmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
for (i=0;i<rowscols;i++)
{
record[i] = mapIndex[i];
rowNum = i / cols;
colNum = i % cols;
x = colNum 50;
y = rowNum 50;
SelectObject(bufdc,tile);
if(!mapIndex[i])
BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY);
else
{
if(mapIndex[i] == 2)
{
nowPos = i;
pathpush(i);
record[i] = 0;
}
BitBlt(mdc,x,y,50,50,bufdc,0,0,WHITENESS);
}
}
prePos = cols rows + 1;
MyPaint(hdc);
return TRUE;
}
//核心代码
void MyPaint(HDC hdc)
{
int rowNum,colNum;
int x,y;
int up,down,left,right;
rowNum = prePos / cols;
colNum = prePos % cols;
x = colNum 50;
y = rowNum 50;
SelectObject(bufdc,ball);
BitBlt(mdc,x,y,50,50,bufdc,0,0, WHITENESS);
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);
if(!find)
{
str = "迷宫入口";
up = nowPos - cols;
down = nowPos + cols;
left = nowPos - 1;
right = nowPos + 1;
if(up>=0 && record[up])
{
pathpush(up);
record[up] = 0;
prePos = nowPos;
nowPos = up;
if(mapIndex[nowPos] == 3)
find = true;
}
else if(down<=colsrows-1 && record[down])
{
pathpush(down);
record[down] = 0;
prePos = nowPos;
nowPos = down;
if(mapIndex[nowPos] == 3)
find = true;
}
else if(left>=rowNumcols && record[left])
{
pathpush(left);
record[left] = 0;
prePos = nowPos;
nowPos = left;
if(mapIndex[nowPos] == 3)
find = true;
}
else if(right<=(rowNum+1)cols-1 && record[right])
{
pathpush(right);
record[right] = 0;
prePos = nowPos;
nowPos = right;
if(mapIndex[nowPos] == 3)
find = true;
}
else
{
if(pathsize() <= 1) //
str = "xxxxx";
else
{
pathpop();
prePos = nowPos;
nowPos = pathtop();
}
}
}
else
{
str = "找到出口";
}
TextOut(mdc,0,0,str,strlen(str));
BitBlt(hdc,10,10,cols50,rows50,mdc,0,0,SRCCOPY);
tPre = GetTickCount();
}
//消息函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN:
if(wParam==VK_ESCAPE)
PostQuitMessage(0);
break;
case WM_DESTROY:
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(ball);
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// 可以运行 请采纳
有不懂的可以联系我
这个可是标准c++的 这是结果
这是源代码
以上就是关于跪求老鼠走迷宫游戏,必须用C++编写,用栈来实现,因为是数据结构课程设计所以只要现成代码,越快越好。全部的内容,包括:跪求老鼠走迷宫游戏,必须用C++编写,用栈来实现,因为是数据结构课程设计所以只要现成代码,越快越好。、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)