#include <iostream>
#include<stdio.h>
#include <graphics.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <mmsystem.h>
#pragma comment (lib,"winmm.lib")
#include <conio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int kaishi();//470-590//580-650
int diji1(int i);
int wanjia();
int lpdzd();
int shibai(int l,int df);
int diji2(int i);
int diji0(int i);
int shibai(int l,int df);
int Boss(int i);
int danmo();
int bao(int i);
int jies(int x,int y);
char k;
struct fj
{
int xl=0,xh;//血量和型号
int dy,js,bsdx;
int dx;//飞机的坐标 和子弹个数 还有计数器js(用来确定发射子弹的间隔)
};fj dj[100];//对应的各个飞机(如果这个飞机血量等于0了,就让最后一个了补位,并且敌机计数变量减一)
struct baozha
{
int x,y,js,xh;
}; baozha bz[1000];
struct wj
{
int wxl=0,wsh;//玩家血量和一个子弹的伤害
int wzdx[100],wzdy[100];//子弹坐标
int wdx,wdy,zds=0,wjs,wjs2;//中心坐标和子弹数
};wj wdj;
struct zd
{
int zdx,zdy,bx,by,sh;//坐标,每次改变的坐标(速度),子弹伤害
};zd dzd[10000];
int p=0,n=1,m=1,df=0,zds=0,bss=0,bzs=0;//m是计数的,n是敌机数p是开始页面的选择(选择页面还没有做)df得分,zds子弹数
int main(int argc, char** argv) {
initgraph(1200, 790,0);
// 随机数初始化,如果需要使用随机数的话
srand(time(0));
setfont(30, 0,"宋体");
while(p<590||p>660)
{
p=0;
wdj.wxl=0;
mciSendString("open 音乐\\ks.mp3 alias mymusic", NULL, 0, NULL); // 打开音乐
mciSendString("play mymusic repeat", NULL, 0, NULL); //循环播放
while(p<476||p>660||(p<590&&p>546))
{
p=kaishi();
}
mciSendString(TEXT("close mymusic"),NULL,0,NULL);
cleardevice();
if(p>580) break;
mciSendString("open 音乐\\1.mp3 alias mymusic", NULL, 0, NULL); // 打开音乐
mciSendString("play mymusic repeat", NULL, 0, NULL); //循环播放
wdj.wxl=1;
for(;wdj.wxl>=0;delay_fps(60),m++,bss++)
{
cleardevice();
// PIMAGE img2 = newimage();
// getimage(img2, "C:\\c++2\\bj2.jpg");
// putimage(0, 0, img2);
// delimage(img2);
wanjia();
for(int i=0;i<n;i++)
// diji2(i);
if(dj[i].xh==1) diji1(i);
else if(dj[i].xh==2) diji2(i);
else if(dj[i].xh==0) diji0(i);
else if(dj[i].xh==4) Boss(i);
if(m>100)
m=0,dj[n++].xh=rand()%3;
if(bss==1200) dj[n++].xh=4;
for(int i=0;i<bzs;i++)
bao(i);
lpdzd();
}
}
closegraph();
return 0;
}
int bao(int i)
{
PIMAGE img1 = newimage();
if(bz[i].xh==1)
getimage(img1, "pk.jpg");
else if(bz[i].xh==2)
getimage(img1, "djb.jpg");
putimage(bz[i].x, bz[i].y, img1,SRCPAINT);
delimage(img1);
bz[i].js-=3;
if(bz[i].js<=0) bz[i]=bz[--bzs],i--;
}
int lpdzd()
{
setcolor(EGERGB(0, 0xFF, 0));
setfillcolor(EGERGB(0xff, 0, 0));
for(int i1=0;i1<zds&&wdj.wxl>0;i1++)
{
//子弹绘制
// bar(dzd[i1].zdx+5,dzd[i1].zdy+5);
fillellipsef(dzd[i1].zdx,dzd[i1].zdy,5,5);
dzd[i1].zdx+=dzd[i1].bx,dzd[i1].zdy+=dzd[i1].by;//让子弹飞(特别的,你可以让它斜着飞)
if(dzd[i1].zdx>= ege::getwidth()||dzd[i1].zdy>= ege::getwidth()||dzd[i1].zdy<=0||dzd[i1].zdx<0)//飞出屏幕了,放弃这个子弹
dzd[i1]=dzd[--zds],i1--;
//看看子弹有没有击中玩家如果击中,这个子弹放弃并且玩家减血
else if(dzd[i1].zdx>wdj.wdx&&dzd[i1].zdx<wdj.wdx+45&&dzd[i1].zdy>wdj.wdy&&dzd[i1].zdy<wdj.wdy+27)
{
bz[bzs].x=dzd[i1].zdx-30,bz[bzs].y=dzd[i1].zdy-25,bz[bzs].xh=1,bz[bzs++].js=10;
dzd[i1]=dzd[--zds],wdj.wxl-=dzd[i1].sh,i1--;
// mciSendString(TEXT("close ymusic"),NULL,0,NULL);
// mciSendString("open 音乐\\2.mp3 alias ymusic", NULL, 0, NULL); // 打开音乐
// mciSendString("play ymusic", NULL, 0, NULL); //开始音乐
}
}
if(wdj.wxl<=0)
{
mciSendString(TEXT("close mymusic"),NULL,0,NULL);
if(wdj.wxl!=-2)
wdj.wxl=-1,bz[0].x=wdj.wdx-60,bz[0].y=wdj.wdy-30,bz[0].js=60,bz[0].xh=2;
for(int i=0;i<n;i++)
dj[i].xl=0;
n=0,bss=0;
jies(bz[0].x,bz[0].y);
shibai(wdj.wxl,df);
zds=0,df=0,wdj.wjs2=0;
}
return 0;
}
int diji1(int i)
{
//颜色
//setcolor(EGERGB(0, 0xFF, 0));
//setfillcolor(EGERGB(0, 0, 0xFF));
//初始化数据
if(dj[i].xl==0)
{
dj[i].xl=100;
dj[i].dx=rand()%1100,dj[i].dy=0;
}
//飞机绘制(图片也可)
//bar(dj[i].dx,dj[i].dy,dj[i].dx+100,dj[i].dy+50);
PIMAGE img1 = newimage();
getimage(img1, "1号1.png",100,50);
putimage(dj[i].dx, dj[i].dy, img1,SRCPAINT);
delimage(img1);
//决定子弹发射速度 (20的大小)
if(dj[i].js>40)
dj[i].js=0,dzd[zds].zdx=dj[i].dx+50,dzd[zds].zdy=dj[i].dy+50,dzd[zds].bx=0,dzd[zds].by=4,dzd[zds].sh=50,zds++;
dj[i].dy++,dj[i].js++;
//看看玩家子弹有没有击中这个飞机
for(int i1=0;i1<wdj.zds;i1++)
if(wdj.wzdx[i1]>dj[i].dx&&wdj.wzdx[i1]<dj[i].dx+100&&wdj.wzdy[i1]>dj[i].dy&&wdj.wzdy[i1]<dj[i].dy+68)
{
bz[bzs].x=wdj.wzdx[i1]-30,bz[bzs].y=wdj.wzdy[i1]-25,bz[bzs].xh=1,bz[bzs++].js=10;
dj[i].xl-=wdj.wsh,wdj.wsh++,wdj.wzdx[i1]=wdj.wzdx[wdj.zds-1],wdj.wzdy[i1]=wdj.wzdy[--wdj.zds],df+=wdj.wsh/10,i1--;
// mciSendString(TEXT("close ymusic"),NULL,0,NULL);
// mciSendString("open 音乐\\2.mp3 alias ymusic", NULL, 0, NULL); // 打开音乐
// mciSendString("play ymusic", NULL, 0, NULL);
}
//看看有没有和玩家飞机撞在一起(有就减血)
if((dj[i].dx+50-wdj.wdx)*(dj[i].dx+50-wdj.wdx)+(dj[i].dy+25-wdj.wdy)*(dj[i].dy+25-wdj.wdy)<75*75)
wdj.wxl-=50,dj[i].xl-=50;
//看看这个飞机死没死(有条件可以加上爆炸音效和图片)
if(dj[i].xl<=0) bz[bzs].x=dj[i].dx,bz[bzs].y=dj[i].dy,bz[bzs].xh=2,bz[bzs++].js=60,wdj.wjs2++;
if(dj[i].dy>=1200||dj[i].dx>=ege::getwidth()||dj[i].xl<=0)
dj[i]=dj[--n],dj[n].xl=0,i--;
return 0;
}
int diji2(int i)
{
//颜色
//setcolor(EGERGB(0, 0xFF, 0));
//setfillcolor(EGERGB(0, 0, 0xFF));
//初始化数据
if(dj[i].xl==0)
{
dj[i].xl=50;
dj[i].dx=rand()%1100,dj[i].dy=0;
}
//飞机绘制(图片也可)
//bar(dj[i].dx,dj[i].dy,dj[i].dx+100,dj[i].dy+50);
PIMAGE img1 = newimage();
getimage(img1, "2号.jpg");
putimage(dj[i].dx, dj[i].dy, img1,SRCPAINT);
delimage(img1);
//决定子弹发射速度 (20的大小)
if(dj[i].js>20)
dj[i].js=0,dzd[zds].zdx=dj[i].dx+50,dzd[zds].zdy=dj[i].dy+50,dzd[zds].bx=0,dzd[zds].by=6,dzd[zds].sh=30,zds++;
dj[i].dy+=2,dj[i].js++;
//看看玩家子弹有没有击中这个飞机
for(int i1=0;i1<wdj.zds;i1++)
if(wdj.wzdx[i1]>dj[i].dx&&wdj.wzdx[i1]<dj[i].dx+110&&wdj.wzdy[i1]>dj[i].dy&&wdj.wzdy[i1]<dj[i].dy+76)
{
bz[bzs].x=wdj.wzdx[i1],bz[bzs].y=wdj.wzdy[i1],bz[bzs].xh=1,bz[bzs++].js=10;
dj[i].xl-=wdj.wsh,wdj.wsh++,wdj.wzdx[i1]=wdj.wzdx[wdj.zds-1],wdj.wzdy[i1]=wdj.wzdy[--wdj.zds],df+=wdj.wsh/10,i1--;
// mciSendString(TEXT("close ymusic"),NULL,0,NULL);
// mciSendString("open 音乐\\2.mp3 alias ymusic", NULL, 0, NULL); // 打开音乐
// mciSendString("play ymusic", NULL, 0, NULL); //开始音乐
}
//看看有没有和玩家飞机撞在一起(有就减血)
if((dj[i].dx+50-wdj.wdx)*(dj[i].dx+50-wdj.wdx)+(dj[i].dy+25-wdj.wdy)*(dj[i].dy+25-wdj.wdy)<75*75)
wdj.wxl-=50,dj[i].xl-=50;
//看看这个飞机死没死(有条件可以加上爆炸音效和图片)
if(dj[i].xl<=0) bz[bzs].x=dj[i].dx-30,bz[bzs].y=dj[i].dy-25,bz[bzs].xh=2,bz[bzs++].js=60,wdj.wjs2++;
if(dj[i].dy>=1200||dj[i].dx>=ege::getwidth()||dj[i].xl<=0)
dj[i]=dj[--n],dj[n].xl=0,i--;
return 0;
}
int diji0(int i)
{
//颜色
//setcolor(EGERGB(0, 0xFF, 0));
//setfillcolor(EGERGB(0, 0, 0xFF));
//初始化数据
if(dj[i].xl==0)
{
dj[i].xl=300;
dj[i].dx=rand()%1000,dj[i].dy=0;
}
//飞机绘制(图片也可)
//bar(dj[i].dx,dj[i].dy,dj[i].dx+100,dj[i].dy+50);
PIMAGE img1 = newimage();
getimage(img1, "0号.jpg",200,100);
putimage(dj[i].dx, dj[i].dy, img1,SRCPAINT);
delimage(img1);
//决定子弹发射速度 (20的大小)
if(dj[i].js>60)
dj[i].js=0,dzd[zds].zdx=dj[i].dx+100,dzd[zds].zdy=dj[i].dy+100,dzd[zds].bx=0,dzd[zds].by=4,dzd[zds].sh=50,zds++;
if(dj[i].js%2==0) dj[i].dy++;
dj[i].js++;
//看看玩家子弹有没有击中这个飞机
for(int i1=0;i1<wdj.zds;i1++)
if(wdj.wzdx[i1]>dj[i].dx&&wdj.wzdx[i1]<dj[i].dx+197&&wdj.wzdy[i1]>dj[i].dy&&wdj.wzdy[i1]<dj[i].dy+134)
{
bz[bzs].x=wdj.wzdx[i1]-30,bz[bzs].y=wdj.wzdy[i1]-25,bz[bzs].xh=1,bz[bzs++].js=10;
dj[i].xl-=wdj.wsh,wdj.wsh++,wdj.wzdx[i1]=wdj.wzdx[wdj.zds-1],wdj.wzdy[i1]=wdj.wzdy[--wdj.zds],df+=wdj.wsh/10,i1--;
// mciSendString(TEXT("close ymusic"),NULL,0,NULL);
// mciSendString("open 音乐\\2.mp3 alias ymusic", NULL, 0, NULL); // 打开音乐
// mciSendString("play ymusic", NULL, 0, NULL);
}
//看看有没有和玩家飞机撞在一起(有就减血)
if((dj[i].dx+50-wdj.wdx)*(dj[i].dx+50-wdj.wdx)+(dj[i].dy+25-wdj.wdy)*(dj[i].dy+25-wdj.wdy)<125*125)
wdj.wxl-=50,dj[i].xl-=1;
//看看这个飞机死没死(有条件可以加上爆炸音效和图片)
if(dj[i].xl<=0) bz[bzs].x=dj[i].dx+30,bz[bzs].y=dj[i].dy+20,bz[bzs].xh=2,bz[bzs++].js=60,wdj.wjs2++;
if(dj[i].dy>=1200||dj[i].dx>=ege::getwidth()||dj[i].xl<=0)
dj[i]=dj[--n],dj[n].xl=0,i--;
return 0;
}
int wanjia()
{
setcolor(EGERGB(0, 0,0x8F));
if(wdj.wxl==1) wdj.wdx=550,wdj.wdy=700,wdj.zds=0,wdj.wsh=25,wdj.wjs=0,wdj.wxl=500;
xyprintf(0, 0, "得分%d 剩余血量%d 子弹伤害%d 能量%d",df,wdj.wxl,wdj.wsh,wdj.wjs2);
if(wdj.wjs2>20) xyprintf(0, 60, "能量充足,请按p进行升级");
if (kbhit())//如果有按键进入
{
k = getch();
if(k=='a'&&wdj.wdx>0) wdj.wdx-=6;
else if(k=='d'&&wdj.wdx<ege::getwidth()) wdj.wdx+=6;
else if(k=='w'&&wdj.wdx>0) wdj.wdy-=6;
else if(k=='s'&&wdj.wdx<ege::getwidth()) wdj.wdy+=6;
else if(k=='p'&&wdj.wjs2>20) wdj.wjs2-=20,wdj.wxl+=300,wdj.wsh+=1000;
}
setcolor(EGERGB(0, 0xFF, 0xFF));
setfillcolor(EGERGB(0, 0xFF, 0xFF));//颜色
PIMAGE img1 = newimage();
getimage(img1, "wj1.jpg");
putimage(wdj.wdx,wdj.wdy, img1);
delimage(img1);
//bar(wdj.wdx-25,wdj.wdy-12,wdj.wdx+25,wdj.wdy+12);//玩家飞机绘制
for(int i1=0;i1<wdj.zds;i1++)
{
//PIMAGE img1 = newimage();
//getimage(img1, "C:\\c++2\\wjdd.jpg");
//putimage(wdj.wzdx[i1],wdj.wzdy[i1]--, img1);
//delimage(img1);
fillellipsef(wdj.wzdx[i1],wdj.wzdy[i1]--,5,5); //玩家子弹绘制
wdj.wzdy[i1]-=4;//子弹速度
if(wdj.wzdx[i1]<=0||wdj.wzdy[i1]<=0) //子弹是否出屏幕
wdj.wzdx[i1]=wdj.wzdx[wdj.zds-1],wdj.wzdy[i1]=wdj.wzdy[--wdj.zds];
}
if(wdj.wjs==10) wdj.wjs=0,wdj.wzdx[wdj.zds]=wdj.wdx+25,wdj.wzdy[wdj.zds]=wdj.wdy-20,wdj.zds++;//子弹的发射
wdj.wjs++;//计数器
return 0;
}
int Boss(int i)
{
//颜色
setcolor(EGERGB(0, 0, 0x8F));
setfillcolor(EGERGB(0xFF, 0, 0));
//初始化数据
xyprintf(0, 30, "boss剩余血量");
setcolor(EGERGB(0xFF, 0, 0));
bar(180,40,180+dj[i].xl/60,50);
rectangle(180,40,180+50000/60,50);
if(dj[i].xl==0)
{
dj[i].xl=50000;
dj[i].dx=500,dj[i].dy=100,dj[i].bsdx=1;
}
//飞机绘制(图片也可)
//bar(dj[i].dx,dj[i].dy,dj[i].dx+100,dj[i].dy+50);
//决定子弹发射速度 (20的大小)
if(dj[i].js>30)
{
dj[i].js=0,dzd[zds].zdx=dj[i].dx+75,dzd[zds].zdy=dj[i].dy+100,dzd[zds].bx=0,dzd[zds].by=6,dzd[zds].sh=30,zds++;
dj[i].js=0,dzd[zds].zdx=dj[i].dx+95,dzd[zds].zdy=dj[i].dy+100,dzd[zds].bx=0,dzd[zds].by=8,dzd[zds].sh=30,zds++;
dj[i].js=0,dzd[zds].zdx=dj[i].dx+115,dzd[zds].zdy=dj[i].dy+100,dzd[zds].bx=0,dzd[zds].by=6,dzd[zds].sh=30,zds++;
}
if(dj[i].dx>900) dj[i].bsdx=0-1;
else if(dj[i].dx<100) dj[i].bsdx=1;
dj[i].dx+=dj[i].bsdx,dj[i].js++;
if(rand()%600==0) danmo();
//看看玩家子弹有没有击中这个飞机
for(int i1=0;i1<wdj.zds;i1++)
if(wdj.wzdx[i1]>dj[i].dx&&wdj.wzdx[i1]<dj[i].dx+200&&wdj.wzdy[i1]>dj[i].dy&&wdj.wzdy[i1]<dj[i].dy+150)
{
bz[bzs].x=wdj.wzdx[i1]-30,bz[bzs].y=wdj.wzdy[i1]-25,bz[bzs].xh=1,bz[bzs++].js=10;
dj[i].xl-=wdj.wsh,wdj.wsh++,wdj.wzdx[i1]=wdj.wzdx[wdj.zds-1],wdj.wzdy[i1]=wdj.wzdy[--wdj.zds],df+=wdj.wsh/10,i1--;
// mciSendString(TEXT("close ymusic"),NULL,0,NULL);
// mciSendString("open 音乐\\2.mp3 alias ymusic", NULL, 0, NULL); // 打开音乐
// mciSendString("play ymusic", NULL, 0, NULL);
}
//看看有没有和玩家飞机撞在一起(有就减血)
if((dj[i].dx+100-wdj.wdx)*(dj[i].dx+100-wdj.wdx)+(dj[i].dy+50-wdj.wdy)*(dj[i].dy+50-wdj.wdy)<125*125)
wdj.wxl-=50,dj[i].xl-=50;
//看看这个飞机死没死(有条件可以加上爆炸音效和图片)
if(dj[i].xl<=0)
{
wdj.wxl=-2,bz[0].x=dj[i].dx,bz[0].y=dj[i].dy,bz[0].js=60,bz[0].xh=2;
}
else
{
PIMAGE img1 = newimage();
getimage(img1, "bss.jpg");
putimage(dj[i].dx, dj[i].dy, img1,SRCPAINT);
delimage(img1);
}
return 0;
}
int danmo()
{
int x=0,y=100;
while(x<1195)
{
if(x<600)
dzd[zds].zdx=x,dzd[zds].zdy=y,dzd[zds].bx=0,dzd[zds].by=4,dzd[zds].sh=20,zds++,x+=30,y+=8;
else
dzd[zds].zdx=x,dzd[zds].zdy=y,dzd[zds].bx=0,dzd[zds].by=4,dzd[zds].sh=20,zds++,x+=30,y-=8;
}
}
int jies(int x,int y)
{
for(int i=0;i<bz[0].js;delay_fps(60))
{
PIMAGE img1 = newimage();
getimage(img1, "jies.jpg");
putimage(x,y, img1);
delimage(img1);
i++;
}
}
主函数
#include <iostream>
#include<stdio.h>
#include <graphics.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <mmsystem.h>
#pragma comment (lib,"winmm.lib")
int kaishi(){
PIMAGE img1 = newimage();
getimage(img1, "C:\\c++2\\ks.jpg");
putimage(0, 0, img1);
delimage(img1);
mouse_msg msg = {0};
//获取鼠标消息,这个函数会等待,等待到有消息为止
//类似地,有和kbhit功能相近的函数MouseHit,用于检测有没有鼠标消息
while (msg.is_left()==0)
{
msg = getmouse();
}
return msg.y;
}
开始页面
#include <iostream>
#include<stdio.h>
#include <graphics.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <mmsystem.h>
#pragma comment (lib,"winmm.lib")
int shibai(int l,int df)
{
cleardevice();
PIMAGE img1 = newimage();
if(l==-1)
getimage(img1, "C:\\c++2\\shibai1.jpg");
else if(l==-2)
{
getimage(img1, "C:\\c++2\\shengli.jpg");
}
xyprintf(0, 0, "得分%d ",df);
putimage(0, 0, img1,SRCPAINT);
delimage(img1);
mouse_msg msg = {0};
while (msg.is_right()==0)
{
msg = getmouse();
}
cleardevice();
return 0;
}
结束页面
结语
学习c语言时兴趣所做,水平极为有限,使用了easyx图形库和的dev c++编译器(dev需要调连接器),仅仅只是希望其作为自己学习路上的一个纪念,参考价值极为有限。
|