www.gusucode.com > NOKIA手机游戏贪吃蛇完整版源码源码程序 > NOKIA手机游戏贪吃蛇完整版源码源码程序\code\PROGRAM1View.cpp

    //Download by http://www.NewXing.com
// 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);
        ////////////////////////////////////////////

}