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;
}
}
}
本文地址:http://com.8s8s.com/it/it23607.htm