虚拟歌手苏琴吧 关注:66贴子:354
  • 0回复贴,共1

C++——坦克大战

只看楼主收藏回复

C++——坦克大战
经典的打坦克游戏,自己写哒~\(≧▽≦)/~
玩法:按ASDW控制坦克移动,空格开火【没有设置暂停功能_(:зゝ∠)_,算了╮(╯▽╰)╭】
游戏界面:
︽代表我方坦克
Ξ代表敌方坦克
o代表我方炮弹
·代表敌方炮弹
游戏代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <mmsystem.h>
#pragma comment (lib,"winmm.lib")
MCIDEVICEID dev[1];
MCI_OPEN_PARMS mciOpen;
MCI_PLAY_PARMS mciPlay;
using namespace std;
const int M=16;
const int N=20;
const int X=20;
class tank;
class game;
class boom
{
private:
friend tank;
friend game;
int x,y;
int w;
public:
boom(int x,int y,int w);
boom();
void move();
};
boom::boom()
{
}
boom::boom(int x,int y,int w)
{
this->x=x;
this->y=y;
this->w=w;
}
void boom::move()
{
switch(w)
{
case 0:x++;break;
case 1:x--;break;
case 2:y++;break;
case 3:y--;break;
}
}
class tank
{
private:
friend game;
boom b[M*N];
int x,y;
int w;
int f;
int num_boom;
public:
tank();
void move();
void move(int a);
void fire();
void disfire();
};
tank::tank()
{
num_boom=0;
}
void tank::disfire()
{
for(int i=0;i<num_boom;i++)
{
if(b[i].x<0 || b[i].x>=M || b[i].y<0 || b[i].y>N)
{
for(int j=i;j<num_boom;j++)
{
b[j]=b[j+1];
}
num_boom--;
}
}
}
void tank::fire()
{
boom *temp = new boom(x,y,w);
b[num_boom]=*temp;
num_boom++;
delete temp;
}
void tank::move()
{
w=rand()%4;
switch(w)
{
case 0:if(x<M-1)x++;break;
case 1:if(x>0)x--;break;
case 2:if(y<M-1)y++;break;
case 3:if(y>0)y--;break;
}
}
void tank::move(int a)
{
w=a;
switch(w)
{
case 0:if(x<M-1)x++;break;
case 1:if(x>0)x--;break;
case 2:if(y<M-1)y++;break;
case 3:if(y>0)y--;break;
}
}
class game
{
private:
tank me,other[X];
int num_other;
int score;
public:
game();
void start();
void display();
int play();
};
game::game()
{
score=0;
num_other=0;
}
int game::play()
{
char c;
if(kbhit())
{
c=getch();
if(c<'a')c+=32;
switch(c)
{
case 'd':me.move(0);break;
case 'a':me.move(1);break;
case 's':me.move(2);break;
case 'w':me.move(3);break;
case ' '+32:
me.fire();
mciSendCommand (dev[0], MCI_CLOSE, NULL, NULL);
{
mciOpen.lpstrElementName="biu.mp3";
if(mciSendCommand(NULL,MCI_OPEN, MCI_OPEN_ELEMENT ,(DWORD)&mciOpen)==0)
dev[0]=mciOpen.wDeviceID;
else
mciOpen.wDeviceID=dev[0];
mciPlay.dwFrom = 0;
mciSendCommand(mciOpen.wDeviceID,MCI_PLAY,MCI_NOTIFY | MCI_FROM,(DWORD)&mciPlay);
}
break;
}
}
me.disfire();
for(int i=0;i<me.num_boom;i++)
{
me.b[i].move();
for(int j=0;j<num_other;j++)
{
if(me.b[i].x==other[j].x && me.b[i].y==other[j].y)
{
score++;
mciSendCommand (dev[0], MCI_CLOSE, NULL, NULL);
{
mciOpen.lpstrElementName="boom.mp3";
if(mciSendCommand(NULL,MCI_OPEN, MCI_OPEN_ELEMENT ,(DWORD)&mciOpen)==0)
dev[0]=mciOpen.wDeviceID;
else
mciOpen.wDeviceID=dev[0];
mciPlay.dwFrom = 0;
mciSendCommand(mciOpen.wDeviceID,MCI_PLAY,MCI_NOTIFY | MCI_FROM,(DWORD)&mciPlay);
}
for(int k=j;k<num_other;k++)
{
other[k]=other[k+1];
}
num_other--;
}
}
}
for(i=0;i<num_other;i++)
{
other[i].move();
int p=rand()%100;
if(p<10)other[i].fire();
other[i].disfire();
for(int j=0;j<other[i].num_boom;j++)
{
other[i].b[j].move();
if(me.x==other[i].b[j].x && me.y==other[i].b[j].y)
{
return -1;
}
}
}
return num_other;
}
void game::display()
{
cout<<"┏";
for(int i=0;i<M;i++)
{
cout<<"━";
}
cout<<"┓\n";
for(i=0;i<N;i++)
{
cout<<"┃";
for(int j=0;j<M;j++)
{
int flag=0;
if(j==me.x && i==me.y && flag==0)
{
flag=1;
switch(me.w)
{
case 0:cout<<"》";break;
case 1:cout<<"《";break;
case 2:cout<<"︾";break;
case 3:cout<<"︽";break;
}
}
for(int k=0;k<num_other;k++)
{
if(j==other[k].x && i==other[k].y && flag==0)
{
cout<<"Ξ";flag=1;
}
}
for(k=0;k<me.num_boom;k++)
{
if(j==me.b[k].x && i==me.b[k].y && flag==0)
{
cout<<"o";flag=1;
}
}
for(k=0;k<num_other;k++)
{
for(int l=0;l<other[k].num_boom;k++)
{
if(j==other[k].b[l].x && i==other[k].b[l].y && flag==0)
{
cout<<"·";flag=1;
}
}
}
if(flag==0)cout<<" ";
}
cout<<"┃"<<endl;
}
cout<<"┗";
for(i=0;i<M;i++)
{
cout<<"━";
}
cout<<"┛\n";
}
void game::start()
{
me.x=M/2;
me.y=N-1;
me.w=3;
for(int i=0;i<X;i++)
{
other[i].x=rand()%M;
other[i].y=rand()%N;
other[i].w=rand()%4;
}
num_other=i;
}
void start()
{
cout<<"坦 克 大 战\n\t\t\t——by Swiss"<<endl;
PlaySound("start.wav", NULL, SND_FILENAME | SND_ASYNC);
cout<<"\t  ┃"<<endl;
cout<<"\t  ┃"<<endl;
cout<<"\t〓━╋━〓"<<endl;
cout<<"\t〓╭┸╮〓"<<endl;
cout<<"\t〓│ │〓"<<endl;
cout<<"\t〓│○│〓"<<endl;
cout<<"\t〓╰─╯〓"<<endl;
cout<<"\t〓━━━〓"<<endl;
system("pause");
}
int main()
{
game g;
start();
int f=X;
g.start();
PlaySound("bgm.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
while(1)
{
cout<<"坦克大战\t得分:"<<X-f<<endl;
g.display();
f=g.play();
if(f<=0)break;
Sleep(100);
system("cls");
}
if(f==0)cout<<"你赢了!"<<endl;
else
{
mciSendCommand (dev[0], MCI_CLOSE, NULL, NULL);
{
mciOpen.lpstrElementName="bang.mp3";
if(mciSendCommand(NULL,MCI_OPEN, MCI_OPEN_ELEMENT ,(DWORD)&mciOpen)==0)
dev[0]=mciOpen.wDeviceID;
else
mciOpen.wDeviceID=dev[0];
mciPlay.dwFrom = 0;
mciSendCommand(mciOpen.wDeviceID,MCI_PLAY,MCI_NOTIFY | MCI_FROM,(DWORD)&mciPlay);
}
cout<<"你输了!"<<endl;
PlaySound("no.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}
system("pause");
return 0;
}
下载地址:http://pan.baidu.com/s/1o6pfGRC


IP属地:江西1楼2015-12-13 07:12回复