分享
 
 
 

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

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

class Game{//最为关键的游戏类

public:

void MainBody();

void GameInitialize();

void MapObstacle(Obstacle Ob[],int x);

void GraphicEnd();

void Keywaiting();

bool Continue();

void PressAnyKey();

};

void Game::GameInitialize(){

ScreenLayOut P;

SuperCol Brush;

int midx,midy,i;

P.GraphicStart();

P.ErrorDetect();

midx=getmaxx()/2;midy=getmaxy()/2;

Brush.bar2(midx-20*fudu,midy-15*fudu,midx+20*fudu,midy+15*fudu,LIGHTBLUE);

Brush.bar2(midx-19*fudu,midy-14*fudu,midx+19*fudu,midy+14*fudu,BLACK);

Brush.printword(midx-16*fudu, midy-19*fudu+5, "Snake",1,0,0,LIGHTGREEN);

Brush.printword(midx-2*fudu,midy-17*fudu+10,"Made by:EmilMatthew 05/1/16 ",0,0,1,BROWN);

}

void Game::MapObstacle(Obstacle Ob[],int x){

SuperCol Brush;

int i=0;

int midx=getmaxx()/2,midy=getmaxy()/2;

for(i=0;i<x;i++){

if(Ob[i].flag){

Brush.bar2(midx-19*fudu+Ob[i].x*fudu,midy-14*fudu+Ob[i].y*fudu,

midx-18*fudu+Ob[i].x*fudu,midy-13*fudu+Ob[i].y*fudu,LIGHTBLUE);

}

}

}

void Game::Keywaiting(){

gotoxy(5,10);

printf ("Pause");

while(bioskey(1)==0){;}

gotoxy(5,10);

printf(" ");

}

void Game::PressAnyKey(){

gotoxy(5,10);

printf("Press any key to continue...\n");

}

bool Game::Continue(){

// clrscr();

char tmp;

gotoxy(5,10);

printf(" ");

gotoxy(8,11);

printf("Oh ,you failure\n");

gotoxy(8,12);

printf("Try Again?(y/n)\n");

gotoxy(8,13);

scanf("%c",&tmp);

if(tmp=='y')return true;

else return false;

}

void Game::MainBody(){

//Declare Object or Variables

ScreenLayOut Test;

Drawhead sDh;

Drawbody sDb;

Cleantail sCt;

DrawBean dou;

Obstacle stock[5];

Bean douzi;

int key=0,index=0;

bool Gotbean=false,check;

bool KeyPressed=false;//如果按键过快,将出现不改变

Direction tmdir=Right;

//Set Start Data;

Snake w(1,0,Right,NULL);

Snake b2(2,0,Right,&w);

Snake b1(3,0,Right,&b2);

Snake h(4,0,Right,&b1);

Snake tb1[300];//(0,0,Right,NULL);

Snake tb2(0,0,Right,NULL);

randomize();

stock[0].flag=true;

stock[0].x=random(22)+5;

stock[0].y=random(26)+1;

douzi.flag=true;

Score=0;

MapObstacle(stock,1);

//Draw First Snake.

sDh.Draw(h.getx(),h.gety());

sDb.Draw(b1.getx(),b1.gety());

sDb.Draw(b2.getx(),b2.gety());

//Generate Bean.

h.MakeNewBean(stock,5,douzi);//this interface is not a good one,as I thought

dou.Draw(douzi.x,douzi.y);

while(GameStatur==Gaming){

while(bioskey(1)==0&&GameStatur==Gaming){

if(Gotbean){

//1Insert

h.Insert(&tb1[index]);

sDb.Draw(tb1[index].getx(),tb1[index].gety());

index++;

sDh.Draw(h.getx(),h.gety());

Gotbean=false;

//Not so perfect.

//Add random make bean code etc.

// w.SetTail();

h.MakeNewBean(stock,1,douzi);

dou.Draw(douzi.x,douzi.y);

}

else{

//2bian li

//At first ,you use this:*tb=&h,of course it just stands for h,

//can't work as your thought.

tb2=h;//If you use tb1=h,there will be an error ,because of the shallow copy

h.Bianlidir();

h.setdir(tmdir);

sDh.Draw(h.getx(),h.gety());

sDb.Draw(tb2.getx(),tb2.gety());

sCt.Draw(w.getx(),w.gety());

}

Delay2(0.15,SecPerFrame);

//Collision Check

//Eat self

if(h.EatSelf())

{ cout<<"Eat self"<<endl;

PressAnyKey();

GameStatur=Failure;

break;

}

//Collision with wall

if(h.HitWall())

{ cout<<"Hit Wall"<<endl;

PressAnyKey();

GameStatur=Failure;

break; //Attention here

}

//Collision with obstacle

if(h.HitBrack(stock,1))

{ cout<<"HitBrack"<<endl;

PressAnyKey();

GameStatur=Failure;

break;

}

if(h.GotBean(douzi)){

Gotbean=true;

}

KeyPressed=false;

}

//Check key

key=bioskey(0);

if(!KeyPressed)

{switch(key){

case KeyUp: if(tmdir!=Down){

tmdir=Up;

}

break;

case KeyDown:if(tmdir!=Up){

tmdir=Down; }

break;

case KeyLeft:if(tmdir!=Right){

tmdir=Left;}

break;

case KeyRight:if(tmdir!=Left){

tmdir=Right;}

break;

case KeyEnter:

//small KeyWaiting

Keywaiting();

break;

case KeyEsc:

GameStatur=Exit;

break;

default:

break;

}

KeyPressed=true;

}

}

if(GameStatur==Failure)

{

check=Continue();

if(check)GameStatur=Gaming;

else GameStatur=Exit;

}

}

void Game::GraphicEnd(){

ScreenLayOut P;

P.GraphicEnd();

}

void main(void){

//ScreenLayOut Gg;

Game Director;

while(GameStatur==Gaming){

Director.GameInitialize();

if(GameStatur!=Exit)Director.MainBody();

Director.GraphicEnd();

}

getch();

}

void Delay2(double Times,float SecPerFrame){

bool Running=true;

double Tmptime=(double)clock()/(double)CLOCKS_PER_SEC;

double caculate=0,delaying=SecPerFrame;

while(Running&&caculate<Times){

if((Tmptime+delaying)<((double)clock()/(double)CLOCKS_PER_SEC))

{Tmptime=(double)clock()/(double)CLOCKS_PER_SEC;

caculate+=delaying;

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有