分享
 
 
 

[原创]用C++(TC3.0)做的贪吃蛇游戏---(2)

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

//**画蛇的相关类,实现时并没有用到多态性**//

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有