【设计题目】
走迷宫游戏
说明:程序开始运行时显示一个迷宫地图,迷宫中央有一只老鼠,迷宫的右下方有一个粮仓。游戏的任务是使用键盘上的方向键操纵老鼠在规定的时间内走到粮仓处。
【问题描述和基本要求】
老鼠形象可辨认,可用键盘操纵老鼠上下左右移动;
1、 迷宫的墙足够结实,老鼠不能穿墙而过;
2、 正确检测结果,若老鼠在规定时间内走到粮仓处,提示成功,否则提示失败;
3、 添加编辑迷宫功能,可修改当前迷宫,修改内容:墙变路、路变墙;
4、找出走出迷宫的所有路径,以及最短路径。
注意:必须用C++编写,网上大量的代码都是C,老师说不行,还有就是必须用“栈”实现,其他要求就是上面的说明,要是哪位大神或贵人之前做过或会做就在最好能在今天晚上回复我并把代码发至我的邮箱:[email protected],最迟明天中午,跪求大神相助,之后必有重谢!!!!!!!
#include "stdafx.h"
#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[rows*cols] = { 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[rows*cols];
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( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
tNow = GetTickCount();
if(tNow-tPre >= 100)
MyPaint(hdc);
}
}
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);
}
//****初始化*************************************
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,cols*50,rows*50);
SelectObject(mdc,bmp);
HBITMAP tile;
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;i<rows*cols;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;
path.push(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])
{
path.push(up);
record[up] = 0;
prePos = nowPos;
nowPos = up;
if(mapIndex[nowPos] == 3)
find = true;
}
else if(down<=cols*rows-1 && record[down])
{
path.push(down);
record[down] = 0;
prePos = nowPos;
nowPos = down;
if(mapIndex[nowPos] == 3)
find = true;
}
else if(left>=rowNum*cols && record[left])
{
path.push(left);
record[left] = 0;
prePos = nowPos;
nowPos = left;
if(mapIndex[nowPos] == 3)
find = true;
}
else if(right<=(rowNum+1)*cols-1 && record[right])
{
path.push(right);
record[right] = 0;
prePos = nowPos;
nowPos = right;
if(mapIndex[nowPos] == 3)
find = true;
}
else
{
if(path.size() <= 1) //
str = "xxxxx";
else
{
path.pop();
prePos = nowPos;
nowPos = path.top();
}
}
}
else
{
str = "找到出口";
}
TextOut(mdc,0,0,str,strlen(str));
BitBlt(hdc,10,10,cols*50,rows*50,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++的 这是结果
这是源代码
这个用的是MFC吗?我用的是VC++6.0,既然整成Windows程序也可以。但说好的老鼠和谷仓在哪里呢?貌似没有ctime.h函数,没有时间限定啊,这个能实现墙变路、路变墙的变换吗?找出最短路径数据结构里的内容,这个程序能实现吗?希望你能帮我找个简单一点的。
关键我们没学过Windows编程,我还是希望你帮我做个传统DOS界面黑框的,就是用VC++6.0新建Win32 Console Application,只要用*号表示就行!要符合上面要求,这个题在网上完全相同的有好多,但都不行,希望你能参照http://www.pudn.com/downloads22/sourcecode/windows/detail71822.html上面的代码。
追答图形界面多好啊 dos 界面每移动一步 你还的重新输出 还的写钩子函数响应键盘消息
同学你真是为难人啊 真没弄过这程序
乱七八糟的,你学过C++和数据结构吗,麻烦问一句你到百度上搜过了吗???
追答帮你找到 不行也别喷我 一片好意 谢谢