📄 class.cpp
字号:
}
}
}
// 电脑飞机爆炸
void ComputerPlane::blast()
{
if(life<=0 && explosible && blastNum<8){
glPushMatrix();
glTranslatef(x, y, 0.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,othertex[2].texID);
glBegin(GL_QUADS);
glTexCoord2f(blastNum*0.125f, 0.0f); glVertex2f(-50.0f,-50.0f);
glTexCoord2f(blastNum*0.125f+0.125f,0.0f); glVertex2f( 50.0f,-50.0f);
glTexCoord2f(blastNum*0.125f+0.125f,1.0f); glVertex2f( 50.0f, 50.0f);
glTexCoord2f(blastNum*0.125f, 1.0f); glVertex2f(-50.0f, 50.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
// 爆炸参数更新
blastTime+=timer;
if(blastTime>30){
if(blastNum==0)
FSOUND_PlaySound(FSOUND_FREE,sound_2);
blastNum++;
blastTime=0;
}
if(blastNum==8){ //爆炸完毕
// 按照爆炸了的电脑飞机种类加分
myPlane.setScore(myPlane.getScore()+rewardScore);
// 类型2的高级飞机打完后会自爆
if(kind==2){
for(int i=0;i<20;i++){
Ammunition t1(x,y-25,18*i,3,100.0f,0);
t1.setOwner(3);
ammunitions[ammunitionIndex++]=t1;
if(ammunitionIndex==MAX_AMMUNITION)
ammunitionIndex=0;
}
}
// 飞机爆炸后留下物品
leftaward();
}
}
}
// 电脑飞机爆炸后留下物品
void ComputerPlane::leftaward()
{
int temp=rand()%10;
if(temp==0){ // 高级子弹补给
Award t1(x,y,1,100,0);
awards[awardIndex++]=t1;
if(awardIndex==MAX_AWARD)
awardIndex=0;
}else if(temp==1){ // 生命补给
Award t1(x,y,1,100,1);
awards[awardIndex++]=t1;
if(awardIndex==MAX_AWARD)
awardIndex=0;
}else if(temp==2){ // 炸弹补给
Award t1(x,y,1,100,2);
awards[awardIndex++]=t1;
if(awardIndex==MAX_AWARD)
awardIndex=0;
}
}
void ComputerPlane::damaged(int damage)
{
life-=damage;
if(life<=0)
explosible=true;
}
// Ammunition类的成员定义
// 其种类有四种,分别由kind决定,有0,1,2,3四种。其中:
// 0为普通子弹,1为我方高级子弹,2为敌方高级子弹,3为我方的炸弹
// 子弹初始化
Ammunition::Ammunition(float a,float b,float d,float s,int l,int k):BaseObject(a,b,l,s,d,k)
{
explosible=false;
displacement=0;
owner=0;
if(kind==0) //设置子弹的威力
explodeLevel=20;
else if(kind==1)
explodeLevel=50;
else if(kind==2)
explodeLevel=50;
else if(kind==3)
explodeLevel=100;
}
// 判断子弹是否打中飞机
void Ammunition::hitTheTarget()
{
if(owner==1){ // 玩家发出的子弹
for(int i=0; i<MAX_COMPUTER; i++){
if(distance(computers[i].getX(),computers[i].getY(),x,y)<=25 && computers[i].getLife()>0){ //子弹在敌机范围内
life=0;
explosible=true;
computers[i].damaged(explodeLevel);
return;
}
}
}else if(owner==3){ // 电脑发出的子弹
if(distance(myPlane.getX(),myPlane.getY(),x,y)<=20 && myPlane.getLife()>0){ //子弹在敌机范围内
life=0;
explosible=true;
myPlane.setLife(myPlane.getLife()-explodeLevel);
}
}
}
// 子弹移动
void Ammunition::move()
{
if(life>0){ // 子弹有效
if(kind!=3){ // 不是炸弹
if(displacement<AMMUNITION_RANGE){ // 在射程内
x=x+speed*cos(direction/180*PI)*timer/20;
y=y+speed*sin(direction/180*PI)*timer/20;
displacement+=speed*timer/20;
hitTheTarget(); // 检查是否射中
}else{
life=0;
explosible=false;
}
}else{ // 炸弹
if(displacement<BOMB_RANGE){
x=x+speed*cos(direction/180*PI)*timer/20;
y=y+speed*sin(direction/180*PI)*timer/20;
displacement+=speed*timer/20;
}else{
life=0;
explosible=true;
}
}
}
}
// 子弹爆炸
void Ammunition::blast()
{
if(life<=0 && explosible && blastNum<8){ // 子弹无效且允许爆炸且未爆炸完
glPushMatrix();
glTranslatef(x, y, 0.0f);
glEnable(GL_TEXTURE_2D);
if(kind!=3){
glBindTexture(GL_TEXTURE_2D, othertex[3].texID);
glBegin(GL_QUADS);
glTexCoord2d(blastNum*0.125f, 0.0f); glVertex2f(-10.0f,-10.0f);
glTexCoord2d(blastNum*0.125f+0.125f, 0.0f); glVertex2f( 10.0f,-10.0f);
glTexCoord2d(blastNum*0.125f+0.125f, 1.0f); glVertex2f( 10.0f, 10.0f);
glTexCoord2d(blastNum*0.125f, 1.0f); glVertex2f(-10.0f, 10.0f);
glEnd();
}else{
float temp=0.667*BOMB_BLAST_RANGE;
glBindTexture(GL_TEXTURE_2D, othertex[2].texID);
glBegin(GL_QUADS);
glTexCoord2d(blastNum*0.125f, 0.0f); glVertex2f(-temp,-temp);
glTexCoord2d(blastNum*0.125f+0.125f, 0.0f); glVertex2f( temp,-temp);
glTexCoord2d(blastNum*0.125f+0.125f, 1.0f); glVertex2f( temp, temp);
glTexCoord2d(blastNum*0.125f, 1.0f); glVertex2f(-temp, temp);
glEnd();
}
glDisable(GL_TEXTURE_2D);
glPopMatrix();
// 爆炸参数更新
blastTime+=timer;
if(blastTime>20){
blastNum++;
blastTime-=20;
}
// 炸弹爆炸清除范围内目标
if(kind==3 && blastNum==4){
// 清除范围内飞机
for(int i=0; i<MAX_COMPUTER; i++){
if(distance(computers[i].getX(),computers[i].getY(),x,y)<=BOMB_BLAST_RANGE && computers[i].getLife()>0){
computers[i].damaged(explodeLevel);
}
}
// 清除范围内子弹
for(i=0;i<MAX_AMMUNITION;i++){
if(distance(ammunitions[i].getX(),ammunitions[i].getY(),x,y)<=BOMB_BLAST_RANGE
&& ammunitions[i].getLife()>0
&& ammunitions[i].getOwner()!=owner)
{
ammunitions[i].setLife(0);
ammunitions[i].setExplosible(true);
}
}
}
}
}
// 绘制子弹
void Ammunition::draw()
{
if(life>0)
{
glPushMatrix();
glTranslatef(x, y, 0.0f);
glEnable(GL_TEXTURE_2D);
switch(kind)
{
case 0: // 绘制普通的子弹
{
glBindTexture(GL_TEXTURE_2D, ammunitiontex[0].texID);
glBegin(GL_QUADS);
glTexCoord2i(0,0);glVertex2i(-3,-3);
glTexCoord2i(1,0);glVertex2i( 3,-3);
glTexCoord2i(1,1);glVertex2i( 3, 3);
glTexCoord2i(0,1);glVertex2i(-3, 3);
glEnd();
break;
}
case 1: // 绘制玩家的高级子弹
{
glBindTexture(GL_TEXTURE_2D, ammunitiontex[1].texID);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);glVertex2i(-3,-3);
glTexCoord2i(1, 0);glVertex2i( 3,-3);
glTexCoord2i(1, 1);glVertex2i( 3, 3);
glTexCoord2i(0, 1);glVertex2i(-3, 3);
glEnd();
break;
}
case 2: // 绘制电脑的高级子弹
{
glBindTexture(GL_TEXTURE_2D, ammunitiontex[0].texID);
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);glVertex2i(-5,-5);
glTexCoord2i(1, 0);glVertex2i( 5,-5);
glTexCoord2i(1, 1);glVertex2i( 5, 5);
glTexCoord2i(0, 1);glVertex2i(-5, 5);
glEnd();
glColor3f(1.0f, 1.0f, 1.0f);
break;
}
case 3: // 绘制炸弹
{
glBindTexture(GL_TEXTURE_2D, ammunitiontex[3].texID);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);glVertex2i(-15,-15);
glTexCoord2i(1, 0);glVertex2i( 15,-15);
glTexCoord2i(1, 1);glVertex2i( 15, 15);
glTexCoord2i(0, 1);glVertex2i(-15, 15);
glEnd();
break;
}
default:
break;
}
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
}
// 物品成员定义
// 由kind决定种类,分为0 高级子弹补给,1 炸弹补给,2 生命补给
// 初始化
Award::Award(float a,float b,float s,int l,int k):BaseObject(a,b,l,s,-1,k)
{
}
// 物品绘制
void Award::draw()
{
if(life>0){
glPushMatrix();
glTranslatef(x, y, 0.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, awardtex[kind].texID);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);glVertex2f(-10.0f,-10.0f);
glTexCoord2f(1.0f,0.0f);glVertex2f( 10.0f,-10.0f);
glTexCoord2f(1.0f,1.0f);glVertex2f( 10.0f, 10.0f);
glTexCoord2f(0.0f,1.0f);glVertex2f(-10.0f, 10.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
}
// 物品移动
void Award::move()
{
if(life>0){
if(x>-240 && x<240 && y>-310 && y<300)
y-=speed*timer/20;
else
life=0;
}
}
// 判断物体是否被玩家飞机捡到
void Award::eat()
{
if(life>0 && distance(x,y, myPlane.getX(),myPlane.getY())<30)
{
switch(kind)
{
case 0: // 高级子弹补给
{
if(myPlane.getAmmunitionKind()==1){
for(int i=0;i<40;i++){
Ammunition t1(myPlane.getX(),myPlane.getY(),9*i,3,100.0f,1);
t1.setOwner(1);
ammunitions[ammunitionIndex++]=t1;
if(ammunitionIndex==MAX_AMMUNITION)
ammunitionIndex=0;
}
}
myPlane.setAmmunitionKind(1);
life=0;
break;
}
case 1: // 生命补给
{
if(myPlane.getLife()<50)
myPlane.setLife(myPlane.getLife()+50);
else if(myPlane.getLife()<100)
myPlane.setLife(100);
life=0;
break;
}
case 2: // 炸弹补给
{
myPlane.setBombNum(myPlane.getBombNum()+1);
life=0;
break;
}
default:
break;
}
FSOUND_PlaySound(FSOUND_FREE,sound_3);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -