//*******************************************************
//
// FileName : Push The Box
// Author : Star Fish
// Version : 1.0
// Date : 2005:04:04
// Coments : 采用数组 10 * 10 来建立围墙、目标、箱子和人
// 其中 墙 1
// 目标 2
// 箱子 3
// 空余 4
// 人 5
// 坐标为 视窗原点左移一定位置为原点
// 左边留出一部分输出 关数 和步数
// 本程序没有考虑 资源和速度
//
// 其实该程序应该用,即一个大的数组来存储所有地图
// 用一个小的数组来表示正在用的一关地图
// 既节省空间,又提高速度
// 大地图的结构为 i,j,Sex,如果连着三个为6,6,6
// 则为一关, sex为 1,2,3,4,5
// 其它地方为 墙外
// 因为采用现在模式可以直接看到地图效果
//
// 实际的时候应该将现在的数组转换为上面大的数组
//
// 问题有当搬动箱子的时候可以缩小刷新的区域
//
//*******************************************************
#include <windows.h>
#include "Resource.h"
#define ID_TIMER 1
LRESULT CALLBACK WinProc ( HWND, UINT, WPARAM, LPARAM);
struct Points
{
POINT Position;
struct Points *Next;
};
struct PointSex
{
POINT Position;
// 其中 墙 1
// 目标 2
// 箱子 3
// 空余 4
// 人 5
int i;
struct PointSex *Next;
};
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
static TCHAR szAppName [] = "PushBox";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = ( HBRUSH) GetStockObject ( WHITE_BRUSH);
wndclass.hCursor = LoadCursor ( hInstance, MAKEINTRESOURCE ( IDC_CURSOR1) );
wndclass.hIcon = LoadIcon ( hInstance, MAKEINTRESOURCE ( IDI_ICON1) );
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WinProc;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
if ( !RegisterClass ( &wndclass) )
{
MessageBox( NULL, "This program requires windows NT!",
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow ( szAppName, "推箱子",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow ( hwnd, nShowCmd);
UpdateWindow ( hwnd);
while ( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage ( &msg);
DispatchMessage ( &msg);
}
return msg.wParam;
}
struct PointSex *InitNewLevel ( int iLevel)
{
struct PointSex *pPS0, *pPs2;
int i, j;
static int iBoxMap [ 2 ][ 10 ][ 20 ] = {
// 第一关地图
// 0 为 墙外 ;1 为 墙 ;2 为 目标 ;3为箱子
// 4 为 空区域 ,5 为 人
0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0
0, 0, 1, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //1
0, 0, 1, 1, 4, 4, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //2
0, 0, 0, 1, 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //3
0, 0, 0, 0, 1, 4, 4, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //4
0, 0, 1, 1, 1, 3, 4, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //5
0, 0, 1, 5, 3, 4, 4, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //6
0, 0, 1, 4, 4, 4, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //7
0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //8
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //9
0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0
0, 0, 1, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //1
0, 0, 1, 1, 4, 4, 4, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, //2
0, 0, 0, 1, 1, 3, 3, 1, 0, 0, 0, 1, 1, 2, 1, 0, 0, 0, 0, 0, //3
0, 0, 0, 0, 1, 4, 4, 1, 1, 1, 1, 1, 4, 4, 1, 0, 0, 0, 0, 0, //4
0, 0, 1, 1, 1, 3, 4, 4, 1, 1, 4, 4, 4, 3, 1, 0, 0, 0, 0, 0, //5
0, 0, 1, 5, 3, 4, 3, 4, 2, 4, 4, 4, 4, 4, 1, 0, 0, 0, 0, 0, //6
0, 0, 1, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, //7
0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //8
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //9
// 第二关
// 要把 这个三维数组的第一个改为 2
};
pPS0 = pPs2 = NULL;
iLevel = iLevel % 2;
for ( j = 0; j < 10; j++)
for ( i = 0; i < 20; i++)
{
if ( iBoxMap [ iLevel] [ j ] [ i ])
{
pPs2 = ( struct PointSex *) malloc ( sizeof (struct PointSex) );
pPs2->Next = pPS0;
pPs2->Position.x = i;
pPs2->Position.y = j;
pPs2->i = iBoxMap [ iLevel] [ j ] [ i ];
pPS0 = pPs2;
if ( pPs2->i < 0 || pPs2->i >5 )
{
MessageBox ( NULL,"Init error", "Error", MB_ICONERROR);
while ( pPs2->Next)
{
free ( pPS0);
pPS0 = pPs2;
pPs2 = pPs2->Next;
}
free ( pPS0);
exit(0);
}
}
}
return pPS0;
}
struct Points *InitBox( struct PointSex *pPos)
{
struct Points *pPs2, *pPS2;
struct PointSex *pPS0;
pPS0 = pPos;
pPs2 = pPS2 = NULL;
while ( pPS0->Next)
{
if ( pPS0->i == 3)
{
pPs2 = ( struct Points *) malloc ( sizeof (struct Points) );
pPs2->Position = pPS0->Position;
pPs2 ->Next = pPS2;
pPS2 = pPs2;
}
pPS0 = pPS0->Next;
}
if ( pPS0->i == 3)
{
pPs2 = ( struct Points *) malloc ( sizeof (struct Points) );
pPs2->Position = pPS0->Position;
pPs2 ->Next = pPS2;
pPS2 = pPs2;
}
return pPs2;
}
struct Points *InitDest( struct PointSex *pPos)
{
struct Points *pPs2, *pPS2;
struct PointSex *pPS0;
pPS0 = pPos;
pPs2 = pPS2 = NULL;
while ( pPS0->Next)
{
if ( pPS0->i == 2)
{
pPs2 = ( struct Points *) malloc ( sizeof (struct Points) );
pPs2->Position = pPS0->Position;
pPs2 ->Next = pPS2;
pPS2 = pPs2;
}
pPS0 = pPS0->Next;
}
if ( pPS0->i == 2)
{
pPs2 = ( struct Points *) malloc ( sizeof (struct Points) );
pPs2->Position = pPS0->Position;
pPs2 ->Next = pPS2;
pPS2 = pPs2;
}
return pPs2;
}
POINT InitMan (struct PointSex *pPos)
{
struct PointSex *pPS0;
pPS0 = pPos;
while ( pPS0->Next)
{
if ( pPS0->i == 5)
{
pPS0->i = 4;
break;
}
pPS0 = pPS0->Next;
}
return pPS0->Position;
}
void DrawMap( HDC hdc, struct PointSex *pPos)
{
struct PointSex *p0;
POINT apt [ 4 ];
HBRUSH hBrush;
p0 = pPos;
while ( p0)
{
if ( p0->i == 1) // WALL
{
hBrush = CreateHatchBrush ( HS_DIAGCROSS, RGB ( 100, 100, 100) );
SelectObject ( hdc, hBrush);
apt [ 0 ].x = p0->Position.x * 100;
apt [ 0 ].y = p0->Position.y * 100;
apt [ 1 ].x = ( p0->Position.x + 1) * 100;
apt [ 1 ].y = p0->Position.y * 100;
apt [ 2 ].x = ( p0->Position.x + 1) * 100;
apt [ 2 ].y = ( p0->Position.y + 1) * 100;
apt [ 3 ].x = p0->Position.x * 100;
apt [ 3 ].y = ( p0->Position.y + 1) * 100;
Polygon ( hdc, apt, 4);
DeleteObject ( hBrush);
}
else if ( p0->i == 4) // space
{
SelectObject ( hdc, GetStockObject ( WHITE_BRUSH) );
apt [ 0 ].x = p0->Position.x * 100;
apt [ 0 ].y = p0->Position.y * 100;
apt [ 1 ].x = ( p0->Position.x + 1) * 100;
apt [ 1 ].y = p0->Position.y * 100;
apt [ 2 ].x = ( p0->Position.x + 1) * 100;
apt [ 2 ].y = ( p0->Position.y + 1) * 100;
apt [ 3 ].x = p0->Position.x * 100;
apt [ 3 ].y = ( p0->Position.y + 1) * 100;
Polygon ( hdc, apt, 4);
}
p0 = p0->Next;
}
return;
}
void Drawdest( HDC hdc, int i ,int j)
{
POINT apt [ 4 ];
apt [ 0 ].x = i * 100;
apt [ 0 ].y = j * 100;
apt [ 1 ].x = ( i + 1) * 100;
apt [ 1 ].y = j * 100;
apt [ 2 ].x = ( i + 1) * 100;
apt [ 2 ].y = ( j + 1) * 100;
apt [ 3 ].x = i * 100;
apt [ 3 ].y = ( j + 1) * 100;
Polygon ( hdc, apt, 4);
SelectObject ( hdc, GetStockObject ( BLACK_BRUSH) );
apt [ 0 ].x = i * 100 + 25;
apt [ 0 ].y = j * 100 + 25;
apt [ 1 ].x = i * 100 + 75;
apt [ 1 ].y = j * 100 + 25;
apt [ 2 ].x = i * 100 + 75;
apt [ 2 ].y = j * 100 + 75;
apt [ 3 ].x = i * 100 + 25;
apt [ 3 ].y = j * 100 + 75;
Polygon ( hdc, apt, 4);
}
void DrawDest ( HDC hdc,struct Points *pDest)
{
struct Points *p0;
POINT apt [ 4 ];
p0 = pDest;
while ( p0)
{
apt [ 0 ].x = p0->Position.x * 100;
apt [ 0 ].y = p0->Position.y * 100;
apt [ 1 ].x = ( p0->Position.x + 1) * 100;
apt [ 1 ].y = p0->Position.y * 100;
apt [ 2 ].x = ( p0->Position.x + 1) * 100;
apt [ 2 ].y = ( p0->Position.y + 1) * 100;
apt [ 3 ].x = p0->Position.x * 100;
apt [ 3 ].y = ( p0->Position.y + 1) * 100;
SelectObject ( hdc, GetStockObject ( WHITE_BRUSH) );
Polygon ( hdc, apt, 4);
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
Polyline ( hdc, apt, 4);
MoveToEx( hdc, apt [ 0].x, apt [ 0 ].y, NULL);
LineTo ( hdc, apt [ 3 ].x, apt [ 3 ].y);
SelectObject ( hdc, GetStockObject ( BLACK_BRUSH) );
apt [ 0 ].x = p0->Position.x * 100 + 25;
apt [ 0 ].y = p0->Position.y * 100 + 25;
apt [ 1 ].x = p0->Position.x * 100 + 75;
apt [ 1 ].y = p0->Position.y * 100 + 25;
apt [ 2 ].x = p0->Position.x * 100 + 75;
apt [ 2 ].y = p0->Position.y * 100 + 75;
apt [ 3 ].x = p0->Position.x * 100 + 25;
apt [ 3 ].y = p0->Position.y * 100 + 75;
Polygon ( hdc, apt, 4);
p0 = p0->Next;
}
p0 = pDest;
return;
}
void DrawBox( HDC hdc, struct Points *pBox)
{
struct Points *p0;
POINT apt [ 4 ];
p0 = pBox;
while ( p0)
{
// 斜杠
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
MoveToEx ( hdc, p0->Position.x * 100, p0->Position.y * 100, NULL);
LineTo ( hdc, (p0->Position.x + 1) * 100, ( p0->Position.y + 1) * 100 );
MoveToEx ( hdc, (p0->Position.x + 1) * 100, p0->Position.y * 100, NULL);
LineTo ( hdc, p0->Position.x * 100, ( p0->Position.y + 1) * 100);
// 3 边
apt [ 0 ].x = p0->Position.x * 100;
apt [ 0 ].y = p0->Position.y * 100;
apt [ 1 ].x = ( p0->Position.x + 1) * 100;
apt [ 1 ].y = p0->Position.y * 100;
apt [ 2 ].x = ( p0->Position.x + 1) * 100;
apt [ 2 ].y = ( p0->Position.y + 1) * 100;
apt [ 3 ].x = p0->Position.x * 100;
apt [ 3 ].y = ( p0->Position.y + 1) * 100;
Polyline ( hdc, apt ,4);
MoveToEx ( hdc, p0->Position.x * 100, p0->Position.y * 100, NULL);
LineTo ( hdc, p0->Position.x * 100, ( p0->Position.y + 1) * 100);
p0 = p0->Next;
}
}
void Drawbox ( HDC hdc, int i, int j)
{
POINT apt [ 4 ];
// 斜边
MoveToEx ( hdc, i * 100, j * 100, NULL);
LineTo ( hdc, (i + 1) * 100, ( j + 1) * 100 );
MoveToEx ( hdc, (i + 1) * 100, j * 100, NULL);
LineTo ( hdc, i * 100, ( j + 1) * 100);
// 3 边
apt [ 0 ].x = i * 100;
apt [ 0 ].y = j * 100;
apt [ 1 ].x = ( i + 1) * 100;
apt [ 1 ].y = j * 100;
apt [ 2 ].x = ( i + 1) * 100;
apt [ 2 ].y = ( j + 1) * 100;
apt [ 3 ].x = i * 100;
apt [ 3 ].y = ( j + 1) * 100;
Polyline ( hdc, apt ,4);
MoveToEx ( hdc, i * 100, j * 100, NULL);
LineTo ( hdc, i * 100, ( j + 1) * 100);
}
void DrawMan( HDC hdc, POINT po)
{
int i;
POINT man [ 5 ] = { 80, 50, 20, 50, 40, 40, 20, 50, 40, 60};
// Polyline ( hdc, apt ,4);
for ( i = 0; i < 5; i++ )
{
man [ i ].x += po.x * 100;
man [ i ].y += po.y * 100;
}
Polyline ( hdc, man, 5);
}
void MoveCoordinate ( HDC hdc, int cxClient, int cyClient)
{
SetMapMode ( hdc, MM_ISOTROPIC);
SetWindowExtEx ( hdc, 1500, 1500, NULL);
SetViewportExtEx (hdc, cxClient , cyClient, NULL) ;
SetViewportOrgEx ( hdc, cxClient / 10, cyClient / 10 , NULL);
}
LRESULT CALLBACK WinProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClient, cyClient;
static int cxChar, cxCaps, cyChar;
static int iLevel; // 关
static int iNum; // 统计步数
static BOOL fPass; // 过关
static BOOL bMan;
static struct PointSex *pLevelMap; // 每一关的地图链表
struct PointSex *pPS0, *pPS1; // 每一关的地图链表
static struct Points *pDest, *pBox;
static struct Points *pPs2;
static POINT iMan; //人的位置
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
TCHAR szBuffer [10];
switch ( message)
{
case WM_CREATE:
SetTimer ( hwnd, ID_TIMER, 300, NULL);
hdc = GetDC ( hwnd);
GetTextMetrics ( hdc, &tm);
cxChar = tm.tmAveCharWidth;
cxCaps = ( tm.tmPitchAndFamily & 1 ? 2 : 3)
* cxChar / 2;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
iLevel = 0;
iNum = 0;
bMan = FALSE;
pLevelMap = NULL;
pDest = NULL;
pLevelMap = InitNewLevel ( iLevel);
pDest = InitDest ( pLevelMap);
iMan = InitMan ( pLevelMap);
pBox = InitBox ( pLevelMap);
ReleaseDC ( hwnd, hdc);
return 0;
case WM_SIZE:
cxClient = LOWORD ( lParam);
cyClient = HIWORD ( lParam);
return 0;
case WM_KEYDOWN:
switch ( wParam)
{
case VK_UP:
pPS0 = pLevelMap;
while ( pPS0) // while 1
{
if ( pPS0->Position .x == iMan.x &&
pPS0->Position .y == iMan.y - 1 &&
pPS0->i == 4)
// 为空
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.y--;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
// 如果原位置是目的 则要将该位置画白
pPs2 = pDest;
// 找到人原位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y + 1)
// iMan.y已减1
{
SelectObject ( hdc, GetStockObject ( WHITE_BRUSH) );
Drawdest ( hdc , iMan.x, iMan.y + 1);
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
break;
}// end if
else if ( pPS0->Position .x == iMan.x &&
pPS0->Position .y == iMan.y - 1 &&
pPS0->i == 2)
// 为目的
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.y--;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
break;
} // end else if
else if ( pPS0->Position .x == iMan.x &&
pPS0->Position .y == iMan.y - 1 &&
pPS0->i == 3)
// else if 2
// 上面为箱子
{
pPS1 = pLevelMap;
while ( pPS1) // while 3
{
if ( pPS1->Position .x == iMan.x &&
pPS1->Position .y == iMan.y - 2 &&
( pPS1->i == 4 || pPS1->i == 2)
)
// 找到箱子移动到的位置,即空或目的
// if 4
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
// 处理人
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.y--;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
// 还要处理箱子
// 分为箱子链和地图位置处理
// 先处理箱子链位置
pPs2 = pBox;
// 找到箱子原位置 也就是现在人的位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
Drawbox( hdc, pPs2->Position.x, pPs2->Position.y - 1);
pPs2->Position.y--;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
Drawbox( hdc, pPs2->Position.x, pPs2->Position.y );
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
// 重画箱子完成
pPS1->i = 3;
// 到达该位置
// 处理地图
pPS0->i = 4;
// 如果原位置是目的 则要改为目的
pPs2 = pDest;
// 找到箱子原位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
pPS0->i = 2;
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
pPS1 = pLevelMap;
while ( pPS1)
{
if ( pPS1->i ==2)
break;
else if ( pPS1->Next ==NULL &&
( pPS1->i > 2 || pPS1->i < 2))
fPass = TRUE;
pPS1 = pPS1->Next;
}
InvalidateRect ( hwnd, NULL, TRUE);
break;
}// end if 4 空或目标 箱子的新位置
pPS1 = pPS1->Next;
}// end while 3
break;
} // end else if 2 上面为箱子
pPS0 = pPS0->Next;
} // end while 1 所有的位置
break;
case VK_DOWN:
pPS0 = pLevelMap;
while ( pPS0) // while 1
{
if ( pPS0->Position .x == iMan.x &&
pPS0->Position .y == iMan.y + 1 &&
pPS0->i == 4)
// 为空
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.y++;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
pPs2 = pDest;
// 找到人原位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y - 1)
// iMan.y已减1
{
SelectObject ( hdc, GetStockObject ( WHITE_BRUSH) );
Drawdest ( hdc , iMan.x, iMan.y - 1);
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
break;
}// end if
else if ( pPS0->Position .x == iMan.x &&
pPS0->Position .y == iMan.y + 1 &&
pPS0->i == 2)
// 为目的
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.y++;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
break;
} // end else if
else if ( pPS0->Position .x == iMan.x &&
pPS0->Position .y == iMan.y + 1 &&
pPS0->i == 3)
// else if 2
// 为箱子 箱子上面为空或目的
{
pPS1 = pLevelMap;
while ( pPS1) // while 3
{
if ( pPS1->Position .x == iMan.x &&
pPS1->Position .y == iMan.y + 2 &&
( pPS1->i == 4 || pPS1->i == 2)
)
// 找到箱子移动到的位置,即空或目的
// if 4
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
// 处理人
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.y++;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
// 还要处理箱子
// 分为箱子链和地图位置处理
// 先处理箱子链位置
pPs2 = pBox;
// 找到箱子原位置 也就是现在人的位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
Drawbox( hdc, pPs2->Position.x, pPs2->Position.y - 1);
pPs2->Position.y++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
Drawbox( hdc, pPs2->Position.x, pPs2->Position.y );
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
// 重画箱子完成
pPS1->i = 3;
// 到达该位置
// 处理地图
pPS0->i = 4;
// 如果原位置是目的 则要改为目的
pPs2 = pDest;
// 找到箱子原位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
pPS0->i = 2;
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
pPS1 = pLevelMap;
while ( pPS1)
{
if ( pPS1->i ==2)
break;
else if ( pPS1->Next ==NULL &&
( pPS1->i > 2 || pPS1->i < 2))
fPass = TRUE;
pPS1 = pPS1->Next;
}
InvalidateRect ( hwnd, NULL, TRUE);
break;
}// end if 4 箱子的新位置
pPS1 = pPS1->Next;
}// end while 3
break;
} // end else if 2 箱子上面为空或目标
pPS0 = pPS0->Next;
} // end while 1 所有的位置
break;
case VK_LEFT:
pPS0 = pLevelMap;
while ( pPS0) // while 1
{
if ( pPS0->Position .x == iMan.x - 1 &&
pPS0->Position .y == iMan.y &&
pPS0->i == 4)
// 为空
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.x--;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
pPs2 = pDest;
// 找到人原位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x + 1 &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
SelectObject ( hdc, GetStockObject ( WHITE_BRUSH) );
Drawdest ( hdc , iMan.x + 1, iMan.y);
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
break;
}// end if
else if ( pPS0->Position .x == iMan.x - 1 &&
pPS0->Position .y == iMan.y &&
pPS0->i == 2)
// 为目的
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.x--;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
break;
} // end else if
else if ( pPS0->Position .x == iMan.x - 1 &&
pPS0->Position .y == iMan.y &&
pPS0->i == 3)
// else if 2
// 为箱子 箱子上面为空或目的
{
pPS1 = pLevelMap;
while ( pPS1) // while 3
{
if ( pPS1->Position .x == iMan.x - 2 &&
pPS1->Position .y == iMan.y &&
( pPS1->i == 4 || pPS1->i == 2)
)
// 找到箱子移动到的位置,即空或目的
// if 4
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
// 处理人
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.x--;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
// 还要处理箱子
// 分为箱子链和地图位置处理
// 先处理箱子链位置
pPs2 = pBox;
// 找到箱子原位置 也就是现在人的位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
Drawbox( hdc, pPs2->Position.x - 1, pPs2->Position.y);
pPs2->Position.x--;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
Drawbox( hdc, pPs2->Position.x, pPs2->Position.y );
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
// 重画箱子完成
pPS1->i = 3;
// 到达该位置
// 处理地图
pPS0->i = 4;
// 如果原位置是目的 则要改为目的
pPs2 = pDest;
// 找到箱子原位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
pPS0->i = 2;
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
pPS1 = pLevelMap;
while ( pPS1)
{
if ( pPS1->i ==2)
break;
else if ( pPS1->Next ==NULL &&
( pPS1->i > 2 || pPS1->i < 2))
fPass = TRUE;
pPS1 = pPS1->Next;
}
InvalidateRect ( hwnd, NULL, TRUE);
break;
}// end if 4 箱子的新位置
pPS1 = pPS1->Next;
}// end while 3
break;
} // end else if 2 箱子上面为空或目标
pPS0 = pPS0->Next;
} // end while 1 所有的位置
break;
case VK_RIGHT:
pPS0 = pLevelMap;
while ( pPS0) // while 1
{
if ( pPS0->Position .x == iMan.x + 1 &&
pPS0->Position .y == iMan.y &&
pPS0->i == 4)
// 为空
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.x++;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
pPs2 = pDest;
// 找到人原位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x - 1 &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
SelectObject ( hdc, GetStockObject ( WHITE_BRUSH) );
Drawdest ( hdc , iMan.x - 1, iMan.y);
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
break;
}// end if
else if ( pPS0->Position .x == iMan.x + 1 &&
pPS0->Position .y == iMan.y &&
pPS0->i == 2)
// 为目的
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.x++;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
break;
} // end else if
else if ( pPS0->Position .x == iMan.x + 1 &&
pPS0->Position .y == iMan.y &&
pPS0->i == 3)
// else if 2
// 为箱子 箱子上面为空或目的
{
pPS1 = pLevelMap;
while ( pPS1) // while 3
{
if ( pPS1->Position .x == iMan.x + 2 &&
pPS1->Position .y == iMan.y &&
( pPS1->i == 4 || pPS1->i == 2)
)
// 找到箱子移动到的位置,即空或目的
// if 4
{
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
// 处理人
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
iMan.x++;
iNum++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
DrawMan ( hdc, iMan);
// 还要处理箱子
// 分为箱子链和地图位置处理
// 先处理箱子链位置
pPs2 = pBox;
// 找到箱子原位置 也就是现在人的位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
Drawbox( hdc, pPs2->Position.x + 1, pPs2->Position.y);
pPs2->Position.x++;
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
Drawbox( hdc, pPs2->Position.x, pPs2->Position.y );
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
// 重画箱子完成
pPS1->i = 3;
// 到达该位置
// 处理地图
pPS0->i = 4;
// 如果原位置是目的 则要改为目的
pPs2 = pDest;
// 找到箱子原位置
while ( pPs2) // while 5
{
if( pPs2->Position.x == iMan.x &&
pPs2->Position.y == iMan.y )
// iMan.y已减1
{
pPS0->i = 2;
break;
}// end if
pPs2 = pPs2->Next;
}//end while 5
pPS1 = pLevelMap;
while ( pPS1)
{
if ( pPS1->i ==2)
break;
else if ( pPS1->Next ==NULL &&
( pPS1->i > 2 || pPS1->i < 2))
fPass = TRUE;
pPS1 = pPS1->Next;
}
InvalidateRect ( hwnd, NULL, TRUE);
break;
}// end if 4 箱子的新位置
pPS1 = pPS1->Next;
}// end while 3
break;
} // end else if 2 箱子上面为空或目标
pPS0 = pPS0->Next;
} // end while 1 所有的位置
break;
}
return 0;
case WM_TIMER:
hdc = GetDC ( hwnd);
MoveCoordinate ( hdc, cxClient, cyClient);
// DrawMap ( hdc, cxClient, cyClient, iBoxMapTmp);
if ( fPass)
{
fPass = FALSE;
iLevel++;
iNum = 0;
// pLevelMap处理
pPS0 = pLevelMap->Next;
while ( pPS0 && pPS0->Next )
{
free ( pLevelMap );
pLevelMap = pPS0;
pPS0 = pPS0->Next;
}
free ( pPS0);
pPs2 = pDest->Next;
while ( pPs2 && pPs2->Next )
{
free ( pDest);
pDest = pPs2;
pPs2 = pPs2->Next;
}
free ( pPs2);
pPs2 = pBox->Next;
while ( pPs2 && pPs2->Next )
{
free ( pBox);
pBox = pPs2;
pPs2 = pPs2->Next;
}
free ( pPs2);
pLevelMap = InitNewLevel ( iLevel);
pDest = InitDest ( pLevelMap);
iMan = InitMan ( pLevelMap);
pBox = InitBox ( pLevelMap);
InvalidateRect( hwnd, NULL, TRUE);
}
bMan = !bMan;
if ( bMan)
SelectObject ( hdc, GetStockObject ( BLACK_PEN) );
else
SelectObject ( hdc, GetStockObject ( WHITE_PEN) );
DrawMan ( hdc, iMan);
SetTextAlign ( hdc, TA_LEFT | TA_TOP);
TextOut ( hdc, -200, 0, "Level:", 6);
SetTextAlign ( hdc, TA_RIGHT | TA_TOP);
TextOut ( hdc, -20, 0, szBuffer,
wsprintf ( szBuffer, "%2d", iLevel) );
SetTextAlign ( hdc, TA_LEFT | TA_TOP);
TextOut ( hdc, -200, 50, "Num:" , 4 );
SetTextAlign ( hdc, TA_RIGHT | TA_TOP);
TextOut ( hdc, -20, 50, szBuffer,
wsprintf ( szBuffer, "%2d", iNum) );
SetTextAlign ( hdc, TA_LEFT | TA_TOP);
// 目的地有问题
// DrawBox( hdc, pBox);
// DrawDest( hdc, pDest);
ReleaseDC ( hwnd, hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint ( hwnd, &ps);
MoveCoordinate ( hdc, cxClient, cyClient);
// 输出文字
SetTextAlign ( hdc, TA_LEFT | TA_TOP);
TextOut ( hdc, -200, 0, "Level:", 6);
SetTextAlign ( hdc, TA_RIGHT | TA_TOP);
TextOut ( hdc, -20, 0, szBuffer,
wsprintf ( szBuffer, "%2d", iLevel) );
SetTextAlign ( hdc, TA_LEFT | TA_TOP);
TextOut ( hdc, -200, 50, "Num:" , 4 );
SetTextAlign ( hdc, TA_RIGHT | TA_TOP);
TextOut ( hdc, -20, 50, szBuffer,
wsprintf ( szBuffer, "%2d", iNum) );
SetTextAlign ( hdc, TA_LEFT | TA_TOP);
// 输出图形
DrawMap ( hdc, pLevelMap);
Dra