3.实现人机交互
玩家利用键盘的四个方向键操纵小动物在迷宫中移动,为了捕获玩家对键盘的操作,需要在对话框中重载PreTranslateMessage函数,函数只对上、下、左、右四个按键进行处理,根据前进方向是否有围墙来移动小动物,移动小动物的函数为MoveIcon()。实现的代码如下:
BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_KEYDOWN) /*如果玩家按下按键*/
{
switch (pMsg->wParam)
{
case VK_UP: /*按“上”键,如上方无围墙,则调用MoveIcon移动小动物*/
if ( !maze.HWall[int(maze.current_x)][int(maze.current_y)] )
MoveIcon(VK_UP);
break;
case VK_DOWN: /*向下*/
if ( !maze.HWall[int(maze.current_x)][int(maze.current_y+1)] )
MoveIcon(VK_DOWN);
break;
case VK_LEFT: /*向左*/
if ( !maze.VWall[int(maze.current_x)][int(maze.current_y)] )
MoveIcon(VK_LEFT);
break;
case VK_RIGHT: /*向右*/
if ( !maze.VWall[int(maze.current_x+1)][int(maze.current_y)] )
MoveIcon(VK_RIGHT);
break;
default:
break;
}
}
|