www.gusucode.com > NOKIA手机游戏贪吃蛇完整版源码源码程序 > NOKIA手机游戏贪吃蛇完整版源码源码程序\code\DRAGON.cpp
//Download by http://www.NewXing.com // DRAGON.cpp: implementation of the DRAGON class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "PROGRAM1.h" #include "DRAGON.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// DRAGON::DRAGON() { Init(); } DRAGON::~DRAGON() { } void DRAGON::Move() { tail->rlink=first; first->llink=tail; first=tail; tail=tail->llink; tail->rlink=NULL; } DRAGON::DRAGONNODE::DRAGONNODE() { x=0; y=0; llink=NULL; rlink=NULL; } void DRAGON::UpMove() { tail->x=first->x; tail->y=first->y-Width; Move(); } void DRAGON::DownMove() { tail->x=first->x; tail->y=(first->y)+Width; Move(); } void DRAGON::LeftMove() { tail->x=first->x-Width; tail->y=first->y; Move(); } void DRAGON::RightMove() { tail->x=first->x+Width; tail->y=first->y; Move(); } int DRAGON::GetX(int nflag) { DRAGONNODE *current=first; for(int i=0;i<nflag;i++) if(current!=tail) current=current->rlink; return current->x; } int DRAGON::GetY(int nflag) { DRAGONNODE *current=first; for(int i=0;i<nflag;i++) if(current!=tail) current=current->rlink; return current->y; } void DRAGON::DgnMove() { if(Direction==1) UpMove(); if(Direction==2) DownMove(); if(Direction==3) LeftMove(); if(Direction==4) RightMove(); } BOOL DRAGON::IsBody(int x, int y) { for(int i=1;i<Lenth;i++) if(GetX(i)==x&&GetY(i)==y) return TRUE; return FALSE; } void DRAGON::Eat() { DRAGONNODE *newnode=new DRAGONNODE; newnode->llink=tail; tail->rlink=newnode; if(GetX(Lenth)==GetX(Lenth-1)) { newnode->x=tail->x; if(GetY(Lenth)>GetY(Lenth-1)) newnode->y=tail->y+1; else newnode->y=tail->y-1; } else if(GetY(Lenth)==GetY(Lenth-1)) { newnode->y=tail->y; if(GetX(Lenth)>GetX(Lenth-1)) newnode->y=tail->y+1; if(GetX(Lenth)>GetX(Lenth-1)) newnode->y=tail->y+1; } tail=newnode; Lenth++; } void DRAGON::Init() { tail=first=NULL; IsDead=FALSE; Direction=4; Lenth=6; Width=15; for(int i=0;i<Lenth;i++) { DRAGONNODE *newnode=new DRAGONNODE; newnode->x=Width+Width*i; newnode->y=Width; newnode->rlink=first; if(first!=NULL) first->llink=newnode; first=newnode; if(newnode->rlink==NULL) tail=newnode; } }