📄 catcatchratview.cpp
字号:
// CatCatchRatView.cpp : implementation of the CCatCatchRatView class
//
#include "stdafx.h"
#include "CatCatchRat.h"
#include "GameSlectDlg.h"
#include <mmsystem.h>
#include "CatCatchRatDoc.h"
#include "CatCatchRatView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCatCatchRatView
IMPLEMENT_DYNCREATE(CCatCatchRatView, CView)
BEGIN_MESSAGE_MAP(CCatCatchRatView, CView)
//{{AFX_MSG_MAP(CCatCatchRatView)
ON_COMMAND(IDM_Hard, OnHard)
ON_COMMAND(IDM_Start, OnStart)
ON_COMMAND(ID_Stop, OnStop)
ON_COMMAND(ID_Restart, OnRestart)
ON_COMMAND(ID_Normal, OnNormal)
ON_COMMAND(ID_Easy, OnEasy)
ON_UPDATE_COMMAND_UI(ID_Easy, OnUpdateEasy)
ON_UPDATE_COMMAND_UI(ID_Normal, OnUpdateNormal)
ON_UPDATE_COMMAND_UI(ID_Restart, OnUpdateRestart)
ON_UPDATE_COMMAND_UI(ID_Stop, OnUpdateStop)
ON_UPDATE_COMMAND_UI(IDM_Hard, OnUpdateHard)
ON_UPDATE_COMMAND_UI(IDM_Start, OnUpdateStart)
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_COMMAND(ID_GaSel, OnGaSel)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CCatCatchRatView construction/destruction
CCatCatchRatView::CCatCatchRatView()
{
//OnOpiontMusic();
m_nDifficulty=1;
m_timeWork=m_timeSet=20;
m_nCatSpeed=5;
m_nRatSpeed=3;
/*GameSlectDlg dlg;
if(dlg.DoModal()==IDOK)
{
RATMAX=dlg.m_RatMount;
m_nDifficulty=dlg.m_GameLevel;
m_nCatSpeed+=m_nDifficulty;
m_nRatSpeed+=(m_nDifficulty*4);
}*/
CCatCatchRatView::SetGameLevel();
m_bRatLive=new bool[RATMAX];
m_nRatDirection=new int[RATMAX];
m_nRatx=new int[RATMAX];
m_nRaty=new int[RATMAX];
m_rectRat=new CRect[RATMAX];
// TODO: add construction code here
m_bmCat[0].LoadBitmap(IDB_CatUp);
m_bmCat[1].LoadBitmap(IDB_CatDown);
m_bmCat[2].LoadBitmap(IDB_CatLeft);
m_bmCat[3].LoadBitmap(IDB_CatRight);
m_bmRat[0].LoadBitmap(IDB_RatUp);
m_bmRat[1].LoadBitmap(IDB_RatDown);
m_bmRat[2].LoadBitmap(IDB_RatLeft);
m_bmRat[3].LoadBitmap(IDB_RatRight);
BITMAP bmCat;
m_bmCat[2].GetBitmap(&bmCat);
m_nCatw=bmCat.bmWidth;
m_nCath=bmCat.bmHeight*2;
BITMAP bmRat[4];
m_bmRat[0].GetBitmap(&bmRat[0]);
m_bmRat[1].GetBitmap(&bmRat[1]);
m_bmRat[2].GetBitmap(&bmRat[2]);
m_bmRat[3].GetBitmap(&bmRat[3]);
m_nRatw=bmRat[0].bmWidth;
m_nRath=bmRat[0].bmHeight;
m_rectClient=CRect(20,20,580,380);
m_nRat=RATMAX;
StartFlag=-1;
OnOpiontMusic();
m_nCatx=m_rectClient.left+m_rectClient.Width()/2;
m_nCaty=m_rectClient.top+m_rectClient.Height()/2;
m_rectCat=CRect(m_nCatx,m_nCaty,m_nCatx+m_nCatw,m_nCaty+m_nCath);
m_nCatDirection=2;
srand((unsigned)time(NULL));
for(int i=0;i<RATMAX;i++)
{
m_nRatx[i]=m_rectClient.left+rand()%(m_rectClient.Width()-m_nRatw);
m_nRaty[i]=m_rectClient.top+rand()%(m_rectClient.Height()-m_nRath);
m_rectRat[i]=CRect(m_nRatx[i],m_nRaty[i],m_nRatx[i]+m_nRatw,m_nRaty[i]+m_nRath);
m_nRatDirection[i]=rand()%4;
m_bRatLive[i]=TRUE;
}
/*m_nDifficulty=1;
m_timeWork=m_timeSet=20;
m_nCatSpeed=5;
m_nRatSpeed=3;*/
//CCatCatchRatView::SetGameLevel();
}
CCatCatchRatView::~CCatCatchRatView()
{
}
BOOL CCatCatchRatView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCatCatchRatView drawing
void CCatCatchRatView::OnDraw(CDC* pDC)
{
CCatCatchRatDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CBitmap m_bmp;
m_bmp.LoadBitmap(IDB_BITMAP1);
BITMAP bm;
m_bmp.GetObject(sizeof(BITMAP),&bm);
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
CBitmap*pOldbmp=dcMem.SelectObject(&m_bmp);
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);
dcMem.SelectObject(pOldbmp);
// TODO: add draw code for native data here
pDC->Rectangle(CRect(19,19,581,381));
CString strTime;
CString strRat;
CString strLevel;
CString catspeed;
CString ratspeed;
strTime.Format("剩余时间:%2d:%2d:%2d",m_timeWork.GetHours(),m_timeWork.GetMinutes(),m_timeWork.GetSeconds());
strRat.Format("等抓老鼠:%3d",m_nRat);
strLevel.Format("现在的关数是:%3d",m_nDifficulty);
catspeed.Format("现在猫的速度是:%3d",m_nCatSpeed);
ratspeed.Format("现在鼠的速度是:%3d",m_nRatSpeed);
pDC->TextOut(590,100,strTime);
pDC->TextOut(590,140,strRat);
pDC->TextOut(590,180,strLevel);
pDC->TextOut(590,200,catspeed);
pDC->TextOut(590,220,ratspeed);
CDC *memDC;
memDC=new CDC[RATMAX];
//CDC memDC[RATMAX];
CDC memCatDC;
CBitmap *pOldDC;
memCatDC.CreateCompatibleDC(NULL);
pOldDC=memCatDC.SelectObject(&m_bmCat[m_nCatDirection]);
pDC->BitBlt(m_nCatx,m_nCaty,m_nCatw,m_nCath,&memCatDC,0,0,SRCCOPY);
memCatDC.SelectObject(pOldDC);
for(int i=0;i<RATMAX;i++)
{
if(m_bRatLive[i])
{
memDC[i].CreateCompatibleDC(NULL);
pOldDC=memDC[i].SelectObject(&m_bmRat[m_nRatDirection[i]]);
pDC->BitBlt(m_nRatx[i],m_nRaty[i],m_nRatw,m_nRath,&memDC[i],0,0,SRCCOPY);
memDC[i].SelectObject(pOldDC);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CCatCatchRatView printing
BOOL CCatCatchRatView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCatCatchRatView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCatCatchRatView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCatCatchRatView diagnostics
#ifdef _DEBUG
void CCatCatchRatView::AssertValid() const
{
CView::AssertValid();
}
void CCatCatchRatView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCatCatchRatDoc* CCatCatchRatView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCatCatchRatDoc)));
return (CCatCatchRatDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCatCatchRatView message handlers
void CCatCatchRatView::OnHard()
{
// TODO: Add your command handler code here
m_nDifficulty=3;
m_nCatSpeed=2;
m_nRatSpeed=8;
m_timeSet=10;
}
void CCatCatchRatView::OnStart()
{
// TODO: Add your command handler code here
if(StartFlag)
m_timeStart=m_timeStart.GetCurrentTime();
SetTimer(1,100,NULL);
SetTimer(2,3000,NULL);
StartFlag=1;
}
void CCatCatchRatView::OnStop()
{
// TODO: Add your command handler code here
KillTimer(1);
KillTimer(2);
StartFlag=0;
}
void CCatCatchRatView::OnRestart()
{
// TODO: Add your command handler code here
StartFlag=-1;
m_nRat=RATMAX;
m_timeWork=m_timeSet;
m_nCatDirection=2;
m_nCatx=m_rectClient.left+m_rectClient.Width()/2;
m_nCaty=m_rectClient.top+m_rectClient.Height()/2;
m_rectCat=CRect(m_nCatx,m_nCaty,m_nCatx+m_nCatw,m_nCaty+m_nCath);
srand((unsigned)time(NULL));
for(int i=0;i<RATMAX;i++)
{
m_nRatx[i]=m_rectClient.left+rand()%(m_rectClient.Width()-m_nRatw);
m_nRaty[i]=m_rectClient.top+rand()%(m_rectClient.Height()-m_nRath);
m_rectRat[i]=CRect(m_nRatx[i],m_nRaty[i],m_nRatx[i]+m_nRatw,m_nRaty[i]+m_nRath);
m_nRatDirection[i]=rand()%4;
m_bRatLive[i]=TRUE;
}
Invalidate();
}
void CCatCatchRatView::OnNormal()
{
// TODO: Add your command handler code here
m_nDifficulty=1;
m_nCatSpeed=5;
m_nRatSpeed=3;
m_timeSet=20;
}
void CCatCatchRatView::OnEasy()
{
// TODO: Add your command handler code here
m_nDifficulty=2;
m_nCatSpeed=8;
m_nRatSpeed=2;
m_timeSet=40;
}
void CCatCatchRatView::OnUpdateEasy(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_nDifficulty==0);
pCmdUI->Enable(StartFlag==-1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -