//**画蛇的相关类,实现时并没有用到多态性**// class AbstractDraw{ public: virtual void Draw(int x,int y)=0; }; class Drawhead:public AbstractDraw{ public: void Draw(int x,int y); }; class Drawbody:public AbstractDraw{ public: void Draw(int x,int y); }; class Cleantail:public AbstractDraw{ public: void Draw(int x,int y); };
class DrawBean:public AbstractDraw{ public: void Draw(int x,int y); };
void Drawhead::Draw(int x,int y){ SuperCol Brush; int sx,sy; sx=getmaxx()/2-19*fudu;sy=getmaxy()/2-14*fudu; Brush.circle3(sx+(x+0.5)*fudu,sy+(y+0.5)*fudu,fudu/2,BLUE,RED); } void Drawbody::Draw(int x,int y){ SuperCol Brush; int sx,sy; sx=getmaxx()/2-19*fudu;sy=getmaxy()/2-14*fudu; Brush.bar2(sx+x*fudu,sy+y*fudu,sx+(x+1)*fudu,sy+(y+1)*fudu,BLUE); } void Cleantail::Draw(int x,int y){ SuperCol Brush; int sx,sy; sx=getmaxx()/2-19*fudu;sy=getmaxy()/2-14*fudu; Brush.bar2(sx+x*fudu,sy+y*fudu,sx+(x+1)*fudu,sy+(y+1)*fudu,BLACK); } void DrawBean::Draw(int x,int y){ SuperCol Brush; int sx,sy; sx=getmaxx()/2-19*fudu;sy=getmaxy()/2-14*fudu; Brush.circle3(sx+(x+0.5)*fudu,sy+(y+0.5)*fudu,fudu/2,YELLOW,WHITE); }
//* /*负责图形函数的起始与静态场景的绘制*/ *// class ScreenLayOut{ public: void GraphicStart(); void GraphicEnd(); void ErrorDetect(); void test(); };
void ScreenLayOut::GraphicStart(){ int gdriver,gmode; gdriver=DETECT; gmode=VGAHI; //Kinitgraph(&gdriver,&gmode,"D:\\TC\\BGI");//Bgi's address shoud be compitabled with Source File initgraph(&gdriver,&gmode,address); cleardevice();//Haha Got it!!! } void ScreenLayOut::GraphicEnd(){ closegraph(); } void ScreenLayOut::ErrorDetect(){ int errorcode=0; errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { GameStatur=Exit; printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); } } void ScreenLayOut::test(){ ; }
class Snake{//核心的蛇类 public: void Insert(Snake *now); void DirChan(); void CheckEatSelf(); void CheckEatBean(); Snake(){ mx=0;my=0;mdir=Right;next=NULL; } Snake(int x,int y,Direction dir,Snake *s=NULL){ mx=x; my=y; mdir=dir; next=s; } void show(); //void Bianli(); int getx(){return mx;} int gety(){return my;} int getdir(){return mdir;} void setdir(Direction p){mdir=p; DirChan(); } void Bianlidir();//精->遍链表并改变相关变量值的函数 bool EatSelf(); //void SetTail(){this->next=NULL;} bool HitWall();//碰墙检测 void MakeNewBean(Obstacle Ob[],int num,Bean &tmpD);//产生新豆 bool HitBrack(Obstacle Ob[],int num);//碰障碍物检测 bool GotBean(Bean dou);//吃到豆子的检测
private: int mx; int my; Direction mdir; Snake *next; //here you didn't use new/delete ,which is surely a hidden dangerous. //As a beginner ,I forgive you. }; void Snake::show(){ cout<<" x: "<<mx<<" y: "<<my<<" dir "<<mdir<<" -> "; if(next)next->show(); else cout<<"End\n"; } void Snake::DirChan(){ switch(this->mdir){ case Up:this->my--;break; case Down:this->my++;break; case Left:this->mx--;break; case Right:this->mx++;break; default:break; } } void Snake::Insert(Snake *now){ now->next=this->next;//注意了,now 的内部是可以访问的,这给没有友元的TC带来很大的方便. this->next=now; now->mx=this->mx;now->my=this->my;now->mdir=this->mdir; // cout<<(now->next==NULL); DirChan();//yes }
void Snake::Bianlidir(){//from tail to head ,not head to tail if(next){ next->Bianlidir(); next->mdir=this->mdir; next->DirChan(); } } bool Snake::EatSelf(){ bool flag=false; Snake *pTmp=NULL; if(next){ for(pTmp=next;pTmp!=NULL;pTmp=pTmp->next) {if((this->getx()==pTmp->getx())&&(this->gety()==pTmp->gety())) { flag=true; break; } } } return flag; } bool Snake::HitWall(){ bool flag=false; //Bad smell,for the bare number!. if((this->mx<0)||(this->mx>37)||(this->my<0)||(this->my>27)) { flag=true; } return flag; } void Snake::MakeNewBean(Obstacle Ob[],int num,Bean &tmpD){ bool flag=true; int i=0,j=0,dx=0,dy=0; Snake *pTmp=NULL; int tmpArray[38][28]; for(i=0;i<38;i++){ for(j=0;j<28;j++){ tmpArray[i][j]=0; } } randomize(); for(i=0;i<num;i++){ if(Ob[i].flag){ tmpArray[Ob[i].x][Ob[i].y]=1; } } if(next){ for(pTmp=next;pTmp!=NULL;pTmp=pTmp->next) { tmpArray[pTmp->getx()][pTmp->gety()]=1; } } flag=false; while(!flag) {dx=random(35)+1; dy=random(25)+1; // cout<<"dx"<<dx; if(tmpArray[dx][dy]==0){ tmpD.x=dx; tmpD.y=dy; flag=true; }
} gotoxy(2,2); printf("Score:%d",Score); Score++; }
bool Snake::HitBrack(Obstacle Ob[],int num) { bool flag=false; int i=0 ; for(i=0;i<num;i++){ if(Ob[i].flag){ if((Ob[i].x==this->getx())&&(Ob[i].y==this->gety()))flag=true; } } return flag; }
bool Snake::GotBean(Bean dou){ //Consider a multiple beans mode?Too complicated ,maybe. bool flag=false; if((dou.x==this->getx())&&(dou.y==this->gety()))flag=true; return flag; }

|