📄 例子view.cpp
字号:
// 例子View.cpp : implementation of the CMyView class
//
#include <time.h>
#include <stdlib.h>
#include "stdafx.h"
#include "例子.h"
#include "例子Doc.h"
#include "例子View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_COMMAND(ID_GAME_NEW, OnGameNew)
ON_WM_KEYDOWN()
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_MAP_NEW, OnMapNew)
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_MENU_start, OnMENUstart)
ON_WM_SETCURSOR()
ON_COMMAND(ID_MENU_end, OnMENUend)
ON_COMMAND(ID_MENU_cachu, OnMENUcachu)
ON_COMMAND(ID_MENU_qiang, OnMENUqiang)
ON_COMMAND(ID_MENU_cancel, OnMENUcancel)
ON_COMMAND(ID_MENU_play, OnMENUplay)
ON_COMMAND(ID_MENU_stop, OnMENUstop)
ON_COMMAND(IDM_time1000, Ontime1000)
ON_UPDATE_COMMAND_UI(IDM_time1000, OnUpdatetime1000)
ON_COMMAND(IDM_time700, Ontime700)
ON_UPDATE_COMMAND_UI(IDM_time700, OnUpdatetime700)
ON_COMMAND(IDB_time400, Ontime400)
ON_UPDATE_COMMAND_UI(IDB_time400, OnUpdatetime400)
ON_COMMAND(IDM_time200, Ontime200)
ON_UPDATE_COMMAND_UI(IDM_time200, OnUpdatetime200)
ON_COMMAND(IDB_time100, Ontime100)
ON_UPDATE_COMMAND_UI(IDB_time100, OnUpdatetime100)
ON_COMMAND(IDM_time50, Ontime50)
ON_UPDATE_COMMAND_UI(IDM_time50, OnUpdatetime50)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{
// TODO: add construction code here
m_bitmap[0].LoadBitmap(IDB_qiang);
m_bitmap[1].LoadBitmap(IDB_xing);
m_bitmap[2].LoadBitmap(IDB_black);
m_bitmap[3].LoadBitmap(IDB_qi);
//从资源加载
hcursor[0]=(HCURSOR)LoadImage(AfxGetResourceHandle(),
MAKEINTRESOURCE(IDC_qiang),//icon id
IMAGE_CURSOR,
15, 15, LR_DEFAULTSIZE);
hcursor[1]=(HCURSOR)LoadImage(AfxGetResourceHandle(),
MAKEINTRESOURCE(IDC_xing),//icon id
IMAGE_CURSOR,
15, 15, LR_DEFAULTSIZE);
hcursor[2]=(HCURSOR)LoadImage(AfxGetResourceHandle(),
MAKEINTRESOURCE(IDC_black),//icon id
IMAGE_CURSOR,
15, 15, LR_DEFAULTSIZE);
hcursor[3]=(HCURSOR)LoadImage(AfxGetResourceHandle(),
MAKEINTRESOURCE(IDC_qi),//icon id
IMAGE_CURSOR,
15, 15, LR_DEFAULTSIZE);
srand( (unsigned)time( NULL ) );
InitGame();
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBrush brush;
CRect rect(0,0,1200,800);
brush.CreateSolidBrush(RGB(0,0,0));
pDC->SelectObject(&brush);
pDC->FillRect(rect,&brush);//画背景
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
for(int x=0;x<m;x++)
for(int y=0;y<n;y++)
{
if(arry[x][y]==0)//画墙
{
MemDC.SelectObject(m_bitmap[0]);
pDC->BitBlt(x*15+x_start,y*15+y_start,15,15,&MemDC,0,0,SRCCOPY);
}
if(arry[x][y]==1)//画起点
{
MemDC.SelectObject(m_bitmap[1]);
pDC->BitBlt(x*15+x_start,y*15+y_start,15,15,&MemDC,0,0,SRCCOPY);
}
if(arry[x][y]==2)//画背景
{
MemDC.SelectObject(m_bitmap[2]);
pDC->BitBlt(x*15+x_start,y*15+y_start,15,15,&MemDC,0,0,SRCCOPY);
}
if(arry[x][y]==3)//画终点
{
MemDC.SelectObject(m_bitmap[3]);
pDC->BitBlt(x*15+x_start,y*15+y_start,15,15,&MemDC,0,0,SRCCOPY);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
bool CMyView::Over()
{
if((xnow==xend)&&(ynow==yend))
return true;
return 0;
}
void CMyView::OnGameNew()
{
// TODO: Add your command handler code here
InitGame();
Invalidate();
}
void CMyView::InitGame()
{
m_time=100;
stop=false;
m=50;
n=30;
x_start=12;//迷宫相对窗体的位置
y_start=15;
xstart=2;
ystart=10;
xnow=xstart;
ynow=ystart;
xend=m-2;
yend=n-2;
cur_flag=-1;
for(int x=0;x<m;x++)
for(int y=0;y<n;y++)
{
if(x==0||x==m-1||y==0||y==n-1)
arry[x][y]=0;//外围墙
else
{
arry[x][y]=rand()%3+4;//[4.......6]生成随机的墙
if(arry[x][y]==4)//1/3的概率
arry[x][y]=0;//让其成为真实的墙
else arry[x][y]=2;//2/3的概率为空,black背景色
}
}
arry[xnow][ynow]=1;//起点
arry[xend][yend]=3;//终点
}
void CMyView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC1;
pDC1=GetDC();
CDC MemDC1;
MemDC1.CreateCompatibleDC(pDC1);
char ch=(char)nChar;
switch(ch)
{
case 'S'://down
case 's':
if(arry[xnow][++ynow]==0)//如果有墙
{
--ynow;
MessageBeep(MB_OK);
break;
}
else
{
MemDC1.SelectObject(m_bitmap[2]);//black
pDC1->BitBlt(xnow*15+x_start,(ynow-1)*15+y_start,15,15,&MemDC1,0,0,SRCCOPY);
arry[xnow][ynow-1]=2;//背景色
MemDC1.SelectObject(m_bitmap[1]);//xing
pDC1->BitBlt(xnow*15+x_start,ynow*15+y_start,15,15,&MemDC1,0,0,SRCCOPY);
arry[xnow][ynow]=1;//xing
break;
}
case 'W'://up
case 'w':
if(arry[xnow][--ynow]==0)//如果有墙
{
++ynow;
MessageBeep(MB_OK);
break;
}
else
{
MemDC1.SelectObject(m_bitmap[2]);
pDC1->BitBlt(xnow*15+x_start,(ynow+1)*15+y_start,15,15,&MemDC1,0,0,SRCCOPY);
arry[xnow][ynow+1]=2;
MemDC1.SelectObject(m_bitmap[1]);
pDC1->BitBlt(xnow*15+x_start,ynow*15+y_start,15,15,&MemDC1,0,0,SRCCOPY);
arry[xnow][ynow]=1;
break;
}
case 'A'://left
case 'a':
if(arry[--xnow][ynow]==0)//如果有墙
{
++xnow;
MessageBeep(MB_OK);
break;
}
else
{
MemDC1.SelectObject(m_bitmap[2]);
pDC1->BitBlt((xnow+1)*15+x_start,ynow*15+y_start,15,15,&MemDC1,0,0,SRCCOPY);
arry[xnow+1][ynow]=2;
MemDC1.SelectObject(m_bitmap[1]);
pDC1->BitBlt(xnow*15+x_start,ynow*15+y_start,15,15,&MemDC1,0,0,SRCCOPY);
arry[xnow][ynow]=1;
break;
}
case 'D'://right
case 'd':
if(arry[++xnow][ynow]==0)//如果有墙
{
--xnow;
MessageBeep(MB_OK);
break;
}
else
{
MemDC1.SelectObject(m_bitmap[2]);
pDC1->BitBlt((xnow-1)*15+x_start,ynow*15+y_start,15,15,&MemDC1,0,0,SRCCOPY);
arry[xnow-1][ynow]=2;
MemDC1.SelectObject(m_bitmap[1]);
pDC1->BitBlt(xnow*15+x_start,ynow*15+y_start,15,15,&MemDC1,0,0,SRCCOPY);
arry[xnow][ynow]=1;
break;
}
default:
;
}
if(Over())//结束后刷新为前一个地图
{
MessageBox("You win!!");
for(int i1=0;i1<m;i1++)
for(int j=0;j<n;j++)
if(arry[i1][j]!=0)
{
if(xstart==i1&&ystart==j)
arry[i1][j]=1;
else if(xend==i1&¥d==j)
arry[i1][j]=3;
else arry[i1][j]=2;
}
xnow=xstart;//恢复起点,终点是固定的
ynow=ystart;
Invalidate();
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CMyView::OnFileOpen()
{
// TODO: Add your command handler code here
CFileDialog dlg(TRUE,"maz",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"迷宫地图(*.MAZ)|*.maz|All Files|*.*||",this);
if(dlg.DoModal()==IDOK)
dlg.GetFileName();
else
return;
CString str;
int i,j,s;
CStdioFile file;
if(file.Open(dlg.GetFileName(),CFile::modeRead)==0)
{
AfxMessageBox("save error!");
return;
}
CArchive ar(&file,CArchive::load);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
ar.ReadString(str);
sscanf(str,"%d",&s);
if(s==0)
arry[i][j]=0;
if(s==1)
{
arry[i][j]=1;
xnow=i;
ynow=j;
}
if(s==3)
{
arry[i][j]=3;
xend=i;
yend=j;
}
if(s==-1)
arry[i][j]=-1;
}
file.Close();
ar.Close();
Invalidate(false);//更新,不用OnPaint()
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -