📄 qiyuanview.cpp
字号:
ArtificialMaze[9].CarPos.x=ArtificialMaze[9].StartPos.x=1;
ArtificialMaze[9].CarPos.y=ArtificialMaze[9].StartPos.y=0;
ArtificialMaze[9].GoalPos.x=14;
ArtificialMaze[9].GoalPos.y=15;
ArtificialMaze[9].Road[0]=ArtificialMaze[9].StartPos;
ArtificialMaze[9].name="人工迷宫";//{{1,2},{3,4},{5,6}};
int a9[MOSTNUM][MOSTNUM]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},//0
{0,0,0,0,0,0,1,1,0,1,1,0,0,0,1,1,1,1},//1
{1,0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1},//2
{1,0,0,1,1,0,1,1,0,1,1,0,1,1,1,1,1,1},//3
{1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,1,1,1},//4
{1,0,1,0,0,0,0,1,0,1,0,1,1,0,1,1,1,1},//5
{1,0,1,0,0,0,0,1,0,1,0,1,1,0,1,1,1,1},//6
{1,0,1,0,1,1,0,1,0,1,0,1,1,0,1,1,1,1},//7
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},//8
{1,0,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1},//9
{1,0,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1},//10
{1,0,1,1,0,0,0,0,0,0,0,0,0,1,0,1,1,1},//11
{1,0,1,1,0,0,0,1,0,1,0,0,0,1,0,1,1,1},//12
{1,0,1,1,0,0,0,1,0,1,0,0,0,1,0,1,1,1},//13
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1},//14
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};//15
for(i=0;i<MOSTNUM;i++)
{ for(j=0;j<MOSTNUM;j++)
{
ArtificialMaze[9].MazeMap[i][j]=a9[i][j];
}
}
}
CQiyuanView::~CQiyuanView()
{
}
BOOL CQiyuanView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CQiyuanView drawing
void CQiyuanView::OnDraw(CDC* pDC)
{
CQiyuanDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//加载迷宫墙为图
CBitmap wall;
wall.LoadBitmap(IDB_BITMAP2);
BITMAP bmp;
wall.GetBitmap(&bmp);
//加载小车位图
LoadCar();
CBitmap car;
switch (RobotNum)
{
case 0: //加载第一个机器人
{
car.LoadBitmap(IDB_BITMAP5);
}
break;
case 1: //加载第2个机器人
{
car.LoadBitmap(IDB_BITMAP6);
}
break;
case 2: //加载第3个机器人
{
car.LoadBitmap(IDB_BITMAP7);
}
break;
case 3: //加载第4个机器人
{
car.LoadBitmap(IDB_BITMAP8);
}
break;
case 4: //加载第5个机器人
{
car.LoadBitmap(IDB_BITMAP9);
}
break;
case 5: //加载第6个机器人
{
car.LoadBitmap(IDB_BITMAP10);
}
break;
case 6: //加载第7个机器人
{
car.LoadBitmap(IDB_BITMAP11);
}
break;
case 7: //加载第8个机器人
{
car.LoadBitmap(IDB_BITMAP12);
}
break;
case 8: //加载第9个机器人
{
car.LoadBitmap(IDB_BITMAP13);
}
break;
case 9: //加载第10个机器人
{
car.LoadBitmap(IDB_BITMAP14);
}
break;
default :
car.LoadBitmap(IDB_BITMAP4);
break;
}
BITMAP bmp1;
car.GetBitmap(&bmp1);
CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);
//创建几个画刷和画笔用于画东西
m_pBlackPen = new CPen(PS_SOLID,1,BLACK);
m_pSetWinPen = new CPen(PS_SOLID,1,WHITE);
//画刷
m_pWhiteBrush = new CBrush(WHITE);
m_pBlackBrush = new CBrush(BLACK);
m_pSetWinBrush = new CBrush(SetWin);
CRect rect;
GetClientRect(&rect);
m_WinWidth=rect.Width();//总用户区的大小
m_WinHeight=rect.Height();
m_SetWinWidth=(m_WinWidth*7)/20;//设置区的大小
m_SetWinHeight=m_WinHeight;
m_MazeWinWidth=m_WinWidth-m_SetWinWidth;//迷宫区的大小
m_MazeWinHeight=m_WinHeight;
MapWidth=m_MazeWinWidth/ViewMaze.col;
MapHeight=m_MazeWinHeight/ViewMaze.row;
// pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY);
//绘制迷宫和小车以及路径
if(!FirstGame)
{
for (index1=0;index1<ViewMaze.row;index1++)
{
for (index2=0;index2<ViewMaze.col;index2++)
{
if (ViewMaze.MazeMap[index1][index2]==WALL)//是墙则贴墙的图
{
dcCompatible.SelectObject(&wall);
pDC->StretchBlt(index2*MapWidth,index1*MapHeight,MapWidth,MapHeight,&dcCompatible,
0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
//释放位图资源
DeleteObject(wall);
}//是墙则贴墙的图
}
}
}
if(!FirstGame)//是车则贴车的图
{
dcCompatible.SelectObject(&car);
pDC->StretchBlt((ViewMaze.CarPos.y)*MapWidth,(ViewMaze.CarPos.x)*MapHeight,MapWidth,MapHeight,&dcCompatible,
0,0,bmp1.bmWidth,bmp1.bmHeight,SRCCOPY);
//释放位图资源
DeleteObject(car);
}//是车则贴车的图
//画上迷宫设置区
//选用默认画刷,绘制迷宫设置区域的背景
if (!GameEndFlag)
{
if(!FirstGame)
{
pDC->SelectObject(m_pSetWinBrush);
pDC->Rectangle(m_MazeWinWidth ,0, m_WinWidth ,m_MazeWinHeight);
pDC -> SetBkColor(SetWin);
pDC -> SetTextColor(WHITE);
pDC -> SetBkColor(BLACK);
pDC -> SetTextColor(WHITE);
CString MyText;
MyText.Format("迷宫大小:");
pDC -> TextOut(m_MazeWinWidth+20, 0+20, MyText);
MyText.Format("%d 行%d列",ViewMaze.row,ViewMaze.col);
pDC -> TextOut(m_MazeWinWidth+20, 0+40,MyText);
pDC -> TextOut(m_MazeWinWidth+20, 0+80, "起止点:");
MyText.Format("起点:%d行%d列 终点:%d行%d列",ViewMaze.StartPos.x+1,ViewMaze.StartPos.y+1,ViewMaze.GoalPos.x+1,ViewMaze.GoalPos.y+1);
pDC -> TextOut(m_MazeWinWidth+20, 0+100,MyText);
pDC -> TextOut(m_MazeWinWidth+20, 0+140, "小车位置:");
MyText.Format("%d 行%d列",ViewMaze.CarPos.x+1,ViewMaze.CarPos.y+1);
pDC -> TextOut(m_MazeWinWidth+20, 0+160,MyText);
pDC -> TextOut(m_MazeWinWidth+20, 0+200,"小车速度:");
MyText.Format("%d ",UserSpeed);
pDC -> TextOut(m_MazeWinWidth+20, 0+220,MyText);
pDC -> TextOut(m_MazeWinWidth+20, 0+260,"路径情况:");
if(FoundWayFlag)
{
pDC -> TextOut(m_MazeWinWidth+20, 0+280," 存在路径");
}
else
{
pDC -> TextOut(m_MazeWinWidth+20, 0+280," 无路径");
}
pDC -> TextOut(m_MazeWinWidth+20, 0+320,"游戏运行情况:");
if(OnTheEndFlag)
{
MyText.Format("到达终点");
}
else
{
if(ArtificialWalkFlag)
{
MyText.Format("人工行走");
}
else
{
if(GameStartFlag)
{
if(GamePauseFlag)
{
MyText.Format("游戏暂停");
}
else
{
MyText.Format("正在行走");
}
}
else
{
MyText.Format("等待开始");
}
}
}
pDC -> TextOut(m_MazeWinWidth+20, 0+340,MyText);
pDC -> TextOut(m_MazeWinWidth+20, 0+380,"提示:人工方式方向键控制。");
}
}
//删除设备环境
dcCompatible.DeleteDC();
delete(m_pBlackPen);
delete(m_pWhiteBrush);
delete(m_pBlackBrush);
}
/////////////////////////////////////////////////////////////////////////////
// CQiyuanView diagnostics
#ifdef _DEBUG
void CQiyuanView::AssertValid() const
{
CView::AssertValid();
}
void CQiyuanView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CQiyuanDoc* CQiyuanView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CQiyuanDoc)));
return (CQiyuanDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CQiyuanView message handlers
void CQiyuanView::OnNewGameSet()
{
// TODO: Add your command handler code here
if(GameSet())//读取用户设置
{
while (HaveFoundFlag==0)
{
CreateMaze(); //生成迷宫
GameRun(); //按要求行走
}
}
//设之定时器用于定时刷新显示
}
BOOL CQiyuanView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
/* UINT MapWidth,MapHeight;
CBitmap tile;
CBitmap ball;
BITMAP bmp;
CRect rect;
*/
/* CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);
//背景的黑笔
m_pBGBrush= new CBrush(BGColor);
CRect rectBG;
GetClientRect(&rectBG);
pDC->SelectObject(m_pBGBrush);
pDC->Rectangle(0 ,0, rectBG.Width() ,rectBG.Height());
delete(m_pBGBrush);
*/
return CView::OnEraseBkgnd(pDC);
}
void CQiyuanView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(ArtificialWalkFlag)
{
// if(OnTheEndFlag)
// {
//ArtificialWalkFlag=0;
// return ;
// }
ArtificialDire=nChar;
switch (ArtificialDire)
{
case 37: //向左可走,(可走的条件是没超出边界且为空,下同)
{
if(ViewMaze.MazeMap[ViewMaze.CarPos.x][ViewMaze.CarPos.y-1]==0)
{
//更新显示
CRect windowSonRect(MapWidth*ViewMaze.CarPos.y,MapHeight*ViewMaze.CarPos.x,MapWidth*ViewMaze.CarPos.y+MapWidth,MapHeight*ViewMaze.CarPos.x+MapHeight);
InvalidateRect(&windowSonRect);
CRect windowSonNextRect(MapWidth*(ViewMaze.CarPos.y-1),MapHeight*ViewMaze.CarPos.x,MapWidth*(ViewMaze.CarPos.y-1)+MapWidth,MapHeight*ViewMaze.CarPos.x+MapHeight);
InvalidateRect(&windowSonNextRect);
//向左走一步
ViewMaze.CarPos.y-=1;
//更新文字——小车位置
CRect windowTextCarposition(m_MazeWinWidth+20, 0+160,m_MazeWinWidth+100, 0+180);
InvalidateRect(&windowTextCarposition);
}
}
break;
case 38: //向上可走,(可走的条件是没超出边界且为空,下同)
{
if(ViewMaze.MazeMap[ViewMaze.CarPos.x-1][ViewMaze.CarPos.y]==0)
{
//更新显示
CRect windowSonRect(MapWidth*ViewMaze.CarPos.y,MapHeight*ViewMaze.CarPos.x,MapWidth*ViewMaze.CarPos.y+MapWidth,MapHeight*ViewMaze.CarPos.x+MapHeight);
InvalidateRect(&windowSonRect);
CRect windowSonNextRect(MapWidth*(ViewMaze.CarPos.y),MapHeight*(ViewMaze.CarPos.x-1),MapWidth*ViewMaze.CarPos.y+MapWidth,MapHeight*(ViewMaze.CarPos.x-1)+MapHeight);
InvalidateRect(&windowSonNextRect);
//向上走一步
ViewMaze.CarPos.x-=1;
//更新文字——小车位置
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -