已结贴√
问题点数:10 回复次数:8
写下走迷宫游戏的注释
没学过在WINDOWS下编程,所以这个代码看着很吃力啊,有哪位高手能简单写下注释吗?还有谁要是有不是在WINDOWS下编的这个程序,就是单纯的C语言编写的,不要求界面多美观,符合要求就行,帮忙发下了,谢谢啦
题目:走迷宫游戏
程序开始运行时显示一个迷宫地图,迷宫中央有一只老鼠,迷宫的右下方有一个粮仓。游戏的任务是使用键盘上的方向键操纵老鼠在规定的时间内走到粮仓处。
要求:
1) 老鼠形象可辨认,可用键盘操纵老鼠上下左右移动;
2) 迷宫的墙足够结实,老鼠不能穿墙而过;
3) 正确检测结果,若老鼠在规定时间内走到粮仓处,提示成功,否则提示失败;
4) 添加编辑迷宫功能,可修改当前迷宫,修改内容:墙变路、路变墙;
5) 找出走出迷宫的所有路径,以及最短路径。
#include
#include
#include
#include
#include
using namespace std;
int ttime=8;
int incr[4][2]={ {0,1},{1,0},{-1,0},{0,-1} };
typedef vector path;
int DFS_maze(int maze[20][20],path& tpath,int m,int n)
{
if(m==18&&n==18)
{
POINT temp;
temp.x=m;
temp.y=n;
tpath.push_back(temp);
return 1;
}
if(maze[m][n]==0)
{
POINT temp;
temp.x=m;
temp.y=n;
tpath.push_back(temp);
maze[m][n]=-1;
for(int dir=0;dir<4;dir++)
if(DFS_maze(maze,tpath,m+incr[dir][0],n+incr[dir][1])==1) return 1;
tpath.pop_back();
//maze[m][n]=0;
}
return 0;
}
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
int Maze[20][20];
int x=10;
int y=10;
int h=22;
int w=22;
int k=0;
path Path;
bool first=false;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
time_t t;
srand((unsigned) time(&t));
int i;
for(i=0; i<20; i++)
for(int j=0; j<20; j++)
{
Maze[i][j] = rand()%2;
if(Maze[i][j]) Maze[i][j]=rand()%2;
}
for(i=0; i<20; i++)
{
Maze[ 0][ i] = 1;
Maze[19][ i] = 1;
Maze[ i][ 0] = 1;
Maze[ i][19] = 1;
}
Maze[18][18]=0;
Maze[10][10]=0;
MSG Message;
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{
HDC hDC;
PAINTSTRUCT ps;
HPEN hPen;
HBRUSH hBrush;
switch(iMessage)
{
int i,j;
case WM_LBUTTONDOWN:
x=10;
y=10;
k=0;
first=true;
SetTimer(hWnd,1,1000,NULL);
InvalidateRect(hWnd,NULL,FALSE);
break;
case WM_TIMER:
k++;
if(k==ttime&&first){
first=false;
MessageBox(hWnd,"time is over!","Failed",MB_OK);
}
break;
case WM_RBUTTONDOWN:
int temp[20][20];
for(i=0;i<20;i++)
for(j=0;j<20;j++)
temp[i][j]=Maze[i][j];
if(DFS_maze(temp,Path,10,10)==0) MessageBox(hWnd,"No Path!","sorry",MB_OK);
InvalidateRect(hWnd,NULL,FALSE);
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:
if(Maze[x][y-1]==0){
y--;
InvalidateRect(hWnd,NULL,FALSE);
}
break;
case VK_DOWN:
if(Maze[x][y+1]==0){
y++;
InvalidateRect(hWnd,NULL,FALSE);
}
break;
case VK_LEFT:
if(Maze[x-1][y]==0){
x--;
InvalidateRect(hWnd,NULL,FALSE);
}
break;
case VK_RIGHT:
if(Maze[x+1][y]==0){
x++;
InvalidateRect(hWnd,NULL,FALSE);
}
break;
case VK_SHIFT:
if(Maze[x][y]==0) Maze[x][y]=1;
else Maze[x][y]=0;
InvalidateRect(hWnd,NULL,FALSE);
break;
case VK_F4:
if(Maze[x+1][y]==0) Maze[x+1][y]=1;
else Maze[x+1][y]=0;
InvalidateRect(hWnd,NULL,FALSE);
break;
case VK_F3:
if(Maze[x-1][y]==0) Maze[x-1][y]=1;
else Maze[x-1][y]=0;
InvalidateRect(hWnd,NULL,FALSE);
break;
case VK_F2:
if(Maze[x][y+1]==0) Maze[x][y+1]=1;
else Maze[x][y+1]=0;
InvalidateRect(hWnd,NULL,FALSE);
break;
case VK_F1:
if(Maze[x][y-1]==0) Maze[x][y-1]=1;
else Maze[x][y-1]=0;
InvalidateRect(hWnd,NULL,FALSE);
break;
}
break;
case WM_PAINT:
hDC=BeginPaint(hWnd,&ps);
SetMapMode(hDC,MM_ANISOTROPIC);
for(i=0; i<20; i++)
for(j=0; j<20; j++)
{
if(Maze[i][j]==0)
{
hPen=(HPEN)GetStockObject(WHITE_PEN);
hBrush=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
if(i==10&&j==10){
hPen=(HPEN)GetStockObject(WHITE_PEN);
hBrush=(HBRUSH)GetStockObject(DKGRAY_BRUSH);
}
}
else
{
hPen=(HPEN)GetStockObject(WHITE_PEN);
hBrush=(HBRUSH)GetStockObject(BLACK_BRUSH);
}
SelectObject(hDC,hBrush);
SelectObject(hDC,hPen);
Rectangle(hDC,i*w,j*h,(i+1)*w,(j+1)*w);
}
hPen=CreatePen(PS_INSIDEFRAME,0,RGB(255,0,0));
hBrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
SelectObject(hDC,hBrush);
SelectObject(hDC,hPen);
if(Path.size()>0)
{
POINT *s=new POINT[Path.size()];
for(i=0;i
s[i].x=Path[i].x*w+w/2;
s[i].y=Path[i].y*h+h/2;
}
Polyline(hDC,s,Path.size());
EndPaint(hWnd,&ps);
Path.clear();
}
else{
Ellipse(hDC,x*w,y*h,(x+1)*w,(y+1)*w);
EndPaint(hWnd,&ps);
if(x==18&&y==18&&first){
first=false;
MessageBox(hWnd,"What a smart mouse!","WOW",MB_OK);
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
KillTimer(hWnd,1);
break;
default:
return DefWindowProc(hWnd,iMessage,wParam,lParam);
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("cjw",
"老鼠绕粮仓",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
return false;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,"END");
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName="cjw";
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&WndClass);
}
搜索更多相关主题的帖子:
注释 游戏 迷宫