Class.cpp
上传用户:lygtks
上传日期:2022-06-08
资源大小:874k
文件大小:16k
源码类别:

射击游戏

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include <math.h>
  3. #include <glgl.h>
  4. #include "Class.h"
  5. #include "fmod.h"
  6. #include "texture.h"
  7. //各变量定义:
  8. extern TextureTga playertex[3]; // 纹理数组,保存纹理名字
  9. extern TextureTga  computertex[3];
  10. extern TextureTga ammunitiontex[4];
  11. extern TextureTga awardtex[3];
  12. extern TextureTga othertex[4];
  13. extern PlayerPlane myPlane; // 我方飞机
  14. extern ComputerPlane computers[MAX_COMPUTER]; // 电脑飞机数组
  15. extern Ammunition ammunitions[MAX_AMMUNITION]; // 子弹数组
  16. extern Award awards[MAX_AWARD]; // 物品数组
  17. int ammunitionIndex=0;
  18. int awardIndex=0;
  19. extern FSOUND_SAMPLE *sound_1; // 发射子弹的声音指针
  20. extern FSOUND_SAMPLE *sound_2; // 射击中的声音指针
  21. extern FSOUND_SAMPLE *sound_3; // 遇到物品的声音指针
  22. extern FSOUND_SAMPLE *sound_4; // 游戏的背影音乐指针
  23. extern int myPlaneNum; // 我方目前余下的飞机数目
  24. extern bool end; // 控制游戏结束的布尔值
  25. extern DWORD endtime; // 结束动画的起始时间
  26. extern int timer; // 这一帧的运行时间
  27. // 计算点(x1,y1)与点(x2,y2)的距离
  28. float distance(float x1,float y1,float x2,float y2)
  29. {
  30. return (float)sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
  31. }
  32. // 玩家飞机类定义
  33. // 玩家飞机初始化
  34. void PlayerPlane::initPlane(float a,float b,int l,float s)
  35. {
  36. x=a;
  37. y=b;
  38. life=l;
  39. speed=s;
  40. blastNum=0;
  41. blastTime=0;
  42. bombNum=START_BOMB_NUMBER;
  43. planeTexture=0;
  44. fireTime=0;
  45. ammunitionKind=0;
  46. score=0;
  47. }
  48. PlayerPlane::PlayerPlane(float a,float b,int l,float s)
  49. {
  50. initPlane(a,b,l,s);
  51. }
  52. // 更新飞机
  53. void PlayerPlane::update()
  54. {
  55. if (life<=0&&blastNum==8){ // 玩家飞机爆炸完毕
  56. myPlaneNum--;
  57. if (myPlaneNum!=0){ // 玩家还有剩余飞机
  58. int temp=score;
  59. initPlane(0,-230,100,2);
  60. score=temp;
  61. }else{
  62. // 游戏结束(失败)
  63. endtime=GetTickCount();
  64. end=true;
  65. }
  66. }else if(score>=WIN_SCORE){ // 游戏结束(胜利)
  67. endtime=GetTickCount();
  68. end=true;
  69. }
  70. }
  71. // 发射子弹
  72. void PlayerPlane::fire()
  73. {
  74. if(life>0){
  75. fireTime+=timer;
  76. if(fireTime>100){
  77. Ammunition t1(x-12,y+1,90,4,100.0f,ammunitionKind);
  78. Ammunition t2(x+12,y+1,90,4,100.0f,ammunitionKind);
  79. t1.setOwner(1);
  80. t2.setOwner(1);
  81. ammunitions[ammunitionIndex++]=t1;
  82. if(ammunitionIndex==MAX_AMMUNITION)
  83. ammunitionIndex=0;
  84. ammunitions[ammunitionIndex++]=t2;
  85. if(ammunitionIndex==MAX_AMMUNITION)
  86. ammunitionIndex=0;
  87. fireTime-=100;
  88. }
  89. }
  90. }
  91. // 发射炸弹
  92. void PlayerPlane::fireBomb()
  93. {
  94. if(life>0 && bombNum>0){
  95. Ammunition t1(x,y+30,90,4,100.0f,3);
  96. t1.setOwner(1);
  97. ammunitions[ammunitionIndex++]=t1;
  98. if(ammunitionIndex==MAX_AMMUNITION)
  99. ammunitionIndex=0;
  100. bombNum--;
  101. }
  102. }
  103. // 检测并绘制飞机爆炸
  104. void PlayerPlane::blast()
  105. {
  106. if(life<=0){
  107. blastTime+=timer;
  108. if(blastNum<8){
  109. // 绘制爆炸图像
  110. glPushMatrix();
  111. glTranslatef(x, y, 0.0f);
  112. glEnable(GL_TEXTURE_2D);
  113. glBindTexture(GL_TEXTURE_2D, othertex[2].texID);
  114. glBegin(GL_QUADS);
  115. glTexCoord2d(blastNum*0.125f,0); glVertex2f(-50.0f,-50.0f);
  116. glTexCoord2d((blastNum+1)*0.125f,0);glVertex2f( 50.0f,-50.0f);
  117. glTexCoord2d((blastNum+1)*0.125f,1);glVertex2f( 50.0f, 50.0f);
  118. glTexCoord2d(blastNum*0.125f,1); glVertex2f(-50.0f, 50.0f);
  119. glEnd();
  120. glDisable(GL_TEXTURE_2D);
  121. glPopMatrix();
  122. // 更新爆炸相关参数
  123. if(blastTime>30){
  124. if(blastNum==0)
  125. FSOUND_PlaySound(FSOUND_FREE,sound_2);
  126. blastNum++;
  127. blastTime=0;
  128. }
  129. }
  130. }
  131. }
  132. // 检测飞机是否相撞
  133. void PlayerPlane::hitcomPlane()
  134. {
  135. if(life>0){
  136. for(int i=0; i<MAX_COMPUTER; i++){
  137. if(distance(x,y,computers[i].getX(),computers[i].getY())<30 && computers[i].getLife()>0){
  138. life-=100; // 飞机相撞,生命值减少100
  139. computers[i].setLife(0); // 对方飞机爆炸
  140. computers[i].setExplosible(true);
  141. return ;
  142. }
  143. }
  144. }
  145. }
  146. // 飞机向上移动
  147. void PlayerPlane::moveUp()
  148. {
  149. if(y<280 && life>0){
  150. y+=speed*timer/20;
  151. planeTexture=0;
  152. }
  153. }
  154. // 飞机向下移动
  155. void PlayerPlane::moveDown()
  156. {
  157. if(y>-230 && life>0){
  158. y-=speed*timer/20;
  159. planeTexture=0;
  160. }
  161. }
  162. // 飞机向右移动
  163. void PlayerPlane::moveRight()
  164. {
  165. if(life>0){
  166. planeTexture=2;
  167. if(x<230.0){
  168. x+=speed*timer/20;
  169. }
  170. }
  171. }
  172. // 飞机向左移动
  173. void PlayerPlane::moveLeft()
  174. {
  175. if(life>0){
  176. planeTexture=1;
  177. if(x>-230.0){
  178. x-=speed*timer/20;
  179. }
  180. }
  181. }
  182. // 飞机原地不动
  183. void PlayerPlane::stay()
  184. {
  185. if(life>0)
  186. planeTexture=0;
  187. }
  188. // 绘制飞机
  189. void PlayerPlane::draw()
  190. {
  191. if(life>0){
  192. glPushMatrix();
  193. glTranslatef(x, y, 0.0f);
  194. glEnable(GL_TEXTURE_2D);
  195. glBindTexture(GL_TEXTURE_2D, playertex[planeTexture].texID);
  196. // 设置飞机颜色
  197. if(life>50)
  198. glColor3f(1.0f, 1.0f, 1.0f);
  199. else
  200. glColor3f(0.7f, 0.7f, 0.7f);
  201. glBegin(GL_QUADS);
  202. glTexCoord2i(0.0f, 0.0f);glVertex2i(-20,-20);
  203. glTexCoord2i(1.0f, 0.0f);glVertex2i( 20,-20);
  204. glTexCoord2i(1.0f, 1.0f);glVertex2i( 20, 20);
  205. glTexCoord2i(0.0f, 1.0f);glVertex2i(-20, 20);
  206. glEnd();
  207. glColor3f(1.0f, 1.0f, 1.0f);
  208. glDisable(GL_TEXTURE_2D);
  209. glPopMatrix();
  210. }
  211. }
  212. // 电脑飞机类成员定义
  213. ComputerPlane::ComputerPlane(float a,float b,int l,float d,float s,int k):BaseObject(a,b,l,s,d,k)
  214. {
  215. fireTime=0;
  216. }
  217. // 电脑飞机的种类分为0,1,2三种,对于不同的种类,将会有不同的生命值和子弹属性等。
  218. void ComputerPlane::setKind(int k)
  219. {
  220. if(k==0){
  221. kind=0;
  222. speed=2;
  223. life=150;
  224. rewardScore=100;
  225. }else if(k==1){
  226. kind=1;
  227. speed=3;
  228. life=100;
  229. rewardScore=200;
  230. }else if(k==2){
  231. kind=2;
  232. speed=1;
  233. life=200;
  234. rewardScore=500;
  235. }
  236. }
  237. // 电脑飞机初始化
  238. void ComputerPlane::compinit()
  239. {
  240. x=(float)(rand()%500-250);
  241. y=(float)(rand()%100+300);
  242. direction=(float)(rand()%90-135);
  243. blastNum=0;
  244. explosible=false;
  245. // 初始化电脑飞机种类kind
  246. int temp=rand()%8;
  247. if(temp<=4)
  248. setKind(0);
  249. else if(temp==5 || temp==6)
  250. setKind(1);
  251. else if(temp==7)
  252. setKind(2);
  253. }
  254. // 更新电脑飞机
  255. void ComputerPlane::update()
  256. {
  257. if(life<=0 && blastNum==8 && explosible || life<=0 && !explosible){
  258. compinit();
  259. blastNum=0;
  260. }
  261. }
  262. // 绘制电脑飞机
  263. void ComputerPlane::draw()
  264. {
  265. if(life>0){
  266. glPushMatrix();
  267. glTranslatef(x, y, 0.0f);
  268. glEnable(GL_TEXTURE_2D);
  269. glBindTexture(GL_TEXTURE_2D, computertex[kind].texID);
  270. glBegin(GL_QUADS);
  271. glTexCoord2i(0, 0);glVertex2i(-25,-25);
  272. glTexCoord2i(1, 0);glVertex2i( 25,-25);
  273. glTexCoord2i(1, 1);glVertex2i( 25, 25);
  274. glTexCoord2i(0, 1);glVertex2i(-25, 25);
  275. glEnd();
  276. glDisable(GL_TEXTURE_2D);
  277. glPopMatrix();
  278. }
  279. }
  280. // 电脑飞机移动
  281. void ComputerPlane::move()
  282. {
  283. if(x>-275 && x<275 && y>-325){
  284. x=x+speed*cos(direction/180*PI)*timer/20;
  285. y=y+speed*sin(direction/180*PI)*timer/20;
  286. }else{
  287. life=0;
  288. explosible=false;
  289. }
  290. }
  291. // 电脑飞机移动以及子弹发射
  292. void ComputerPlane::fire()
  293. {
  294. if(life>0){
  295. fireTime+=timer;
  296. // 射击速度随机
  297. if((fireTime>1000+rand()%5000) && y>-100){
  298. if(kind==0){
  299. Ammunition t1(x,y-25,-90,3,100.0f,0);
  300. t1.setOwner(3);
  301. ammunitions[ammunitionIndex++]=t1;
  302. if(ammunitionIndex==MAX_AMMUNITION)
  303. ammunitionIndex=0;
  304. fireTime=0;
  305. }else if(kind==1){
  306. int temp=atan((myPlane.getY()-(y-25))/(myPlane.getX()-x))/PI*180;
  307. if(myPlane.getX()<x) // 若飞机在子弹的左边,则加180度
  308. temp+=180;
  309. Ammunition t1(x,y-25,temp,3,100.0f,0);
  310. t1.setOwner(3);
  311. ammunitions[ammunitionIndex++]=t1;
  312. if(ammunitionIndex==MAX_AMMUNITION)
  313. ammunitionIndex=0;
  314. fireTime=0;
  315. }else if(kind==2){
  316. Ammunition t1(x,y-25,-90,3,100.0f,2);
  317. t1.setOwner(3);
  318. ammunitions[ammunitionIndex++]=t1;
  319. if(ammunitionIndex==MAX_AMMUNITION)
  320. ammunitionIndex=0;
  321. fireTime=0;
  322. }
  323. }
  324. }
  325. }
  326. // 电脑飞机爆炸
  327. void ComputerPlane::blast()
  328. {
  329. if(life<=0 && explosible && blastNum<8){
  330. glPushMatrix();
  331. glTranslatef(x, y, 0.0f);
  332. glEnable(GL_TEXTURE_2D);
  333. glBindTexture(GL_TEXTURE_2D,othertex[2].texID);
  334. glBegin(GL_QUADS);
  335. glTexCoord2f(blastNum*0.125f, 0.0f); glVertex2f(-50.0f,-50.0f);
  336. glTexCoord2f(blastNum*0.125f+0.125f,0.0f); glVertex2f( 50.0f,-50.0f);
  337. glTexCoord2f(blastNum*0.125f+0.125f,1.0f); glVertex2f( 50.0f, 50.0f);
  338. glTexCoord2f(blastNum*0.125f, 1.0f); glVertex2f(-50.0f, 50.0f);
  339. glEnd();
  340. glDisable(GL_TEXTURE_2D);
  341. glPopMatrix();
  342. // 爆炸参数更新
  343. blastTime+=timer;
  344. if(blastTime>30){
  345. if(blastNum==0)
  346. FSOUND_PlaySound(FSOUND_FREE,sound_2);
  347. blastNum++;
  348. blastTime=0;
  349. }
  350. if(blastNum==8){ //爆炸完毕
  351. // 按照爆炸了的电脑飞机种类加分
  352. myPlane.setScore(myPlane.getScore()+rewardScore);
  353. // 类型2的高级飞机打完后会自爆
  354. if(kind==2){
  355. for(int i=0;i<20;i++){
  356. Ammunition t1(x,y-25,18*i,3,100.0f,0);
  357. t1.setOwner(3);
  358. ammunitions[ammunitionIndex++]=t1;
  359. if(ammunitionIndex==MAX_AMMUNITION)
  360. ammunitionIndex=0;
  361. }
  362. }
  363. // 飞机爆炸后留下物品
  364. leftaward();
  365. }
  366. }
  367. }
  368. // 电脑飞机爆炸后留下物品
  369. void ComputerPlane::leftaward()
  370. {
  371. int temp=rand()%10;
  372. if(temp==0){ // 高级子弹补给
  373. Award t1(x,y,1,100,0);
  374. awards[awardIndex++]=t1;
  375. if(awardIndex==MAX_AWARD)
  376. awardIndex=0;
  377. }else if(temp==1){ // 生命补给
  378. Award t1(x,y,1,100,1);
  379. awards[awardIndex++]=t1;
  380. if(awardIndex==MAX_AWARD)
  381. awardIndex=0;
  382. }else if(temp==2){ // 炸弹补给
  383. Award t1(x,y,1,100,2);
  384. awards[awardIndex++]=t1;
  385. if(awardIndex==MAX_AWARD)
  386. awardIndex=0;
  387. }
  388. }
  389. void ComputerPlane::damaged(int damage)
  390. {
  391. life-=damage;
  392. if(life<=0)
  393. explosible=true;
  394. }
  395. // Ammunition类的成员定义
  396. // 其种类有四种,分别由kind决定,有0,1,2,3四种。其中:
  397. // 0为普通子弹,1为我方高级子弹,2为敌方高级子弹,3为我方的炸弹
  398. // 子弹初始化
  399. Ammunition::Ammunition(float a,float b,float d,float s,int l,int k):BaseObject(a,b,l,s,d,k)
  400. {
  401. explosible=false;
  402. displacement=0;
  403. owner=0;
  404. if(kind==0) //设置子弹的威力
  405. explodeLevel=20;
  406. else if(kind==1)
  407. explodeLevel=50;
  408. else if(kind==2)
  409. explodeLevel=50;
  410. else if(kind==3)
  411. explodeLevel=100;
  412. }
  413. // 判断子弹是否打中飞机
  414. void Ammunition::hitTheTarget()
  415. {
  416. if(owner==1){ // 玩家发出的子弹
  417. for(int i=0; i<MAX_COMPUTER; i++){
  418. if(distance(computers[i].getX(),computers[i].getY(),x,y)<=25 && computers[i].getLife()>0){ //子弹在敌机范围内
  419. life=0;
  420. explosible=true;
  421. computers[i].damaged(explodeLevel);
  422. return;
  423. }
  424. }
  425. }else if(owner==3){ // 电脑发出的子弹
  426. if(distance(myPlane.getX(),myPlane.getY(),x,y)<=20 && myPlane.getLife()>0){ //子弹在敌机范围内
  427. life=0;
  428. explosible=true;
  429. myPlane.setLife(myPlane.getLife()-explodeLevel);
  430. }
  431. }
  432. }
  433. // 子弹移动
  434. void Ammunition::move()
  435. {
  436. if(life>0){ // 子弹有效
  437. if(kind!=3){ // 不是炸弹
  438. if(displacement<AMMUNITION_RANGE){ // 在射程内
  439. x=x+speed*cos(direction/180*PI)*timer/20;
  440. y=y+speed*sin(direction/180*PI)*timer/20;
  441. displacement+=speed*timer/20;
  442. hitTheTarget(); // 检查是否射中
  443. }else{
  444. life=0;
  445. explosible=false;
  446. }
  447. }else{ // 炸弹
  448. if(displacement<BOMB_RANGE){
  449. x=x+speed*cos(direction/180*PI)*timer/20;
  450. y=y+speed*sin(direction/180*PI)*timer/20;
  451. displacement+=speed*timer/20;
  452. }else{
  453. life=0;
  454. explosible=true;
  455. }
  456. }
  457. }
  458. }
  459. // 子弹爆炸
  460. void Ammunition::blast()
  461. {
  462. if(life<=0 && explosible && blastNum<8){ // 子弹无效且允许爆炸且未爆炸完
  463. glPushMatrix();
  464. glTranslatef(x, y, 0.0f);
  465. glEnable(GL_TEXTURE_2D);
  466. if(kind!=3){
  467. glBindTexture(GL_TEXTURE_2D, othertex[3].texID);
  468. glBegin(GL_QUADS);
  469. glTexCoord2d(blastNum*0.125f, 0.0f); glVertex2f(-10.0f,-10.0f);
  470. glTexCoord2d(blastNum*0.125f+0.125f, 0.0f); glVertex2f( 10.0f,-10.0f);
  471. glTexCoord2d(blastNum*0.125f+0.125f, 1.0f); glVertex2f( 10.0f, 10.0f);
  472. glTexCoord2d(blastNum*0.125f, 1.0f); glVertex2f(-10.0f, 10.0f);
  473. glEnd();
  474. }else{
  475. float temp=0.667*BOMB_BLAST_RANGE;
  476. glBindTexture(GL_TEXTURE_2D, othertex[2].texID);
  477. glBegin(GL_QUADS);
  478. glTexCoord2d(blastNum*0.125f, 0.0f); glVertex2f(-temp,-temp);
  479. glTexCoord2d(blastNum*0.125f+0.125f, 0.0f); glVertex2f( temp,-temp);
  480. glTexCoord2d(blastNum*0.125f+0.125f, 1.0f); glVertex2f( temp, temp);
  481. glTexCoord2d(blastNum*0.125f, 1.0f); glVertex2f(-temp, temp);
  482. glEnd();
  483. }
  484. glDisable(GL_TEXTURE_2D);
  485. glPopMatrix();
  486. // 爆炸参数更新
  487.   blastTime+=timer;
  488. if(blastTime>20){
  489. blastNum++;
  490. blastTime-=20;
  491. }
  492. // 炸弹爆炸清除范围内目标
  493. if(kind==3 && blastNum==4){
  494. // 清除范围内飞机
  495. for(int i=0; i<MAX_COMPUTER; i++){
  496. if(distance(computers[i].getX(),computers[i].getY(),x,y)<=BOMB_BLAST_RANGE && computers[i].getLife()>0){
  497. computers[i].damaged(explodeLevel);
  498. }
  499. }
  500. // 清除范围内子弹
  501. for(i=0;i<MAX_AMMUNITION;i++){
  502. if(distance(ammunitions[i].getX(),ammunitions[i].getY(),x,y)<=BOMB_BLAST_RANGE
  503. && ammunitions[i].getLife()>0
  504. && ammunitions[i].getOwner()!=owner)
  505. {
  506. ammunitions[i].setLife(0);
  507. ammunitions[i].setExplosible(true);
  508. }
  509. }
  510. }
  511. }
  512. }
  513. // 绘制子弹
  514. void Ammunition::draw()
  515. {
  516. if(life>0)
  517. {
  518. glPushMatrix();
  519. glTranslatef(x, y, 0.0f);
  520. glEnable(GL_TEXTURE_2D);
  521. switch(kind)
  522. {
  523. case 0: // 绘制普通的子弹
  524. {
  525. glBindTexture(GL_TEXTURE_2D, ammunitiontex[0].texID);
  526. glBegin(GL_QUADS);
  527. glTexCoord2i(0,0);glVertex2i(-3,-3);
  528. glTexCoord2i(1,0);glVertex2i( 3,-3);
  529. glTexCoord2i(1,1);glVertex2i( 3, 3);
  530. glTexCoord2i(0,1);glVertex2i(-3, 3);
  531. glEnd();
  532. break;
  533. }
  534. case 1: // 绘制玩家的高级子弹
  535. {
  536. glBindTexture(GL_TEXTURE_2D, ammunitiontex[1].texID);
  537. glBegin(GL_QUADS);
  538. glTexCoord2i(0, 0);glVertex2i(-3,-3);
  539. glTexCoord2i(1, 0);glVertex2i( 3,-3);
  540. glTexCoord2i(1, 1);glVertex2i( 3, 3);
  541. glTexCoord2i(0, 1);glVertex2i(-3, 3);
  542. glEnd();
  543. break;
  544. }
  545. case 2: // 绘制电脑的高级子弹
  546. {
  547. glBindTexture(GL_TEXTURE_2D, ammunitiontex[0].texID);
  548. glColor3f(0.0f, 1.0f, 1.0f);
  549. glBegin(GL_QUADS);
  550. glTexCoord2i(0, 0);glVertex2i(-5,-5);
  551. glTexCoord2i(1, 0);glVertex2i( 5,-5);
  552. glTexCoord2i(1, 1);glVertex2i( 5, 5);
  553. glTexCoord2i(0, 1);glVertex2i(-5, 5);
  554. glEnd();
  555. glColor3f(1.0f, 1.0f, 1.0f);
  556. break;
  557. }
  558. case 3: // 绘制炸弹
  559. {
  560. glBindTexture(GL_TEXTURE_2D, ammunitiontex[3].texID);
  561. glBegin(GL_QUADS);
  562. glTexCoord2i(0, 0);glVertex2i(-15,-15);
  563. glTexCoord2i(1, 0);glVertex2i( 15,-15);
  564. glTexCoord2i(1, 1);glVertex2i( 15, 15);
  565. glTexCoord2i(0, 1);glVertex2i(-15, 15);
  566. glEnd();
  567. break;
  568. }
  569. default:
  570. break;
  571. }
  572. glDisable(GL_TEXTURE_2D);
  573. glPopMatrix();
  574. }
  575. }
  576. // 物品成员定义
  577. // 由kind决定种类,分为0 高级子弹补给,1 炸弹补给,2 生命补给
  578. // 初始化
  579. Award::Award(float a,float b,float s,int l,int k):BaseObject(a,b,l,s,-1,k)
  580. {
  581. }
  582. // 物品绘制
  583. void Award::draw()
  584. {
  585. if(life>0){
  586. glPushMatrix();
  587. glTranslatef(x, y, 0.0f);
  588. glEnable(GL_TEXTURE_2D);
  589. glBindTexture(GL_TEXTURE_2D, awardtex[kind].texID);
  590. glBegin(GL_QUADS);
  591. glTexCoord2f(0.0f,0.0f);glVertex2f(-10.0f,-10.0f);
  592. glTexCoord2f(1.0f,0.0f);glVertex2f( 10.0f,-10.0f);
  593. glTexCoord2f(1.0f,1.0f);glVertex2f( 10.0f, 10.0f);
  594. glTexCoord2f(0.0f,1.0f);glVertex2f(-10.0f, 10.0f);
  595. glEnd();
  596. glDisable(GL_TEXTURE_2D);
  597. glPopMatrix();
  598. }
  599. }
  600. // 物品移动
  601. void Award::move()
  602. {
  603. if(life>0){
  604. if(x>-240 && x<240 && y>-310 && y<300)
  605. y-=speed*timer/20;
  606. else
  607. life=0;
  608. }
  609. }
  610. // 判断物体是否被玩家飞机捡到
  611. void Award::eat()
  612. {
  613. if(life>0 && distance(x,y, myPlane.getX(),myPlane.getY())<30)
  614. {
  615. switch(kind)
  616. {
  617. case 0: // 高级子弹补给
  618. {
  619. if(myPlane.getAmmunitionKind()==1){
  620. for(int i=0;i<40;i++){
  621. Ammunition t1(myPlane.getX(),myPlane.getY(),9*i,3,100.0f,1);
  622. t1.setOwner(1);
  623. ammunitions[ammunitionIndex++]=t1;
  624. if(ammunitionIndex==MAX_AMMUNITION)
  625. ammunitionIndex=0;
  626. }
  627. }
  628. myPlane.setAmmunitionKind(1);
  629. life=0;
  630. break;
  631. }
  632. case 1: // 生命补给
  633. {
  634. if(myPlane.getLife()<50)
  635. myPlane.setLife(myPlane.getLife()+50);
  636. else if(myPlane.getLife()<100)
  637. myPlane.setLife(100);
  638. life=0;
  639. break;
  640. }
  641. case 2: // 炸弹补给
  642. {
  643. myPlane.setBombNum(myPlane.getBombNum()+1);
  644. life=0;
  645. break;
  646. }
  647. default:
  648. break;
  649. }
  650. FSOUND_PlaySound(FSOUND_FREE,sound_3);
  651. }
  652. }