编译环境是vc6 高版本vs可能在有的地方会不兼容
写的很渣,大神勿喷
飞机由鼠标移动控制,鼠标左键开火
会随机生成五种不同的敌人,这五种敌人除了外观不同以外,血量也不同
当飞机遇到 S 后会增强武器
代码如下
#include
#include
#include
#include
#include
#include
#define WIDTH 680
#define HIGH 800
void Startup();
void Show();
void UserInput();
void Update_no();
void EndGame();
void DrawPlane();
void DrawBullet();
void DrawEnemy();
void SuperBullet();
void Clear();
void ShowData();
struct bullet
{
POINT pos;
int flag;
};
struct enemy
{
POINT pos;
int type;
int flag;
int life;
};
struct super
{
POINT pos;
int flag;
};
struct bullet bullet[50];
struct enemy enemy[50];
struct super super;
POINT pl[3];
int plane_x,plane_y;
int plane_width=60;
int plane_high=35;
int bulletnum=0;
int bullettype=1;
int bulletdamge=1;
int enemynum=0;
int enemytimes=1000;
int enemycount=enemytimes-20;
int enemytype[5]={10,15,20,25,30};
int enemylife[5]={1,2,3,4,5};
float enemyspeed=1.0;
int bulletspeed=5;
int k=0;
int gamelevel=1;
int socre=0;
int gameflag=1;
int main()
{
initgraph(WIDTH+200,HIGH);
setbkcolor(DARKGRAY);
cleardevice();
Startup();
BeginBatchDraw();
while (gameflag)
{
Show();
UserInput();
Show();
Update_no();
Show();
Clear();
}
EndGame();
_getch();
closegraph();
return 0;
}
void Startup()
{
//飞机坐标的初始化
plane_x=WIDTH/2;
plane_y=HIGH-100;
//对飞机的三个顶点的坐标初始化
pl[0].x=plane_x-plane_width/2;
pl[0].y=plane_y;
pl[1].x=plane_x+plane_width/2;
pl[1].y=plane_y;
pl[2].x=plane_x;
pl[2].y=plane_y-plane_high;
super.flag=0;
super.pos.y=-1;
for (int i=0;i<50;i++)
{
bullet[i].flag=0;
}
for (int j=0;i<50;i++)
{
enemy[j].flag=0;
enemy[j].type=0;
}
srand(unsigned(time(NULL)));
}
void Show()
{
DrawPlane();
DrawEnemy();
DrawBullet();
SuperBullet();
ShowData();
FlushBatchDraw();
}
void Clear()
{
Sleep(5);
cleardevice();
}
void DrawPlane()
{
setlinecolor(WHITE);
setlinestyle(BS_SOLID,2);
line(plane_x,plane_y-5,plane_x,plane_y-40);
setlinestyle(BS_SOLID,5);
line(pl[0].x+5,pl[0].y-5,pl[0].x+5,pl[0].y-20);
line(pl[1].x-5,pl[1].y-5,pl[1].x-5,pl[1].y-20);
setfillcolor(LIGHTRED);
setlinestyle(BS_SOLID,4);
setlinecolor(YELLOW);
fillpolygon(pl,3);
}
void DrawBullet()
{
setlinestyle(BS_SOLID,3);
if (bullettype>=6)
{
setlinecolor(LIGHTRED);
bulletdamge=3;
}
else if (bullettype>=4)
{
setlinecolor(LIGHTBLUE);
bulletdamge=2;
}
else if (bullettype==9)
bulletdamge=5;
else
setlinecolor(YELLOW);
for (int i=0;i<50;i++)
{
int x=bullet[i].pos.x;
int y=bullet[i].pos.y;
if (bullet[i].flag)
line(x,y,x,y-20);
}
}
void DrawEnemy()
{
if (enemycount==enemytimes)
{
enemy[enemynum].flag=1;
enemy[enemynum].pos.y=1;
enemy[enemynum].pos.x=rand()%(WIDTH-30)+30;
enemy[enemynum].type=rand()%5;
enemy[enemynum].life=enemylife[enemy[enemynum].type];
enemynum++;
k+=1;
enemycount=0;
if (k==15)
{
k=0;
if (enemytimes>100)
enemytimes-=100;
enemyspeed+=0.5;
gamelevel++;
}
}
for (int i=0;i<50;i++)
{
if (enemy[i].flag)
{
switch (enemy[i].type)
{
case 0:setfillcolor(GREEN);break;
case 1:setfillcolor(RED);break;
case 2:setfillcolor(LIGHTBLUE);break;
case 3:setfillcolor(LIGHTRED);break;
case 4:setfillcolor(BROWN);break;
}
solidcircle(enemy[i].pos.x,enemy[i].pos.y,enemytype[enemy[i].type]);
setlinestyle(BS_SOLID,4);
setlinecolor(RED);
line(enemy[i].pos.x-20,enemy[i].pos.y,enemy[i].pos.x+20,enemy[i].pos.y);
line(enemy[i].pos.x,enemy[i].pos.y-20,enemy[i].pos.x,enemy[i].pos.y+20);
}
}
}
void SuperBullet()
{
TCHAR superbullet[]=_T("S");
if (k%14==0&&super.flag==0)
{
super.pos.x=rand()%(WIDTH-30);
super.pos.y=0;
super.flag=1;
}
if (super.flag)
{
setlinestyle(BS_SOLID,1);
settextcolor(YELLOW);
settextstyle(30, 0, _T("宋体"));
outtextxy(super.pos.x+5,super.pos.y+5,superbullet);
}
}
void UserInput()
{
MOUSEMSG m;
while (MouseHit())
{
m=GetMouseMsg();
if (m.uMsg==WM_MOUSEMOVE&&m.x<=WIDTH-30)
{
plane_x=m.x;
plane_y=m.y+20;
}
if (m.uMsg==WM_LBUTTONUP)
{
if (bullettype==1)
{
bullet[bulletnum].pos.x=plane_x;
bullet[bulletnum].pos.y=plane_y-60;
bullet[bulletnum].flag=1;
bulletnum++;
}
else if (bullettype==2)
{
bullet[bulletnum].pos.x=pl[0].x;
bullet[bulletnum].pos.y=plane_y;
bullet[bulletnum].flag=1;
bulletnum++;
if (bulletnum==50)
bulletnum=0;
bullet[bulletnum].pos.x=pl[1].x;
bullet[bulletnum].pos.y=plane_y;
bullet[bulletnum].flag=1;
bulletnum++;
}
else
{
bullet[bulletnum].pos.x=plane_x;
bullet[bulletnum].pos.y=plane_y-60;
bullet[bulletnum].flag=1;
bulletnum++;
if (bulletnum==50)
bulletnum=0;
bullet[bulletnum].pos.x=pl[0].x;
bullet[bulletnum].pos.y=plane_y;
bullet[bulletnum].flag=1;
bulletnum++;
if (bulletnum==50)
bulletnum=0;
bullet[bulletnum].pos.x=pl[1].x;
bullet[bulletnum].pos.y=plane_y;
bullet[bulletnum].flag=1;
bulletnum++;
}
}
}
}
void Update_no()
{
int n;
//飞机的三个顶点的坐标变换
pl[0].x=plane_x-plane_width/2;
pl[0].y=plane_y;
pl[1].x=plane_x+plane_width/2;
pl[1].y=plane_y;
pl[2].x=plane_x;
pl[2].y=plane_y-plane_high;
for (int i=0;i<50;i++)
{
//子弹坐标的变化
if (bullet[i].flag)
{
bullet[i].pos.y-=bulletspeed;
if (bullet[i].pos.y<=0)
bullet[i].flag=0;
}
//敌人的坐标变化
if (enemy[i].flag)
{
enemy[i].pos.y+=enemyspeed;
if (enemy[i].pos.y>=HIGH)
enemy[i].flag=0;
//敌人碰到我
if (abs(enemy[i].pos.x-plane_x)+abs(enemy[i].pos.y-plane_y)<=60)
gameflag=0;;
}
}
//敌人碰到子弹
for (int j=0;j<50;j++)
{
if (bullet[j].flag)
{
for (int i=0;i<50;i++)
{
if (enemy[i].flag)
{
n=enemytype[enemy[i].type];
if (abs(bullet[j].pos.x-enemy[i].pos.x)+abs(bullet[j].pos.y-enemy[i].pos.y)<=30)
{
enemy[i].life-=bulletdamge;
bullet[j].flag=0;
if (enemy[i].life<=0)
{
enemy[i].flag=0;
socre+=enemylife[enemy[i].type];
}
}
}
}
}
}
if (bulletnum==50)
bulletnum=0;
if (enemynum==50)
enemynum=0;
enemycount+=10;
//武器增强遇到自己
if (abs(super.pos.x-plane_x)+abs(super.pos.y-plane_y)<=70)
{
if (bullettype<10)
bullettype++;
super.flag=0;
super.pos.y=-1;
if (bullettype>=4)
bulletspeed+=2;
}
if (super.pos.y>=0)
super.pos.y++;
if (super.pos.y>HIGH)
{
super.pos.y=-1;
super.flag=0;
}
//游戏难度
if (gamelevel%3==0)
{
for (int i=0;i<5;i++)
enemylife[i]++;
gamelevel=1;
}
}
void ShowData()
{
int gamehard;
gamehard=enemyspeed;
TCHAR s1[]=_T("得分:");
TCHAR s2[]=_T("游戏难度:");
TCHAR s3[]=_T("移动鼠标控制飞机,左键开火");
TCHAR s4[]=_T("游戏难度随时间增加");
TCHAR s5[]=_T("敌人速度与生命都会增加");
TCHAR s6[]=_T("越大的敌人血越后");
TCHAR s7[]=_T("碰到S可以增强武器威力");
char s[5];
char ss[5];
setlinecolor(WHITE);
setlinestyle(BS_SOLID,4);
settextcolor(WHITE);
line(WIDTH,0,WIDTH,HIGH);
settextstyle(30, 0, _T("宋体"));
settextcolor(WHITE);
sprintf(s, _T("%d"), socre);
sprintf(ss, _T("%d"), gamehard);
outtextxy(WIDTH+50, 60, s);
outtextxy(WIDTH+40,20,s1);
outtextxy(WIDTH+40,90,s2);
outtextxy(WIDTH+50,120,ss);
settextstyle(13, 0, _T("宋体"));
outtextxy(WIDTH+10,300,s3);
outtextxy(WIDTH+10,330,s4);
outtextxy(WIDTH+10,360,s5);
outtextxy(WIDTH+10,400,s6);
outtextxy(WIDTH+10,430,s7);
}
void EndGame()
{
EndBatchDraw();
ShowData();
TCHAR s[]=_T("碰到敌人 游戏结束");
TCHAR s1[]=_T("按任意键退出");
settextcolor(LIGHTRED);
settextstyle(30, 0, _T("宋体"));
outtextxy((WIDTH+200)/3,HIGH/2-200,s);
outtextxy((WIDTH+200)/3,HIGH/2-150,s1);
}