?? program1view.cpp
字號:
// PROGRAM1View.cpp : implementation of the CPROGRAM1View class
//
#include "stdafx.h"
#include "PROGRAM1.h"
#include "PROGRAM1Doc.h"
#include "PROGRAM1View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPROGRAM1View
IMPLEMENT_DYNCREATE(CPROGRAM1View, CView)
BEGIN_MESSAGE_MAP(CPROGRAM1View, CView)
//{{AFX_MSG_MAP(CPROGRAM1View)
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_COMMAND(ID_MENU_NEWGAME, OnMenuNewgame)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CPROGRAM1View construction/destruction
CPROGRAM1View::CPROGRAM1View()
{
// TODO: add construction code here
Food=FALSE;
Score=0;
srand(GetTickCount());
}
CPROGRAM1View::~CPROGRAM1View()
{
}
BOOL CPROGRAM1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CPROGRAM1View drawing
void CPROGRAM1View::OnDraw(CDC* pDC)
{
CPROGRAM1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
SetTimer(10,60,NULL); // 設置龍速度
CString sText; // 分數
sText.Format("Score= %u",Score); // 格式化分數
pDC->TextOut(10,10,sText); // 輸出分數
if(theDRAGON.IsDead==FALSE)
{
pDC->TextOut(FoodX,FoodY,"★");
pDC->TextOut(theDRAGON.GetX(0),theDRAGON.GetY(0),"¤"); // 顯示龍頭
for(int i=1;i<theDRAGON.Lenth;i++) // 顯示
pDC->TextOut(theDRAGON.GetX(i),theDRAGON.GetY(i),"■"); // 龍身
// 顯示食物
}
}
/////////////////////////////////////////////////////////////////////////////
// CPROGRAM1View printing
BOOL CPROGRAM1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CPROGRAM1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CPROGRAM1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CPROGRAM1View diagnostics
#ifdef _DEBUG
void CPROGRAM1View::AssertValid() const
{
CView::AssertValid();
}
void CPROGRAM1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPROGRAM1Doc* CPROGRAM1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPROGRAM1Doc)));
return (CPROGRAM1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPROGRAM1View message handlers
void CPROGRAM1View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
///////////////////////////////////方向控制///////////////////////////
if( nChar==VK_UP && theDRAGON.GetX(1) != theDRAGON.GetX(0) ) theDRAGON.Direction=1;
else if( nChar==VK_DOWN && theDRAGON.GetX(1) != theDRAGON.GetX(0) ) theDRAGON.Direction=2;
else if( nChar==VK_LEFT && theDRAGON.GetY(1) != theDRAGON.GetY(0) ) theDRAGON.Direction=3;
else if( nChar==VK_RIGHT && theDRAGON.GetY(1) != theDRAGON.GetY(0) ) theDRAGON.Direction=4;
//////////////////////////////////////////////////////////////////////
}
void CPROGRAM1View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
////更新窗口/////
OnUpdate(NULL,NULL,NULL);
////////////////
//////////////////////////設置食物坐標///////////////////////////
if(Food==FALSE) /////當沒有食物時
{
RandFood(&FoodX,&FoodY); /////食物坐標
Food=TRUE; /////這時食物有無為“TRUE”
}
/////////////////////////////////////////////////////////////////
if(theDRAGON.IsDead==FALSE) ////// 判斷龍是否死亡
{
theDRAGON.DgnMove(); ////// 使龍移動
/////當龍頭與龍體相碰 或 與視圖邊相碰 則置死亡標志IsDead=TRUE///////
if(theDRAGON.IsBody(theDRAGON.GetX(0),theDRAGON.GetY(0))||(theDRAGON.GetX(0)<0||theDRAGON.GetY(0)<0||theDRAGON.GetX(0)>285||theDRAGON.GetY(0)>140))
{
theDRAGON.IsDead=TRUE;
AfxMessageBox("You DEAD Man");
}
///////////////////////////////////////////////////////////////////
//////////////////////當龍頭與食物相碰則“吃(Eat())”下食物/////////////////////////////////////////////
else
{
if(theDRAGON.GetX(0)==FoodX&&theDRAGON.GetY(0)==FoodY) // 判斷頭是否吃到食物
{
theDRAGON.Eat(); // “吃下食物”
Score+=10; // 加分
Food=FALSE; // 食物吃完,置食物標志為FALSE
}
}
}
CView::OnTimer(nIDEvent);
}
void CPROGRAM1View::OnMenuNewgame() // “新游戲”菜單項
{ // TODO: Add your command handler code here
theDRAGON.Init(); // 將龍初始化
Score=0; // 分數歸零
}
void CPROGRAM1View::RandFood(int *x, int *y) //隨機食物坐標布置函數
{
BOOL IsDragon; //“是否龍體”標志
do
{
IsDragon=FALSE; //置標志為“FALSE”
*x=rand()%285/theDRAGON.Width*theDRAGON.Width; // 食物坐標為龍
*y=rand()%140/theDRAGON.Width*theDRAGON.Width; // 體寬(Width)的倍數,且在視圖內
//檢查隨機坐標是否與龍體重合,若重合則重置//
for(int i=0;i<theDRAGON.Lenth;i++)
if(theDRAGON.GetX(i)==*x&&theDRAGON.GetY(i)==*y)
IsDragon=TRUE;
}
while(IsDragon==TRUE);
////////////////////////////////////////////
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -