标签:
#include "ntank101.h" using namespace std; void Menu(); void playsound(int curstate); BasicObj::BasicObj(int _x,int _y) { x=_x; y=_y; next=NULL; _del=0; } Tank::Tank(int _x,int _y):BasicObj(_x,_y) { _id=TANK_PLAYER; direct=DIRECT_UP; //health=2; link_id=TANK; _tank=ege::newimage(); ege::getimage(_tank,"image/player1_up.png",0,0); } Tank::Tank(int _x,int _y,int _direct):BasicObj(_x,_y) { _id=TANK_AI; direct=_direct; //health=1; link_id=TANK; _tank=ege::newimage(); if(_direct&DIRECT_UP) ege::getimage(_tank,"image/enemy_up.png",0,0); if(_direct&DIRECT_DOWN) ege::getimage(_tank,"image/enemy_down.png",0,0); if(_direct&DIRECT_LEFT) ege::getimage(_tank,"image/enemy_left.png",0,0); if(_direct&DIRECT_RIGHT) ege::getimage(_tank,"image/enemy_right.png",0,0); } void Tank::print() { ege::putimage(x,y,_tank); } int Tank::getdata() { return (_id|direct|link_id); } void Tank::ChangeG(int direction,int info) { if(info&TANK_PLAYER){ if(direction==DIRECT_UP){ ege::getimage(_tank,"image/player1_up.png",0,0); direct=DIRECT_UP; } if(direction==DIRECT_RIGHT){ ege::getimage(_tank,"image/player1_right.png",0,0); direct=DIRECT_RIGHT; } if(direction==DIRECT_DOWN){ ege::getimage(_tank,"image/player1_down.png",0,0); direct=DIRECT_DOWN; } if(direction==DIRECT_LEFT){ ege::getimage(_tank,"image/player1_left.png",0,0); direct=DIRECT_LEFT; } } if(info&TANK_AI){ if(direction==DIRECT_UP){ ege::getimage(_tank,"image/enemy_up.png",0,0); direct=DIRECT_UP; } if(direction==DIRECT_RIGHT){ ege::getimage(_tank,"image/enemy_right.png",0,0); direct=DIRECT_RIGHT; } if(direction==DIRECT_DOWN){ ege::getimage(_tank,"image/enemy_down.png",0,0); direct=DIRECT_DOWN; } if(direction==DIRECT_LEFT){ ege::getimage(_tank,"image/enemy_left.png",0,0); direct=DIRECT_LEFT; } } } Bullet::Bullet(int _x,int _y,int _direct):BasicObj(_x,_y) { direct=_direct; link_id=BULLET; _bullet=ege::newimage(); ege::getimage(_bullet,"image/bullet3.png",0,0); } int Bullet::getdata() { return (direct|link_id); } void Bullet::print() { ege::putimage(x,y,_bullet); } void Bullet::ChangeG(int type,int info){ } Map::Map(int _x,int _y,int _blocktype):BasicObj(_x,_y) { if(_blocktype==BRICK){ block=ege::newimage(); ege::getimage(block,"image/brick.png",0,0); } if(_blocktype==STEEL){ block=ege::newimage(); ege::getimage(block,"image/steel.png",0,0); } if(_blocktype==WATER){ block=ege::newimage(); ege::getimage(block,"image/water.png",0,0); } if(_blocktype==GRASS){ block=ege::newimage(); ege::getimage(block,"image/grass1.png",0,0); } if(_blocktype==BASE){ block=ege::newimage(); ege::getimage(block,"image/base.png",0,0); } blocktype=_blocktype; link_id=MAP; } void Map::print() { ege::putimage(x,y,block); } int Map::getdata() { return (blocktype|link_id); } void Map::ChangeG(int type,int info){ } Link::Link() { head=NULL; } void Link::insert_tank(int _x,int _y,int _direct) { BasicObj *p=head; BasicObj *node; node=new Tank(_x,_y,_direct); node->next=NULL; if(head==NULL) head=node; else{ while(p->next!=0){ p=p->next; } p->next=node; } } void Link::insert_tank(int _x,int _y) { BasicObj *p=head; BasicObj *node; node=new Tank(_x,_y); node->next=NULL; if(head==NULL) head=node; else{ while(p->next!=0){ p=p->next; } p->next=node; } } void Link::insert_bullet(int _x,int _y,int _direct) { BasicObj *p=head; BasicObj *node; node=new Bullet(_x,_y,_direct); node->next=NULL; if(head==NULL) head=node; else{ while(p->next!=0){ p=p->next; } p->next=node; } } void Link::insert_elem(int _x,int _y,int _blocktype) { BasicObj *p=head; BasicObj *node; node=new Map(_x,_y,_blocktype); node->next=NULL; if(head==NULL) head=node; else{ while(p->next!=0){ p=p->next; } p->next=node; } } void Link::delete_obj() { BasicObj *s,*p,*pre; if(head){ s=head; while(s){ p=head; pre=NULL; while(p&&p->_del!=1){ pre=p;p=p->next; } if(p){ if(!pre) head=head->next; else pre->next=p->next; delete p; } s=s->next; } } } void Link::clear_obj() { BasicObj *p=head; while(p){ p->_del=1; p=p->next; } delete_obj(); } void Link::print_obj() { BasicObj *p=head; while(p!=0){ //cout<<p<<endl; //cout<<"OBJ TEST INFO:"<<p->getdata()<<endl; //cout<<"-----"<<p->x<<" "<<p->y<<endl; p->print(); p=p->next; } } int Link::update_link(int *bornp) { BasicObj *curr=head; BasicObj *proced; int procetype; int curr_info; int ai_count=0; if (pause==0) { while(curr){ procetype=0; curr_info=curr->getdata(); if(curr_info&BULLET){ proced=Bullet_detector(curr,curr_info); procetype=1; if(proced&&proced->getdata()&TANK_AI) ai_count--; } if(curr_info&TANK_PLAYER){ proced=Player_detector(curr,curr_info); } if(curr_info&TANK_AI){ proced=Ai_detector(curr,curr_info); procetype=2; ai_count++; } if(procetype==1){ if(!proced){ if(curr_info&DIRECT_UP) curr->y=curr->y-6; if(curr_info&DIRECT_DOWN) curr->y=curr->y+6; if(curr_info&DIRECT_LEFT) curr->x=curr->x-6; if(curr_info&DIRECT_RIGHT) curr->x=curr->x+6; } //播放音频代码补充 } if(procetype==2){ AI(proced,curr); } curr=curr->next; } if(ai_count<=4){ Ai_born(bornp); } } return endscense; } int Link::GetCommand() { int key=0; if (GetAsyncKeyState(VK_LEFT)&0x8000) key|=CMD_LEFT; //多按键同时输入的处理 if (GetAsyncKeyState(VK_RIGHT)&0x8000) key|=CMD_RIGHT; if (GetAsyncKeyState(VK_UP)&0x8000) key|=CMD_UP; if (GetAsyncKeyState(VK_DOWN)&0x8000) key|=CMD_DOWN; if (GetAsyncKeyState(VK_SPACE)&0x8000) key|=CMD_FIRE; if (GetAsyncKeyState(VK_CONTROL)&0x8000) key|=CMD_PAUSE; if (GetAsyncKeyState(VK_ESCAPE)&0x8000) key|=CMD_QUIT; return key; } void Link::DispatchCommand(int key) { int i=key&CMD_UP|key&CMD_DOWN|key&CMD_LEFT|key&CMD_RIGHT|key&CMD_FIRE|key&CMD_PAUSE|key&CMD_QUIT; switch(i){ case CMD_PAUSE:OnPause();break; case CMD_QUIT:OnQuit();break; case CMD_FIRE:OnFire();playsound(0);break; case CMD_UP|CMD_LEFT:OnUp();break; case CMD_UP|CMD_LEFT|CMD_FIRE:OnUp();OnFire();playsound(0);break; case CMD_UP:OnUp();break; case CMD_UP|CMD_FIRE:OnUp();OnFire();playsound(0);break; case CMD_UP|CMD_RIGHT:OnUp();break; case CMD_UP|CMD_RIGHT|CMD_FIRE:OnUp();OnFire();playsound(0);break; case CMD_RIGHT:OnRight();break; case CMD_RIGHT|CMD_FIRE:OnRight();OnFire();playsound(0);break; case CMD_RIGHT|CMD_DOWN:OnDown();break; case CMD_RIGHT|CMD_DOWN|CMD_FIRE:OnDown();OnFire();playsound(0);break; case CMD_DOWN:OnDown();break; case CMD_DOWN|CMD_FIRE:OnDown();OnFire();playsound(0);break; case CMD_DOWN|CMD_LEFT:OnDown();break; case CMD_DOWN|CMD_LEFT|CMD_FIRE:OnDown();OnFire();playsound(0);break; case CMD_LEFT:OnLeft();break; case CMD_LEFT|CMD_FIRE:OnLeft();OnFire();playsound(0);break; } } BasicObj* Link::Player_detector(BasicObj* player,int player_info) { BasicObj* curr=head; int curr_info; int temp_player_x=player->x; int temp_player_y=player->y; if(player_info&DIRECT_UP){ if(temp_player_y<0){ player->y=player->y+2; return player; } while(curr){ curr_info=curr->getdata(); if(curr!=player){ if((temp_player_y-curr->y<40)&&(curr->y<temp_player_y)){ if(curr_info&MAP){ if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+BLOCK_WIDTH)) if(temp_player_y<curr->y+BLOCK_HEIGHT){ player->y=player->y+2; return curr; } } if(curr_info&TANK_PLAYER){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+TANK_PLAYER_UD_WIDTH)) if(temp_player_y<curr->y+TANK_PLAYER_UD_HEIGHT){ player->y=player->y+2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+TANK_PLAYER_LR_WIDTH)) if(temp_player_y<curr->y+TANK_PLAYER_LR_HEIGHT){ player->y=player->y+2; return curr; } } if(curr_info&TANK_AI){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+TANK_AI_UD_WIDTH)) if(temp_player_y<curr->y+TANK_AI_UD_HEIGHT){ player->y=player->y+2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+TANK_AI_LR_WIDTH)) if(temp_player_y<curr->y+TANK_AI_LR_HEIGHT){ player->y=player->y+2; return curr; } } } } curr=curr->next; } } if(player_info&DIRECT_DOWN){ if(temp_player_y+TANK_PLAYER_UD_HEIGHT>BORDERHEIGHT){ player->y=player->y-2; return player; } while(curr){ curr_info=curr->getdata(); if(curr!=player){ if((curr->y-temp_player_y<40)&&(curr->y>temp_player_y)){ if(curr_info&MAP){ if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+BLOCK_WIDTH)) if(temp_player_y+TANK_PLAYER_UD_HEIGHT>curr->y){ player->y=player->y-2; return curr; } } if(curr_info&TANK_PLAYER){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+TANK_PLAYER_UD_WIDTH)) if(temp_player_y+TANK_PLAYER_UD_HEIGHT>curr->y){ player->y=player->y-2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+TANK_PLAYER_LR_WIDTH)) if(temp_player_y+TANK_PLAYER_UD_HEIGHT>curr->y){ player->y=player->y-2; return curr; } } if(curr_info&TANK_AI){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+TANK_AI_UD_WIDTH)) if(temp_player_y+TANK_PLAYER_UD_HEIGHT>curr->y){ player->y=player->y-2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_player_x+TANK_PLAYER_UD_WIDTH>curr->x)&&(temp_player_x<curr->x+TANK_AI_LR_WIDTH)) if(temp_player_y+TANK_PLAYER_UD_HEIGHT>curr->y){ player->y=player->y-2; return curr; } } } } curr=curr->next; } } if(player_info&DIRECT_LEFT){ if(temp_player_x<0){ player->x=player->x+2; return player; } while(curr){ curr_info=curr->getdata(); if(curr!=player){ if((temp_player_x-curr->x<40)&&(curr->x<temp_player_x)){ if(curr_info&MAP){ if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+BLOCK_HEIGHT)) if(temp_player_x<curr->x+BLOCK_WIDTH){ player->x=player->x+2; return curr; } } if(curr_info&TANK_PLAYER){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+TANK_PLAYER_UD_HEIGHT)) if(temp_player_x<curr->x+TANK_PLAYER_UD_WIDTH){ player->x=player->x+2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+TANK_PLAYER_LR_HEIGHT)) if(temp_player_x<curr->x+TANK_PLAYER_LR_WIDTH){ player->x=player->x+2; return curr; } } if(curr_info&TANK_AI){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+TANK_AI_UD_HEIGHT)) if(temp_player_x<curr->x+TANK_AI_UD_WIDTH){ player->x=player->x+2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+TANK_AI_LR_HEIGHT)) if(temp_player_x<curr->x+TANK_AI_LR_WIDTH){ player->x=player->x+2; return curr; } } } } curr=curr->next; } } if(player_info&DIRECT_RIGHT){ if(temp_player_x+TANK_PLAYER_LR_WIDTH>BORDERWIDTH){ player->x=player->x-2; return player; } while(curr){ curr_info=curr->getdata(); if(curr!=player){ if((curr->x-temp_player_x<40)&&(curr->x>temp_player_x)){ if(curr_info&MAP){ if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+BLOCK_HEIGHT)) if(temp_player_x+TANK_PLAYER_LR_WIDTH>curr->x){ player->x=player->x-2; return curr; } } if(curr_info&TANK_PLAYER){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+TANK_PLAYER_UD_HEIGHT)) if(temp_player_x+TANK_PLAYER_LR_WIDTH>curr->x){ player->x=player->x-2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+TANK_PLAYER_LR_HEIGHT)) if(temp_player_x+TANK_PLAYER_LR_WIDTH>curr->x){ player->x=player->x-2; return curr; } } if(curr_info&TANK_AI){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+TANK_AI_UD_HEIGHT)) if(temp_player_x+TANK_PLAYER_LR_WIDTH>curr->x){ player->x=player->x-2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_player_y+TANK_PLAYER_LR_HEIGHT>curr->y)&&(temp_player_y<curr->y+TANK_AI_LR_HEIGHT)) if(temp_player_x+TANK_PLAYER_LR_WIDTH>curr->x){ player->x=player->x-2; return curr; } } } } curr=curr->next; } } return NULL; } BasicObj* Link::Bullet_detector(BasicObj* p,int p_info) { BasicObj* curr=head; int curr_info; int temp_p_x=p->x; int temp_p_y=p->y; while(curr){ curr_info=curr->getdata(); if(temp_p_x<0||temp_p_x>(BORDERWIDTH-BULLET_L)||temp_p_y<0||temp_p_y>(BORDERHEIGHT-BULLET_L)){ p->_del=1; return curr; } else{ if(curr_info&TANK){ //子弹撞击对象是坦克 if(curr_info&TANK_AI){ //AI坦克与子弹碰撞检测 if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_p_x+BULLET_L>curr->x)&&(temp_p_x<curr->x+TANK_AI_UD_WIDTH)) if((temp_p_y+BULLET_L>curr->y)&&(temp_p_y<curr->y+TANK_AI_UD_HEIGHT)){ p->_del=1; curr->_del=1; playsound(1); return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_x+BULLET_L>curr->x)&&(temp_p_x<curr->x+TANK_AI_LR_WIDTH)) if((temp_p_y+BULLET_L>curr->y)&&(temp_p_y<curr->y+TANK_AI_LR_HEIGHT)){ p->_del=1; curr->_del=1; playsound(1); return curr; } } if(curr_info&TANK_PLAYER){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) //撞击时因为玩家坦克方向不同长宽不等 if((temp_p_x+BULLET_L>curr->x)&&(temp_p_x<curr->x+TANK_PLAYER_UD_WIDTH)) //判断是否发生撞击 if((temp_p_y+BULLET_L>curr->y)&&(temp_p_y<curr->y+TANK_PLAYER_UD_HEIGHT)){ p->_del=1; Score=clock()-Score; playsound(2); gameover(0); //玩家坦克被毁,游戏结束 } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_x+BULLET_L>curr->x)&&(temp_p_x<curr->x+TANK_PLAYER_LR_WIDTH)) if((temp_p_y+BULLET_L>curr->y)&&(temp_p_y<curr->y+TANK_PLAYER_LR_HEIGHT)){ p->_del=1; Score=clock()-Score; playsound(2); gameover(0); //玩家坦克被毁,游戏结束 } } } if(curr_info&BRICK){ if((temp_p_x+BULLET_L>curr->x)&&(temp_p_x<curr->x+BLOCK_WIDTH)) if((temp_p_y+BULLET_L>curr->y)&&(temp_p_y<curr->y+BLOCK_HEIGHT)){ p->_del=1; curr->_del=1; return curr; } } if(curr_info&STEEL){ if((temp_p_x+BULLET_L>curr->x)&&(temp_p_x<curr->x+BLOCK_WIDTH)) if((temp_p_y+BULLET_L>curr->y)&&(temp_p_y<curr->y+BLOCK_HEIGHT)){ p->_del=1; return curr; } } if(curr_info&BASE){ if((temp_p_x+BULLET_L>curr->x)&&(temp_p_x<curr->x+40)) if((temp_p_y+BULLET_L>curr->y)&&(temp_p_y<curr->y+40)){ p->_del=1; curr->_del=1; Score=clock()-Score; playsound(2); gameover(1); } } if(curr_info&BULLET&&(p!=curr)){ //子弹和子弹相撞,不能是同一个子弹 if(((temp_p_y-curr->y)*(temp_p_y-curr->y)+(temp_p_x-curr->x)*(temp_p_x-curr->x))<100){ p->_del=1; curr->_del=1; return curr; } } } curr=curr->next; } return NULL; } BasicObj* Link::Ai_detector(BasicObj* p,int p_info) { BasicObj* curr=head; int curr_info; int temp_p_x=p->x; int temp_p_y=p->y; if(p_info&DIRECT_UP){ if(temp_p_y<0){ p->y=p->y+2; return p; } while(curr){ curr_info=curr->getdata(); if(curr!=p){ if((temp_p_y-curr->y<40)&&(curr->y<temp_p_y)){ if(curr_info&MAP){ if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+BLOCK_WIDTH)) if(temp_p_y<curr->y+BLOCK_HEIGHT){ p->y=p->y+2; return curr; } } if(curr_info&TANK_PLAYER){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+TANK_PLAYER_UD_WIDTH)) if(temp_p_y<curr->y+TANK_PLAYER_UD_HEIGHT){ p->y=p->y+2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+TANK_PLAYER_LR_WIDTH)) if(temp_p_y<curr->y+TANK_PLAYER_LR_HEIGHT){ p->y=p->y+2; return curr; } } if(curr_info&TANK_AI){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+TANK_AI_UD_WIDTH)) if(temp_p_y<curr->y+TANK_AI_UD_HEIGHT){ p->y=p->y+2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+TANK_AI_LR_WIDTH)) if(temp_p_y<curr->y+TANK_AI_LR_HEIGHT){ p->y=p->y+2; return curr; } } } } curr=curr->next; } } if(p_info&DIRECT_DOWN){ if(temp_p_y+TANK_AI_UD_HEIGHT>BORDERHEIGHT){ p->y=p->y-2; return p; } while(curr){ curr_info=curr->getdata(); if(curr!=p){ if((curr->y-temp_p_y<40)&&(curr->y>temp_p_y)){ if(curr_info&MAP){ if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+BLOCK_WIDTH)) if(temp_p_y+TANK_AI_UD_HEIGHT>curr->y){ p->y=p->y-2; return curr; } } if(curr_info&TANK_PLAYER){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+TANK_PLAYER_UD_WIDTH)) if(temp_p_y+TANK_AI_UD_HEIGHT>curr->y){ p->y=p->y-2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+TANK_PLAYER_LR_WIDTH)) if(temp_p_y+TANK_AI_UD_HEIGHT>curr->y){ p->y=p->y-2; return curr; } } if(curr_info&TANK_AI){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+TANK_AI_UD_WIDTH)) if(temp_p_y+TANK_AI_UD_HEIGHT>curr->y){ p->y=p->y-2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_x+TANK_AI_UD_WIDTH>curr->x)&&(temp_p_x<curr->x+TANK_AI_LR_WIDTH)) if(temp_p_y+TANK_AI_UD_HEIGHT>curr->y){ p->y=p->y-2; return curr; } } } } curr=curr->next; } } if(p_info&DIRECT_LEFT){ if(temp_p_x<0){ p->x=p->x+2; return p; } while(curr){ curr_info=curr->getdata(); if(curr!=p){ if((temp_p_x-curr->x<40)&&(curr->x<temp_p_x)){ if(curr_info&MAP){ if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+BLOCK_HEIGHT)) if(temp_p_x<curr->x+BLOCK_WIDTH){ p->x=p->x+2; return curr; } } if(curr_info&TANK_PLAYER){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+TANK_PLAYER_UD_HEIGHT)) if(temp_p_x<curr->x+TANK_PLAYER_UD_WIDTH){ p->x=p->x+2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+TANK_PLAYER_LR_HEIGHT)) if(temp_p_x<curr->x+TANK_PLAYER_LR_WIDTH){ p->x=p->x+2; return curr; } } if(curr_info&TANK_AI){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+TANK_AI_UD_HEIGHT)) if(temp_p_x<curr->x+TANK_AI_UD_WIDTH){ p->x=p->x+2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+TANK_AI_LR_HEIGHT)) if(temp_p_x<curr->x+TANK_AI_LR_WIDTH){ p->x=p->x+2; return curr; } } } } curr=curr->next; } } if(p_info&DIRECT_RIGHT){ if(temp_p_x+TANK_AI_LR_WIDTH>BORDERWIDTH){ p->x=p->x-2; return p; } while(curr){ curr_info=curr->getdata(); if(curr!=p){ if((curr->x-temp_p_x<40)&&(curr->x>temp_p_x)){ if(curr_info&MAP){ if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+BLOCK_HEIGHT)) if(temp_p_x+TANK_AI_LR_WIDTH>curr->x){ p->x=p->x-2; return curr; } } if(curr_info&TANK_PLAYER){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+TANK_PLAYER_UD_HEIGHT)) if(temp_p_x+TANK_AI_LR_WIDTH>curr->x){ p->x=p->x-2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+TANK_PLAYER_LR_HEIGHT)) if(temp_p_x+TANK_AI_LR_WIDTH>curr->x){ p->x=p->x-2; return curr; } } if(curr_info&TANK_AI){ if(curr_info&DIRECT_UP||curr_info&DIRECT_DOWN) if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+TANK_AI_UD_HEIGHT)) if(temp_p_x+TANK_AI_LR_WIDTH>curr->x){ p->x=p->x-2; return curr; } if(curr_info&DIRECT_LEFT||curr_info&DIRECT_RIGHT) if((temp_p_y+TANK_AI_LR_HEIGHT>curr->y)&&(temp_p_y<curr->y+TANK_AI_LR_HEIGHT)) if(temp_p_x+TANK_AI_LR_WIDTH>curr->x){ p->x=p->x-2; return curr; } } } } curr=curr->next; } } return NULL; } void Link::OnUp() { head->ChangeG(DIRECT_UP,TANK_PLAYER); head->y=head->y-2; //坦克每一帧移动的像素 } void Link::OnDown() { head->ChangeG(DIRECT_DOWN,TANK_PLAYER); head->y=head->y+2; } void Link::OnRight() { head->ChangeG(DIRECT_RIGHT,TANK_PLAYER); head->x=head->x+2; } void Link::OnLeft() { head->ChangeG(DIRECT_LEFT,TANK_PLAYER); head->x=head->x-2; } void Link::OnFire() { int info,i; info=head->getdata(); i=(info&DIRECT_UP)|(info&DIRECT_RIGHT)|(info&DIRECT_LEFT)|(info&DIRECT_DOWN); if(time(NULL)-open_fire>=1){ switch(i){ case DIRECT_UP:insert_bullet(head->x+11,head->y-BULLET_L,DIRECT_UP);break; //数值是为调整与枪管看起来像直线位置 case DIRECT_RIGHT:insert_bullet(head->x+31,head->y+12,DIRECT_RIGHT);break; case DIRECT_DOWN:insert_bullet(head->x+11,head->y+30+BULLET_L,DIRECT_DOWN);break; case DIRECT_LEFT:insert_bullet(head->x-BULLET_L,head->y+10,DIRECT_LEFT);break; } } open_fire=time(NULL); } void Link::OnPause() { if(pause==0) pause=1; if(pause==1) pause=0; } void Link::OnQuit() { endscense=1; } void Link::Ai_born(int *point) { int i=rand()%200; if(i==25||i==75||i==125||i==175){ i=rand()%3; switch(i){ case 0: i=rand()%7; if(i==0) insert_tank(point[0],point[1],DIRECT_UP); if(i==1||i==4) insert_tank(point[0],point[1],DIRECT_DOWN); if(i==2||i==5) insert_tank(point[0],point[1],DIRECT_LEFT); if(i==3||i==6) insert_tank(point[0],point[1],DIRECT_RIGHT); break; case 1: i=rand()%7; if(i==0) insert_tank(point[2],point[3],DIRECT_UP); if(i==1||i==4) insert_tank(point[2],point[3],DIRECT_DOWN); if(i==2||i==5) insert_tank(point[2],point[3],DIRECT_LEFT); if(i==3||i==6) insert_tank(point[2],point[3],DIRECT_RIGHT); break; case 2: i=rand()%7; if(i==0) insert_tank(point[4],point[5],DIRECT_UP); if(i==1||i==4) insert_tank(point[4],point[5],DIRECT_DOWN); if(i==2||i==5) insert_tank(point[4],point[5],DIRECT_LEFT); if(i==3||i==6) insert_tank(point[4],point[5],DIRECT_RIGHT); break; } } } void Link::AI(BasicObj *detect_obj,BasicObj *curr_ai) { int random_direct_change; int random_fire; int hit_direct_change; int curr_info=curr_ai->getdata(); int i=curr_info&DIRECT_UP|curr_info&DIRECT_DOWN|curr_info&DIRECT_LEFT|curr_info&DIRECT_RIGHT; if(!detect_obj){ random_direct_change=rand()%200; if(random_direct_change==100){ random_direct_change=rand()%10; if(random_direct_change==0||random_direct_change==1||random_direct_change==2) curr_ai->ChangeG(DIRECT_RIGHT,TANK_AI); if(random_direct_change==3||random_direct_change==4||random_direct_change==5) curr_ai->ChangeG(DIRECT_DOWN,TANK_AI); if(random_direct_change==6||random_direct_change==7||random_direct_change==8) curr_ai->ChangeG(DIRECT_LEFT,TANK_AI); if(random_direct_change==9) curr_ai->ChangeG(DIRECT_UP,TANK_AI); }else switch(i){ case DIRECT_UP: curr_ai->ChangeG(DIRECT_UP,TANK_AI); curr_ai->y=curr_ai->y-2;break; case DIRECT_RIGHT: curr_ai->ChangeG(DIRECT_RIGHT,TANK_AI); curr_ai->x=curr_ai->x+2;break; case DIRECT_DOWN: curr_ai->ChangeG(DIRECT_DOWN,TANK_AI); curr_ai->y=curr_ai->y+2;break; case DIRECT_LEFT: curr_ai->ChangeG(DIRECT_LEFT,TANK_AI); curr_ai->x=curr_ai->x-2;break; } }else{ hit_direct_change=rand()%5; switch(i){ case DIRECT_UP: if(hit_direct_change==0||hit_direct_change==3||hit_direct_change==4){ curr_ai->ChangeG(DIRECT_DOWN,TANK_AI); curr_ai->y=curr_ai->y+2;break; } if(hit_direct_change==1){ curr_ai->ChangeG(DIRECT_LEFT,TANK_AI); curr_ai->x=curr_ai->x-2;break; } if(hit_direct_change==2){ curr_ai->ChangeG(DIRECT_RIGHT,TANK_AI); curr_ai->x=curr_ai->x+2;break; } case DIRECT_RIGHT: if(hit_direct_change==0||hit_direct_change==3){ curr_ai->ChangeG(DIRECT_DOWN,TANK_AI); curr_ai->y=curr_ai->y+2;break; } if(hit_direct_change==1||hit_direct_change==4){ curr_ai->ChangeG(DIRECT_LEFT,TANK_AI); curr_ai->x=curr_ai->x-2;break; } if(hit_direct_change==2){ curr_ai->ChangeG(DIRECT_UP,TANK_AI); curr_ai->y=curr_ai->y-2;break; } case DIRECT_DOWN: if(hit_direct_change==0){ curr_ai->ChangeG(DIRECT_UP,TANK_AI); curr_ai->y=curr_ai->y-2;break; } if(hit_direct_change==1||hit_direct_change==3){ curr_ai->ChangeG(DIRECT_LEFT,TANK_AI); curr_ai->x=curr_ai->x-2;break; } if(hit_direct_change==2||hit_direct_change==4){ curr_ai->ChangeG(DIRECT_RIGHT,TANK_AI); curr_ai->x=curr_ai->x+2;break; } case DIRECT_LEFT: if(hit_direct_change==0){ curr_ai->ChangeG(DIRECT_UP,TANK_AI); curr_ai->y=curr_ai->y-2;break; } if(hit_direct_change==1||hit_direct_change==3){ curr_ai->ChangeG(DIRECT_DOWN,TANK_AI); curr_ai->y=curr_ai->y+2;break; } if(hit_direct_change==2||hit_direct_change==4){ curr_ai->ChangeG(DIRECT_RIGHT,TANK_AI); curr_ai->x=curr_ai->x+2;break; } } } random_fire=rand()%100; if(random_fire==0||random_fire==125||random_fire==250||random_fire==375||random_fire==500||random_fire==675||random_fire==750||random_fire==875||random_fire==1000) switch(i){ case DIRECT_UP:insert_bullet(curr_ai->x+12,curr_ai->y-BULLET_L,DIRECT_UP);break; case DIRECT_RIGHT:insert_bullet(curr_ai->x+29,curr_ai->y+12,DIRECT_RIGHT);break; case DIRECT_DOWN:insert_bullet(curr_ai->x+12,curr_ai->y+29,DIRECT_DOWN);break; case DIRECT_LEFT:insert_bullet(curr_ai->x-BULLET_L,curr_ai->y+12,DIRECT_LEFT);break; } } void showcase(int passsign) { int timer; timer=clock(); for( ;ege::is_run();ege::delay_fps(60)){ setfont(25, 0, "微软雅黑"); if(passsign==-1){ level=0; Menu(); } switch(level){ case 0: outtextxy(260, 300, "第一关"); break; case 1: outtextxy(260, 300, "第二关"); break; case 2: outtextxy(260, 300, "第三关"); break; } if(passsign==1){ outtextxy(260, 300, "通关!!!"); playsound(5); } if(clock()-timer>1000){ ege::cleardevice(); if(passsign==1) Menu(); playsound(3); MainLoop(); } } } void Menu() { int x=105,y=600; int first=0; int i=0; ege::PIMAGE intergraph; ege::PIMAGE selector; intergraph=ege::newimage(); selector=ege::newimage(); ege::getimage(intergraph,"image/welcome.jpg",0,0); ege::getimage(selector,"image/player1_right.png",0,0); setfont(25,0,"微软雅黑"); for( ;ege::is_run();ege::delay_fps(60)){ if(y>=200){ y-=5; ege::putimage(x,y,intergraph); }else{ if(!first){ ege::putimage(x+120,y+200,selector); outtextxy(x+160, y+200, "开始游戏"); outtextxy(x+160, y+235, "游戏说明"); } first=1; if (GetAsyncKeyState(VK_UP)&0x8000){ ege::cleardevice(); ege::putimage(x,y,intergraph); ege::putimage(x+120,y+200,selector); outtextxy(x+160, y+200, "开始游戏"); outtextxy(x+160, y+235, "游戏说明"); i=0; } if (GetAsyncKeyState(VK_DOWN)&0x8000){ ege::cleardevice(); ege::putimage(x,y,intergraph); ege::putimage(x+120,y+235,selector); outtextxy(x+160, y+200, "开始游戏"); outtextxy(x+160, y+235, "游戏说明"); i=1; } if(GetAsyncKeyState(VK_RETURN)&0x8000){ switch(i){ case 0: cout<<"start selected"<<endl; ege::cleardevice(); playsound(0); showcase(0); break; case 1: cout<<"help selected"<<endl; ege::cleardevice(); playsound(0); Help(); break; } } if(GetAsyncKeyState(VK_ESCAPE)&0x8000){ break; } } } } void LoadMap(Link* list,int *born,int maplevel) { int i=0,j=0,line=0; char buffer[41]; ifstream readmap; readmap.open("map.txt"); switch(maplevel){ //842字节为一个地图单元 TXT文本要跟目前的格式一样,不然地图单元错乱。 case 0:readmap.seekg(0,ios::beg);break; //这个switch可以加条件,就是关卡数,地图在TXT文件也要自己加上。SEEKG以842字节为单元加上去就行了。 case 1:readmap.seekg(842,ios::beg);break; case 2:readmap.seekg(1684,ios::beg);break; } for(i=0;i<20;i++) { readmap.getline(buffer,sizeof(buffer)); for(j=0;j<40;j++) if(buffer[j]!=',') map[line][j/2]=buffer[j]-48; line++; } list->insert_tank(192,558); list->insert_elem(280,555,BASE); born[0]=160;born[1]=0;born[2]=320;born[3]=0;born[4]=480;born[5]=0; for(i=0;i<20;i++) for(j=0;j<20;j++){ if(map[i][j]==1) list->insert_elem(i*BLOCK_WIDTH,j*BLOCK_HEIGHT,BRICK); //1砖 2铁 3水 4草 5基 if(map[i][j]==2) list->insert_elem(i*BLOCK_WIDTH,j*BLOCK_HEIGHT,STEEL); if(map[i][j]==3) list->insert_elem(i*BLOCK_WIDTH,j*BLOCK_HEIGHT,WATER); if(map[i][j]==4) list->insert_elem(i*BLOCK_WIDTH,j*BLOCK_HEIGHT,GRASS); if(map[i][j]==5) list->insert_elem(i*BLOCK_WIDTH,j*BLOCK_HEIGHT,BASE); } } void Help() { ege::cleardevice(); for( ;ege::is_run();ege::delay_fps(60)){ //说明自己改,游戏玩法之类的 setfont(25, 0, "微软雅黑"); outtextxy(60, 80, "仿90坦克大战ver 0.60(测试版DEMO)"); outtextxy(60, 170, "游戏控制说明:"); outtextxy(100, 200, "↑"); outtextxy(100, 235, "↓"); outtextxy(130, 235, "→"); outtextxy(60, 235, "←"); outtextxy(200, 200, "ESC键返回/退出 ENTER/回车键确认"); outtextxy(230, 235, "空格键开火"); outtextxy(60, 300, "图片及音频素材均来源自网络"); outtextxy(60, 330, "鸣谢:EGE图形库作者:EGE娘"); setfont(20, 0, "微软雅黑"); outtextxy(50, 530, "Copyright-2016 江师12级嵌入式~GAMEer. All rights reserved."); if(GetAsyncKeyState(VK_ESCAPE)&0x8000){ ege::cleardevice(); break; } } Menu(); } void playsound(int curstate) { switch(curstate){ case 0: PlaySound(TEXT("sound\\shoot.wav"),NULL,SND_ASYNC|SND_NODEFAULT); break; case 1: PlaySound(TEXT("sound\\hit.wav"),NULL,SND_ASYNC|SND_NODEFAULT); break; case 2: PlaySound(TEXT("sound\\eblow.wav"),NULL,SND_ASYNC|SND_NODEFAULT); break; case 3: PlaySound(TEXT("sound\\start.wav"),NULL,SND_ASYNC|SND_NODEFAULT); break; case 4: PlaySound(TEXT("sound\\lose.wav"),NULL,SND_ASYNC|SND_NODEFAULT); break; case 5: PlaySound(TEXT("sound\\03.wav"),NULL,SND_ASYNC|SND_NODEFAULT); break; }; } void printscore(int curtime,int statictime) { char s[20]; sprintf(s,"%4d",(curtime-statictime)); outtextxy(0,0,s); } void gameover(int type) { ege::PIMAGE u_died; ege::PIMAGE u_lost; u_died=ege::newimage(); u_lost=ege::newimage(); ege::getimage(u_died,"image/dead.png",0,0); ege::getimage(u_lost,"image/lost.png",0,0); setfont(25, 0, "微软雅黑"); playsound(4); for( ;ege::is_run();ege::delay_fps(60)){ if(type==0){ ege::putimage(0,300,u_died); outtextxy(10,380, "总得分:"); xyprintf(5,410,"%d",Score); } if(type==1){ ege::putimage(0,300,u_lost); outtextxy(10,380, "总得分:"); xyprintf(5,410,"%d",Score); } if(GetAsyncKeyState(VK_ESCAPE)&0x8000){ ege::cleardevice(); break; } } Menu(); } void MainLoop() { Link test0; clock_t Timer=0; int ai_born[6]={0}; int end=0; LoadMap(&test0,ai_born,level); Timer=clock(); //关卡计时器 setfont(25, 0, "微软雅黑"); for( ;ege::is_run();ege::delay_fps(60)) { //printscore(clock(),Timer); int key=test0.GetCommand(); if(key!=0) test0.DispatchCommand(key); end=test0.update_link(ai_born); ege::cleardevice(); test0.print_obj(); test0.delete_obj(); if((clock()-Timer)>=(30000+level*(30000*0.2))){ //进入下一关判断条件=时间,每关难度加20%时间,基础时间30S,可以自己改基础时间和加值 level++; ege::cleardevice(); test0.clear_obj(); LoadMap(&test0,ai_born,level); Timer=clock(); if(level>(3-1)){ level=0; showcase(1); } showcase(0); } if(end){ ege::cleardevice(); endscense=0; showcase(-1); break; } } } int main(int argc, char *argv[]) { ege::setinitmode(ege::INIT_ANIMATION); ege::initgraph(600,600); srand((unsigned int)time(NULL)); Menu(); ege::closegraph(); return 0; }
标签:
原文地址:http://blog.csdn.net/u014302603/article/details/51278360