Advertisement
Guest User

Untitled

a guest
Jul 14th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 41.99 KB | None | 0 0
  1. /*  Project Name:       STAR WARS
  2.     Type:               Space War Game
  3.     Platform:           C++ and iGraphics
  4.     Used Softwares:     Microsoft Visual C++ 2006, Sublime Text 2
  5.  
  6.     Group name: BFS
  7.     Members:    1. Md. Rajuan Islam             ID: 13.02.04.060
  8.                 2. Md. Fahim Mohiuddin          ID: 13.02.04.062
  9.                 3. Ebne Faiz Al-Amin            ID: 13.02.04.063
  10.  
  11.                 Year:       1st
  12.                 Semester:   2nd
  13.                 Department of Computer Science and Engineering
  14.                 Ahsanullah University of Science and Technology
  15.  
  16.     Objective:
  17.         To develop a 2D game for the course mentioned below.
  18.         Course title:   CSE 1200
  19.         Course name:    Software Development - 1
  20.  
  21.     " bismillahir rahmanir rahim "
  22. */
  23.  
  24. // C headers
  25. # include "iGraphics.h"
  26.  
  27. // C++ STL Classes
  28. # include <iostream>
  29. # include <vector>
  30. using namespace std;
  31.  
  32. // timer count
  33. int sec;
  34. int player_down;
  35. int intro_sec;
  36. int load_sec;
  37.  
  38. // timer indices
  39. int back_timer, fire_timer, sec_timer, enemy_timer;
  40.  
  41. // variables
  42. int score;
  43. int life;
  44. bool game_pause;
  45. int high_score;
  46. char high_name[100];
  47. char name[100];
  48.  
  49. // Macro List
  50. # define GAME_X 800
  51. # define GAME_Y 700
  52. # define MAIN_X 1000
  53. # define MAIN_Y 700
  54.  
  55. // Application levels
  56. # define INTRODUCTION 1
  57. # define MAIN_MENU 2
  58. # define LOAD_GAME 3
  59. # define GAME_PLAY 4
  60. # define NEW_HIGH_SCORE 5
  61. # define HIGH_SCORE 6
  62. # define CREDIT 7
  63. # define HELP 8
  64. int LEVEL;
  65.  
  66. // function prototypes which are needed
  67. void initializeGame();
  68.  
  69. // common structures -------------------------------------------------------------------------------------------------------
  70. struct bullet
  71. {
  72.     int x,y;
  73.     bullet( int u, int v )
  74.     {
  75.         x = u, y = v;
  76.     }
  77. };
  78. vector <bullet> plf,pmf,prf; // bullets for the player
  79. vector <bullet> ef;          // bullets for the enemies
  80.  
  81. // foods --------------------------------------------------------------
  82. struct food
  83. {
  84.     int x, y, value;
  85.  
  86.     food( int u, int v, int w )
  87.     {
  88.         x = u;
  89.         y = v;
  90.         value = w;
  91.     }
  92.  
  93.     void draw()
  94.     {
  95.         if( value == 1 )
  96.         {
  97.             iShowBMP( x-20, y-20, "img/life40.bmp" );
  98.         }
  99.         else if( value == 2 )
  100.         {
  101.             iShowBMP( x-20, y-20, "img/pow40.bmp" );
  102.         }
  103.     }
  104.  
  105.     bool destroy( int bx, int by )
  106.     {
  107.         int x1,x2,y1,y2;
  108.  
  109.         x1 = x-20;
  110.         y1 = y-20;
  111.         x2 = x+20;
  112.         y2 = y+20;
  113.  
  114.         if( bx>=x1 && bx<=x2 && by>=y1 && by<=y2 ) return true;
  115.         return false;
  116.     }
  117. } ;
  118. vector <food> foods;
  119.  
  120. // demo variables ----------------------------------------------------------------------------------------------------------
  121. int point_x = 0, point_y = 0;
  122. int point_x_2 = 0, point_y_2 = 0;
  123. void point_show()
  124. {
  125.     char str[100];
  126.     iSetColor(255,255,255);
  127.     sprintf( str, "%d", point_x );
  128.     iText( 820, 350, str, GLUT_BITMAP_HELVETICA_18 );
  129.     sprintf( str, "%d", point_y );
  130.     iText( 900, 350, str, GLUT_BITMAP_HELVETICA_18 );
  131.  
  132.     sprintf( str, "%d", point_x_2 );
  133.     iText( 820, 300, str, GLUT_BITMAP_HELVETICA_18 );
  134.     sprintf( str, "%d", point_y_2 );
  135.     iText( 900, 300, str, GLUT_BITMAP_HELVETICA_18 );
  136. }
  137.  
  138. //--------------------------------------------------------------------------------------------------------------------------
  139.  
  140. // Background construction
  141. bool first_time_background;
  142. int point_add_count;
  143.  
  144. struct point
  145. {
  146.     int x, y;
  147.  
  148.     point( int u, int v )
  149.     {
  150.         x = u, y = v;
  151.     }
  152.  
  153.     void draw()
  154.     {
  155.         iSetColor( 255, 255, 255 );
  156.         iPoint( x, y );
  157.     }
  158. };
  159. vector <point> points;
  160.  
  161. void changeBackGround()
  162. {
  163.     // variables
  164.     int i;
  165.  
  166.     // if it is for the first time
  167.     if( first_time_background )
  168.     {
  169.         first_time_background = false;
  170.         for( i=0; i<30; i++ )
  171.         {
  172.             int x = rand()%801;
  173.             int y = rand()%701;
  174.             points.push_back( point( x, y ) );
  175.         }
  176.         return;
  177.     }
  178.  
  179.     // changing point coordinates
  180.     for( i=0; i<points.size(); )
  181.     {
  182.         points[i].y -= 1;
  183.         if( points[i].y < 0 ) points.erase( points.begin()+i );
  184.         else i++;
  185.     }
  186.  
  187.     // adding new point after time gap
  188.     point_add_count++;
  189.     if( point_add_count == 25 )
  190.     {
  191.         point_add_count = 0;
  192.         int x = rand()%801;
  193.         int y = rand()%101 + 700;
  194.         points.push_back( point( x, y ) );
  195.     }
  196. }
  197.  
  198. void backGround()
  199. {
  200.     // variables
  201.     int i;
  202.  
  203.     // background point
  204.     for( i=0; i<points.size(); i++ )
  205.     {
  206.         points[i].draw();
  207.     }
  208.  
  209.     // demo purpose margin line on the right side
  210.     //iLine( GAME_X, 0, GAME_X, GAME_Y );
  211.     // demo purpose time count on the right side
  212.     //char str[100]; sprintf( str, "%d\n", sec );
  213.     //iText( GAME_X+10, 10, str, GLUT_BITMAP_HELVETICA_18 );
  214.  
  215.     // demo purpose point click showing
  216. }
  217.  
  218. void scoring()
  219. {
  220.     // score table background
  221.     /*iSetColor(0,0,0);
  222.     iFilledRectangle( GAME_X, 0, 200, 700 );
  223.     iSetColor(255,255,255);
  224.     iLine( GAME_X, 0, GAME_X, GAME_Y );*/
  225.     iShowBMP( GAME_X, 0, "img/score_board.bmp" );
  226.  
  227.     char str[100];
  228.  
  229.     // score print
  230.     iSetColor( 255, 255, 255 );
  231.     iText( 830, 650, "SCORE", GLUT_BITMAP_TIMES_ROMAN_24 );
  232.     sprintf( str, "%d", score );
  233.     iText( 830, 600, str, GLUT_BITMAP_TIMES_ROMAN_24 );
  234.  
  235.     // health print
  236.     iText( 830, 550, "HEALTH", GLUT_BITMAP_TIMES_ROMAN_24 );
  237.     sprintf( str, "%d", life );
  238.     iText( 830, 500, str, GLUT_BITMAP_TIMES_ROMAN_24 );
  239. }
  240.  
  241. // Enemy Aircrafts --------------------------------------------------------------------------------------------------------
  242. // The basic enemy ---------------------------------------------------------|
  243. struct basic
  244. {
  245.     int x, y;
  246.  
  247.     basic( int u, int v )
  248.     {
  249.         x = u;
  250.         y = v;
  251.     }
  252.  
  253.     void draw()
  254.     {
  255.         // base circle
  256.         iSetColor( 49, 89, 134 );
  257.         iFilledCircle( x, y, 56 );
  258.  
  259.         // body line circle
  260.         iSetColor( 0, 0, 0 );
  261.         iCircle( x, y, 37 );
  262.         iCircle( x, y, 32 );
  263.  
  264.         // backline straight line
  265.         iLine( x, y+37, x, y+56 );
  266.  
  267.         // front circles
  268.         iFilledCircle( x, y-45, 5 );
  269.         iFilledCircle( x+19, y-43, 5 );
  270.         iFilledCircle( x-19, y-43, 5 );
  271.  
  272.         // side extensions
  273.         iSetColor( 151, 196, 217 );
  274.         iFilledRectangle( x-43, y-42, 3, 42 );
  275.         iFilledRectangle( x+40, y-42, 3, 42 );
  276.  
  277.         // front panel window
  278.         iSetColor( 100, 100, 100 );
  279.         iFilledRectangle( x-24, y-18, 48, 10 );
  280.         iSetColor( 0, 0, 0 );
  281.         iRectangle( x-24, y-18, 48, 10 );
  282.     }
  283.  
  284.     bool destroy( int bx, int by )
  285.     {
  286.         if( sqrt( pow( x-bx, 2.0 ) + pow( y-by, 2.0 ) ) < 56 ) return true;
  287.         return false;
  288.     }
  289.  
  290.     void deadDraw()
  291.     {
  292.         iShowBMP( x-40, y-40, "img/basic_enemy_dead.bmp" );
  293.     }
  294. } ;
  295. vector <basic> basics;
  296.  
  297. // the middle enemy ---------------------------------------------------
  298. struct middle
  299. {
  300.     int x, y;
  301.  
  302.     middle( int u, int v )
  303.     {
  304.         x = u;
  305.         y = v;
  306.     }
  307.  
  308.     bool destroy( int mx, int my )
  309.     {
  310.         int x1=x-46, x2=x+46, y1=y-25, y2=y+27;
  311.         if( mx>=x1 && mx<=x2 && my>=y1 && my<=y2 ) return true;
  312.         return false;
  313.     }
  314.  
  315.     void draw()
  316.     {
  317.         // drawing front circle
  318.         iSetColor( 175, 97, 50 );
  319.         iFilledCircle( x, y, 11 );
  320.  
  321.         // drawing middle body
  322.         iSetColor( 115, 15, 15 );
  323.         iFilledRectangle( x-9, y, 18, 14 );
  324.  
  325.         // wing tips
  326.         iSetColor( 255, 255, 255 );
  327.         iLine( x-9-10, y-4, x-9-10, y-4-10 );
  328.         iLine( x+9+10, y-4, x+9+10, y-4-10 );
  329.  
  330.         // wings
  331.         {
  332.             iSetColor( 193, 32, 32 );
  333.  
  334.             // drawing left wing
  335.             {
  336.                 double wing_x[] = { x-9-36, x-9-36, x-9, x-9 };
  337.                 double wing_y[] = { y-4, y-4+10, y+18, y-4 };
  338.                 iFilledPolygon( wing_x, wing_y, 4 );
  339.             }
  340.  
  341.             // drawing right wing
  342.             {
  343.                 double wing_x[] = { x+9+36, x+9+36, x+9, x+9 };
  344.                 double wing_y[] = { y-4, y-4+10, y+18, y-4 };
  345.                 iFilledPolygon( wing_x, wing_y, 4 );
  346.             }
  347.         }
  348.  
  349.         // extension
  350.         {
  351.             iSetColor( 115, 15, 15 );
  352.  
  353.             // right extension
  354.             {
  355.                 // lower
  356.                 {
  357.                     double ex_x[] = { x+9+23, x+9+23+6, x+9+36+6, x+9+36 };
  358.                     double ex_y[] = { y-4-11-10, y-4-11-10, y-4, y-4 };
  359.                     iFilledPolygon( ex_x, ex_y, 4 );
  360.                 }
  361.                 // middle
  362.                 {
  363.                     double ex_x[] = { x+9+36, x+9+36+6, x+9+36+6, x+9+36 };
  364.                     double ex_y[] = { y-4, y-4, y-4+10, y-4+10 };
  365.                     iFilledPolygon( ex_x, ex_y, 4 );
  366.                 }
  367.                 // top
  368.                 {
  369.                     double ex_x[] = { x+9+36, x+9+36+6, x+9+23+6, x+9+23 };
  370.                     double ex_y[] = { y-4+10, y-4+10, y-4+10+11+10, y-4+10+11+10 };
  371.                     iFilledPolygon( ex_x, ex_y, 4 );
  372.                 }
  373.             }
  374.  
  375.             // left extension
  376.             {
  377.                 // lower
  378.                 {
  379.                     double ex_x[] = { x-9-23, x-9-23-6, x-9-36-6, x-9-36 };
  380.                     double ex_y[] = { y-4-11-10, y-4-11-10, y-4, y-4 };
  381.                     iFilledPolygon( ex_x, ex_y, 4 );
  382.                 }
  383.                 // middle
  384.                 {
  385.                     double ex_x[] = { x-9-36, x-9-36-6, x-9-36-6, x-9-36 };
  386.                     double ex_y[] = { y-4, y-4, y-4+10, y-4+10 };
  387.                     iFilledPolygon( ex_x, ex_y, 4 );
  388.                 }
  389.                 // top
  390.                 {
  391.                     double ex_x[] = { x-9-36, x-9-36-6, x-9-23-6, x-9-23 };
  392.                     double ex_y[] = { y-4+10, y-4+10, y-4+10+11+10, y-4+10+11+10 };
  393.                     iFilledPolygon( ex_x, ex_y, 4 );
  394.                 }
  395.             }
  396.         }
  397.     }
  398.  
  399.     void deadDraw()
  400.     {
  401.         iShowBMP( x-40, y-40, "img/basic_enemy_dead.bmp" );
  402.     }
  403. };
  404. vector <middle> middles;
  405.  
  406. // the final enemy ----------------------------------------------------
  407. struct final
  408. {
  409.     int x, y;
  410.     bool fire;
  411.  
  412.     final( int u, int v )
  413.     {
  414.         x = u;
  415.         y = v;
  416.         fire = true;
  417.     }
  418.  
  419.     bool destroy( int bx, int by )
  420.     {
  421.         int x1=x-52;
  422.         int x2=x+50;
  423.         int y1=y-80;
  424.         int y2=y+20;
  425.  
  426.         if( bx>=x1 && bx<=x2 && by>=y1 && by<=y2 ) return true;
  427.         return false;
  428.     }
  429.  
  430.     void deadDraw()
  431.     {
  432.         iShowBMP( x-40, y-40, "img/basic_enemy_dead.bmp" );
  433.     }
  434.  
  435.     void draw()
  436.     {
  437.         //skeleton
  438.         iSetColor( 140, 140, 140 );
  439.         iLine( x, y-15, x-48, y-30 );
  440.         iLine( x, y-15, x+48, y-30 );
  441.         iLine( x-50, y, x-25, y-25 );
  442.         iLine( x+50, y, x+25, y-25 );
  443.         iLine( x-15, y, x-25, y-25 );
  444.         iLine( x+15, y, x+25, y-25 );
  445.  
  446.         // wings
  447.         iSetColor( 46, 165, 105 );
  448.         iFilledRectangle( x-53, y-80, 6, 103 );
  449.         iSetColor(0,0,0);
  450.         iRectangle( x-53, y-80, 6, 103 );
  451.         iSetColor( 46, 165, 105 );
  452.         iFilledRectangle( x+47, y-80, 6, 103 );
  453.         iSetColor(0,0,0);
  454.         iRectangle( x+47, y-80, 6, 103 );
  455.         // second part
  456.         iSetColor( 46, 165, 105 );
  457.         iFilledRectangle( x-53, y-80, 25, 10 );
  458.         iFilledRectangle( x+28, y-80, 25, 10 );
  459.         iSetColor(0,0,0);
  460.         iRectangle( x-53, y-80, 25, 10 );
  461.         iRectangle( x+28, y-80, 25, 10 );
  462.  
  463.  
  464.         // middle body
  465.         iSetColor( 0, 98, 49 );
  466.         iFilledRectangle( x-50, y, 100, 20 );
  467.         iSetColor( 0, 0, 0 );
  468.         iRectangle( x-50, y, 100, 20 );
  469.         iSetColor( 64, 128, 115 );
  470.         iFilledCircle( x, y, 20 );
  471.         iSetColor(0,0,0);
  472.         iCircle( x, y, 20 );
  473.         iCircle( x, y, 17 );
  474.         iSetColor( 80, 80, 80 );
  475.         iFilledCircle( x, y-10, 5 );
  476.         iSetColor(0,0,0);
  477.         iCircle( x, y-10, 5 );
  478.     }
  479. };
  480. vector <final> finals;
  481.  
  482. // food----------------------------------------------------------------
  483.  
  484.  
  485. //--------------------------------------------------------------------------------------------------------------------------
  486. // Player Aircraft
  487. struct player
  488. {
  489.     // variables
  490.     int x, y;
  491.     bool boost, fire;
  492.     int fireLevel;
  493.  
  494.     // functions
  495.     void initialize()
  496.     {
  497.         x=200, y=200;
  498.         fire=boost=false;
  499.         fireLevel = 0;
  500.  
  501.         plf.clear();
  502.         pmf.clear();
  503.         pmf.clear();
  504.     }
  505.  
  506.     void fireBlastDraw()
  507.     {
  508.         iSetColor( 255, 255, 0 );
  509.         iFilledCircle( x, y+30+15, 10 );
  510.     }
  511.  
  512.     void fireDraw()
  513.     {
  514.         int i;
  515.         iSetColor( 255, 0, 0 );
  516.         // left
  517.         for( i=0; i<plf.size(); i++ ) iFilledCircle( plf[i].x, plf[i].y, 3 );
  518.         // middle
  519.         for( i=0; i<pmf.size(); i++ ) iFilledCircle( pmf[i].x, pmf[i].y, 3 );
  520.         // right
  521.         for( i=0; i<prf.size(); i++ ) iFilledCircle( prf[i].x, prf[i].y, 3 );
  522.     }
  523.  
  524.     void boostDraw()
  525.     {
  526.         iSetColor( 255, 0, 0 );//color: red
  527.         iFilledEllipse( x, y-15, 7, 30 );
  528.  
  529.         iSetColor( 255, 255, 0 ); // color: yellow
  530.         iFilledEllipse( x, y-15, 3, 20 );
  531.     }
  532.  
  533.     void bodyDraw()
  534.     {
  535.         // drawing front
  536.         {
  537.             // drawing the front base
  538.             iSetColor( 192, 192, 192 ); // color: silver
  539.             {
  540.                 double front_x[] = { x-10, x, x+10 };
  541.                 double front_y[] = { y+30, y+30+15, y+30 };
  542.                 iFilledPolygon( front_x, front_y, 3 );
  543.             }
  544.             // drawing the front top
  545.             iSetColor( 0, 0, 0 ); // color: black
  546.             {
  547.                 iFilledCircle( x, y+30, 7 );
  548.             }
  549.         }
  550.         /* drawing wings*/
  551.         {
  552.             // wing base
  553.             iSetColor( 192, 192, 192 ) ; // color: silver
  554.             {
  555.                 double wing_x[] = { x-15-30, x-15, x+15, x+15+30 };
  556.                 double wing_y[] = { y, y+20, y+20, y };
  557.                 iFilledPolygon( wing_x, wing_y, 4 );
  558.             }
  559.             // wing top
  560.             iSetColor( 255, 255, 0 ); // color : yellow
  561.             {
  562.                 double wing_x[] = { x-15-15, x-15, x+15, x+15+15 };
  563.                 double wing_y[] = { y+4, y+13, y+13, y+4 };
  564.                 iFilledPolygon( wing_x, wing_y, 4 );
  565.             }
  566.             // wing tip
  567.             iSetColor( 255, 255, 255 );
  568.             iLine( x-15-20, y, x-15-20, y+20 ); // left tip
  569.             iLine( x+15+20, y, x+15+20, y+20 ); // right tip
  570.         }
  571.         // drawing back wings
  572.         {
  573.             iSetColor( 192, 192, 192 ) ; // color: silver
  574.             {
  575.                 double wing_x[] = { x-15-10, x-15, x+15, x+15+10 };
  576.                 double wing_y[] = { y-15, y, y, y-15 };
  577.                 iFilledPolygon( wing_x, wing_y, 4 );
  578.             }
  579.         }
  580.         /* drawing middle */
  581.         {
  582.             // drawing the middle body
  583.             iSetColor( 128, 128, 128 ); // color: grey
  584.             iFilledRectangle( x-15, y-15, 30, 30+15 );
  585.  
  586.             iSetColor( 0, 0, 0 ); // color: black
  587.             iRectangle( x-15, y-15, 30, 30+15 );
  588.             iRectangle( x-12, y-12, 24, 30+8 );
  589.  
  590.             iSetColor( 80, 80, 80 ); // color: grey
  591.             iFilledRectangle( x-9, y-8, 18, 8 );
  592.  
  593.             iSetColor( 0, 0, 0 ); // color: black
  594.             iRectangle( x-9, y-8, 18, 8 );
  595.         }
  596.     }
  597.  
  598.     bool destroy( int bx, int by )
  599.     {
  600.         int x1, x2, y1, y2;
  601.         // first
  602.         x1 = x-15;
  603.         y1 = y+20;
  604.         x2 = x+15;
  605.         y2 = y+45;
  606.         if( bx>=x1 && bx<=x2 && by>=y1 && by<=y2 ) return true;
  607.         // second middle
  608.         x1 = x-15-20;
  609.         y1 = y;
  610.         x2 = x+15+20;
  611.         y2 = y+20;
  612.         if( bx>=x1 && bx<=x2 && by>=y1 && by<=y2 ) return true;
  613.         // third
  614.         x1 = x-15-10;
  615.         y1 = y-15;
  616.         x2 = x+15+10;
  617.         y2 = y;
  618.         if( bx>=x1 && bx<=x2 && by>=y1 && by<=y2 ) return true;
  619.  
  620.         return false;
  621.     }
  622.  
  623.     bool collide( basic e )
  624.     {
  625.         int tx, ty;
  626.         // top point
  627.         tx = x;
  628.         ty = y+45;
  629.         if( e.destroy( tx, ty )) return true;
  630.  
  631.         // front left
  632.         tx = x-15;
  633.         ty = y+30;
  634.         if( e.destroy( tx, ty )) return true;
  635.         // front right
  636.         tx = x+15;
  637.         ty = y+30;
  638.         if( e.destroy( tx, ty )) return true;
  639.  
  640.         // wing mid left
  641.         tx = x-30;
  642.         ty = y+11;
  643.         if( e.destroy( tx, ty )) return true;
  644.         // wing mid right
  645.         tx = x+30;
  646.         ty = y+11;
  647.         if( e.destroy( tx, ty )) return true;
  648.  
  649.         // wing tip left
  650.         tx = x - 45;
  651.         ty = y;
  652.         if( e.destroy( tx, ty )) return true;
  653.         // wing tip right
  654.         tx = x + 45;
  655.         ty = y;
  656.         if( e.destroy( tx, ty )) return true;
  657.  
  658.         // back wing tip left
  659.         tx = x-25;
  660.         ty = y-15;
  661.         if( e.destroy( tx, ty )) return true;
  662.         // back wing tip right
  663.         tx = x+25;
  664.         ty = y - 15;
  665.         if( e.destroy( tx, ty )) return true;
  666.  
  667.         // back mid
  668.         tx = x;
  669.         ty = y-15;
  670.         if( e.destroy( tx, ty )) return true;
  671.  
  672.         return false;
  673.     }
  674.  
  675.     bool collide( middle e )
  676.     {
  677.         int tx, ty;
  678.         // top point
  679.         tx = x;
  680.         ty = y+45;
  681.         if( e.destroy( tx, ty )) return true;
  682.  
  683.         // front left
  684.         tx = x-15;
  685.         ty = y+30;
  686.         if( e.destroy( tx, ty )) return true;
  687.         // front right
  688.         tx = x+15;
  689.         ty = y+30;
  690.         if( e.destroy( tx, ty )) return true;
  691.  
  692.         // wing mid left
  693.         tx = x-30;
  694.         ty = y+11;
  695.         if( e.destroy( tx, ty )) return true;
  696.         // wing mid right
  697.         tx = x+30;
  698.         ty = y+11;
  699.         if( e.destroy( tx, ty )) return true;
  700.  
  701.         // wing tip left
  702.         tx = x - 45;
  703.         ty = y;
  704.         if( e.destroy( tx, ty )) return true;
  705.         // wing tip right
  706.         tx = x + 45;
  707.         ty = y;
  708.         if( e.destroy( tx, ty )) return true;
  709.  
  710.         // back wing tip left
  711.         tx = x-25;
  712.         ty = y-15;
  713.         if( e.destroy( tx, ty )) return true;
  714.         // back wing tip right
  715.         tx = x+25;
  716.         ty = y - 15;
  717.         if( e.destroy( tx, ty )) return true;
  718.  
  719.         // back mid
  720.         tx = x;
  721.         ty = y-15;
  722.         if( e.destroy( tx, ty )) return true;
  723.  
  724.         return false;
  725.     }
  726.  
  727.     bool collide( final e )
  728.     {
  729.         int tx, ty;
  730.         // top point
  731.         tx = x;
  732.         ty = y+45;
  733.         if( e.destroy( tx, ty )) return true;
  734.  
  735.         // front left
  736.         tx = x-15;
  737.         ty = y+30;
  738.         if( e.destroy( tx, ty )) return true;
  739.         // front right
  740.         tx = x+15;
  741.         ty = y+30;
  742.         if( e.destroy( tx, ty )) return true;
  743.  
  744.         // wing mid left
  745.         tx = x-30;
  746.         ty = y+11;
  747.         if( e.destroy( tx, ty )) return true;
  748.         // wing mid right
  749.         tx = x+30;
  750.         ty = y+11;
  751.         if( e.destroy( tx, ty )) return true;
  752.  
  753.         // wing tip left
  754.         tx = x - 45;
  755.         ty = y;
  756.         if( e.destroy( tx, ty )) return true;
  757.         // wing tip right
  758.         tx = x + 45;
  759.         ty = y;
  760.         if( e.destroy( tx, ty )) return true;
  761.  
  762.         // back wing tip left
  763.         tx = x-25;
  764.         ty = y-15;
  765.         if( e.destroy( tx, ty )) return true;
  766.         // back wing tip right
  767.         tx = x+25;
  768.         ty = y - 15;
  769.         if( e.destroy( tx, ty )) return true;
  770.  
  771.         // back mid
  772.         tx = x;
  773.         ty = y-15;
  774.         if( e.destroy( tx, ty )) return true;
  775.  
  776.         return false;
  777.     }
  778.  
  779.     bool collide( food e )
  780.     {
  781.         int tx, ty;
  782.         // top point
  783.         tx = x;
  784.         ty = y+45;
  785.         if( e.destroy( tx, ty )) return true;
  786.  
  787.         // front left
  788.         tx = x-15;
  789.         ty = y+30;
  790.         if( e.destroy( tx, ty )) return true;
  791.         // front right
  792.         tx = x+15;
  793.         ty = y+30;
  794.         if( e.destroy( tx, ty )) return true;
  795.  
  796.         // wing mid left
  797.         tx = x-30;
  798.         ty = y+11;
  799.         if( e.destroy( tx, ty )) return true;
  800.         // wing mid right
  801.         tx = x+30;
  802.         ty = y+11;
  803.         if( e.destroy( tx, ty )) return true;
  804.  
  805.         // wing tip left
  806.         tx = x - 45;
  807.         ty = y;
  808.         if( e.destroy( tx, ty )) return true;
  809.         // wing tip right
  810.         tx = x + 45;
  811.         ty = y;
  812.         if( e.destroy( tx, ty )) return true;
  813.  
  814.         // back wing tip left
  815.         tx = x-25;
  816.         ty = y-15;
  817.         if( e.destroy( tx, ty )) return true;
  818.         // back wing tip right
  819.         tx = x+25;
  820.         ty = y - 15;
  821.         if( e.destroy( tx, ty )) return true;
  822.  
  823.         // back mid
  824.         tx = x;
  825.         ty = y-15;
  826.         if( e.destroy( tx, ty )) return true;
  827.  
  828.         return false;
  829.     }
  830.  
  831.     void draw()
  832.     {
  833.         if( fire )
  834.         {
  835.             fireBlastDraw();
  836.             fire = false;
  837.         }
  838.  
  839.         fireDraw();
  840.  
  841.         if( boost )
  842.         {
  843.             boostDraw();
  844.             boost = false;
  845.         }
  846.         bodyDraw();
  847.     }
  848.  
  849.     void deadDraw()
  850.     {
  851.         iShowBMP( x-40, y-40, "img/basic_enemy_dead.bmp" );
  852.     }
  853. } plane ;
  854.  
  855. //-------------------------------------------------------------------------------------------------------------------------
  856. void clear_all();
  857. void iDraw()
  858. {  
  859.     // GAME PLAY
  860.     if( LEVEL == GAME_PLAY )
  861.     {
  862.         // variables
  863.         int i,j;
  864.  
  865.         iClear();
  866.         //drawing the background
  867.         backGround();
  868.         //point_show();
  869.  
  870.         // basic enemies
  871.         for( i=0; i<basics.size();  )
  872.         {
  873.             bool gone = false;
  874.  
  875.             // left bullet list
  876.             for( j=0; j<plf.size(); )
  877.             {
  878.                 if( basics[i].destroy( plf[j].x, plf[j].y ) )
  879.                 {
  880.                     basics[i].deadDraw();
  881.                     basics.erase( basics.begin()+i );
  882.                     plf.erase( plf.begin() + j );
  883.                     gone = true;
  884.                     score += 5;
  885.  
  886.                     goto last;
  887.                 }
  888.                 else j++;
  889.             }
  890.             // middle bullet list
  891.             for( j=0; j<pmf.size(); )
  892.             {
  893.                 if( basics[i].destroy( pmf[j].x, pmf[j].y ) )
  894.                 {
  895.                     basics[i].deadDraw();
  896.                     basics.erase( basics.begin()+i );
  897.                     pmf.erase( pmf.begin() + j );
  898.                     gone = true;
  899.                     score += 5;
  900.  
  901.                     goto last;
  902.                 }
  903.                 else j++;
  904.             }
  905.             // right bullet list
  906.             for( j=0; j<prf.size(); )
  907.             {
  908.                 if( basics[i].destroy( prf[j].x, prf[j].y ) )
  909.                 {
  910.                     basics[i].deadDraw();
  911.                     basics.erase( basics.begin()+i );
  912.                     prf.erase( prf.begin() + j );
  913.                     gone = true;
  914.                     score += 5;
  915.  
  916.                     goto last;
  917.                 }
  918.                 else j++;
  919.             }
  920.  
  921. last:
  922.             if( !gone )
  923.             {
  924.                 basics[i].draw();
  925.                 i++;
  926.             }
  927.         }
  928.         // middle enemies
  929.         for( i=0; i<middles.size(); )
  930.         {
  931.             bool gone = false;
  932.  
  933.             // left bullet list
  934.             for( j=0; j<plf.size(); )
  935.             {
  936.                 if( middles[i].destroy( plf[j].x, plf[j].y ) )
  937.                 {
  938.                     middles[i].deadDraw();
  939.                     middles.erase( middles.begin()+i );
  940.                     plf.erase( plf.begin() + j );
  941.                     gone = true;
  942.                     score += 5;
  943.  
  944.                     goto second_last;
  945.                 }
  946.                 else j++;
  947.             }
  948.             // middle bullet list
  949.             for( j=0; j<pmf.size(); )
  950.             {
  951.                 if( middles[i].destroy( pmf[j].x, pmf[j].y ) )
  952.                 {
  953.                     middles[i].deadDraw();
  954.                     middles.erase( middles.begin()+i );
  955.                     pmf.erase( pmf.begin() + j );
  956.                     gone = true;
  957.                     score += 5;
  958.  
  959.                     goto second_last;
  960.                 }
  961.                 else j++;
  962.             }
  963.             // right bullet list
  964.             for( j=0; j<prf.size(); )
  965.             {
  966.                 if( middles[i].destroy( prf[j].x, prf[j].y ) )
  967.                 {
  968.                     middles[i].deadDraw();
  969.                     middles.erase( middles.begin()+i );
  970.                     prf.erase( prf.begin() + j );
  971.                     gone = true;
  972.                     score += 5;
  973.  
  974.                     goto second_last;
  975.                 }
  976.                 else j++;
  977.             }
  978.  
  979. second_last:
  980.             if( !gone )
  981.             {
  982.                 middles[i].draw();
  983.                 i++;
  984.             }
  985.         }
  986.         // final enemies
  987.         for( i=0; i<finals.size(); )
  988.         {
  989.             bool gone = false;
  990.  
  991.             // left bullet list
  992.             for( j=0; j<plf.size(); )
  993.             {
  994.                 if( finals[i].destroy( plf[j].x, plf[j].y ) )
  995.                 {
  996.                     finals[i].deadDraw();
  997.                     finals.erase( finals.begin()+i );
  998.                     plf.erase( plf.begin() + j );
  999.                     gone = true;
  1000.                     score += 5;
  1001.  
  1002.                     goto third_last;
  1003.                 }
  1004.                 else j++;
  1005.             }
  1006.             // middle bullet list
  1007.             for( j=0; j<pmf.size(); )
  1008.             {
  1009.                 if( finals[i].destroy( pmf[j].x, pmf[j].y ) )
  1010.                 {
  1011.                     finals[i].deadDraw();
  1012.                     finals.erase( finals.begin()+i );
  1013.                     pmf.erase( pmf.begin() + j );
  1014.                     gone = true;
  1015.                     score += 5;
  1016.  
  1017.                     goto third_last;
  1018.                 }
  1019.                 else j++;
  1020.             }
  1021.             // right bullet list
  1022.             for( j=0; j<prf.size(); )
  1023.             {
  1024.                 if( finals[i].destroy( prf[j].x, prf[j].y ) )
  1025.                 {
  1026.                     finals[i].deadDraw();
  1027.                     finals.erase( finals.begin()+i );
  1028.                     prf.erase( prf.begin() + j );
  1029.                     gone = true;
  1030.                     score += 5;
  1031.  
  1032.                     goto third_last;
  1033.                 }
  1034.                 else j++;
  1035.             }
  1036.  
  1037. third_last:
  1038.             if( !gone )
  1039.             {
  1040.                 finals[i].draw();
  1041.                 i++;
  1042.             }
  1043.         }
  1044.  
  1045.         //iSetColor(255,255,255);
  1046.         //iText( 225, 340, "PLAYER DOWN! PLAYER DOWN!", GLUT_BITMAP_TIMES_ROMAN_24 );
  1047.         //iShowBMP( 238, 252, "player_down.bmp" );
  1048.         //iText( 225, 340, "GAME OVER!!!", GLUT_BITMAP_TIMES_ROMAN_24 );
  1049.         //iShowBMP( 225, 250, "game_over.bmp" );
  1050.  
  1051.         // pause time handle between lives
  1052.         if( player_down )
  1053.         {
  1054.             if( life )
  1055.             {
  1056.                 iShowBMP( 238, 252, "img/player_down.bmp" );
  1057.             }
  1058.             else
  1059.             {
  1060.                 iShowBMP( 238, 252, "img/game_over.bmp" );
  1061.             }
  1062.  
  1063.             goto end_of_line;
  1064.         }
  1065.  
  1066.         // testing enemy bullets and drawing them
  1067.         for( i=0; i<ef.size(); )
  1068.         {
  1069.             if( plane.destroy( ef[i].x, ef[i].y ))
  1070.             {
  1071.                 plane.deadDraw();
  1072.                 ef.erase( ef.begin() + i );
  1073.  
  1074.                 // life down
  1075.                 plane.fireLevel = 0;
  1076.                 life--;
  1077.                 clear_all();
  1078.                 sec = 1;
  1079.                 player_down = 1;
  1080.                 goto end_of_line;
  1081.             }
  1082.             else
  1083.             {
  1084.                 iSetColor( 255,255,128 );
  1085.                 iFilledCircle( ef[i].x, ef[i].y, 3 );
  1086.  
  1087.                 i++;
  1088.             }
  1089.         }
  1090.         // testing collision with enemies
  1091.         // basics
  1092.         for( i=0; i<basics.size(); i++ )
  1093.         {
  1094.             if( plane.collide( basics[i] ))
  1095.             {
  1096.                 plane.deadDraw();
  1097.  
  1098.                 // life down
  1099.                 plane.fireLevel = 0;
  1100.                 life--;
  1101.                 clear_all();
  1102.                 sec = 1;
  1103.                 player_down = 1;
  1104.                 goto end_of_line;
  1105.             }
  1106.         }
  1107.         // middles
  1108.         for( i=0; i<middles.size(); i++ )
  1109.         {
  1110.             if( plane.collide( middles[i] ))
  1111.             {
  1112.                 plane.deadDraw();
  1113.  
  1114.                 // life down
  1115.                 life--;
  1116.                 clear_all();
  1117.                 sec = 1;
  1118.                 player_down = 1;
  1119.                 goto end_of_line;
  1120.             }
  1121.         }
  1122.         // finals
  1123.         for( i=0; i<finals.size(); i++ )
  1124.         {
  1125.             if( plane.collide( finals[i] ))
  1126.             {
  1127.                 plane.deadDraw();
  1128.  
  1129.                 // life down
  1130.                 life--;
  1131.                 clear_all();
  1132.                 sec = 1;
  1133.                 player_down = 1;
  1134.                 goto end_of_line;
  1135.             }
  1136.         }
  1137.  
  1138.         // testing food collisions
  1139.         for( i=0; i<foods.size(); )
  1140.         {
  1141.             if( plane.collide( foods[i] ) )
  1142.             {
  1143.                 if( foods[i].value == 1 ) life++;
  1144.                 else if( foods[i].value == 2 ) plane.fireLevel = 1;
  1145.  
  1146.                 foods.erase( foods.begin() + i );
  1147.             }
  1148.             else
  1149.             {
  1150.                 foods[i].draw();
  1151.                 i++;
  1152.             }
  1153.         }
  1154.         plane.draw();
  1155.  
  1156. end_of_line:
  1157.         scoring();
  1158.         // pause menu
  1159.         if(game_pause) iShowBMP( 238, 252, "img/pause_menu.bmp" );
  1160.         // demo debug assist
  1161.         //point_show();
  1162.     }
  1163.    
  1164.     // INTRODUCTION
  1165.     else if( LEVEL == INTRODUCTION )
  1166.     {
  1167.         if( intro_sec < 3 )
  1168.         {
  1169.             iShowBMP(0,0,"img/1.bmp");
  1170.         }
  1171.         else if( intro_sec < 5 )
  1172.         {
  1173.             iShowBMP(0,0,"img/2.bmp");
  1174.         }
  1175.         else if( intro_sec < 7 )
  1176.         {
  1177.             iShowBMP(0,0,"img/3.bmp");
  1178.         }
  1179.         else
  1180.         {
  1181.             iShowBMP(0,0,"img/4.bmp");
  1182.         }
  1183.     }
  1184.  
  1185.     // MAIN MENU
  1186.     else if( LEVEL == MAIN_MENU )
  1187.     {
  1188.         iShowBMP(0,0,"img/main_menu.bmp");
  1189.     }
  1190.  
  1191.     // HIGH SCORE
  1192.     else if( LEVEL == HIGH_SCORE )
  1193.     {
  1194.         //place your drawing codes here
  1195.         iClear();
  1196.         iShowBMP(0,0,"img/high_score.bmp");
  1197.    
  1198.         iSetColor(255,255,255);
  1199.         char str[100];
  1200.         if(high_score)
  1201.         {
  1202.             strcpy(str,"Name : ");
  1203.             strcat(str,high_name);
  1204.             iText( 116, 406, str, GLUT_BITMAP_TIMES_ROMAN_24 );
  1205.  
  1206.             sprintf(str,"Score : %d", high_score );
  1207.             iText( 116, 360, str, GLUT_BITMAP_TIMES_ROMAN_24 );
  1208.         }
  1209.         else
  1210.         {
  1211.             iText( 116, 406, "...No high score yet!...", GLUT_BITMAP_TIMES_ROMAN_24 );
  1212.         }
  1213.     }
  1214.  
  1215.     // HELP
  1216.     else if( LEVEL == HELP )
  1217.     {
  1218.         iClear();
  1219.         iShowBMP(0,0,"img/help.bmp");
  1220.     }
  1221.  
  1222.     // CREDIT
  1223.     else if( LEVEL == CREDIT )
  1224.     {
  1225.         iClear();
  1226.         iShowBMP(0,0,"img/credit.bmp");
  1227.     }
  1228.  
  1229.     // LOAD GAME
  1230.     else if( LEVEL == LOAD_GAME )
  1231.     {
  1232.         iClear();
  1233.  
  1234.         if( load_sec < 4 ) iShowBMP(0,0,"img/load_1.bmp");
  1235.         else iShowBMP(0,0,"img/load_2.bmp");
  1236.  
  1237.         iSetColor(255,255,255);
  1238.         iText(10, 137, "LOADING......", GLUT_BITMAP_TIMES_ROMAN_24 );
  1239.  
  1240.         iRectangle(10,100,980,22);
  1241.    
  1242.         iSetColor(255,0,0);
  1243.         iFilledRectangle( 14, 104, 162*load_sec , 12 );
  1244.     }
  1245.  
  1246.     // NEW HIGH SCORE
  1247.     else if( LEVEL == NEW_HIGH_SCORE )
  1248.     {
  1249.         iClear();
  1250.  
  1251.         // background
  1252.         iShowBMP(0,0,"img/new_high_score.bmp");
  1253.         iText( 123, 121, name, GLUT_BITMAP_TIMES_ROMAN_24 );
  1254.  
  1255.         // drawing the box
  1256.         iSetColor(255,255,255);
  1257.         iRectangle(112,110,385,40);
  1258.     }
  1259. }
  1260.  
  1261. void clear_all()
  1262. {
  1263.     basics.clear();
  1264.     middles.clear();
  1265.     finals.clear();
  1266.     foods.clear();
  1267.  
  1268.     plf.clear();
  1269.     prf.clear();
  1270.     pmf.clear();
  1271.  
  1272.     ef.clear();
  1273. }
  1274.  
  1275. // transitional functions
  1276. void after_game()
  1277. {
  1278.     if( score > high_score )
  1279.     {
  1280.         high_score = score;
  1281.         strcpy( name, "" );
  1282.         LEVEL = NEW_HIGH_SCORE;
  1283.     }
  1284.     else
  1285.     {
  1286.         LEVEL = MAIN_MENU;
  1287.     }
  1288. }
  1289.  
  1290. void read_data()
  1291. {
  1292.     // data reading initialization
  1293.     FILE *f;
  1294.     f = fopen("data/record.txt","r");
  1295.     fscanf( f, "%d", &high_score );
  1296.     if(high_score<0) high_score = 0;
  1297.     if(high_score)
  1298.     {
  1299.         fscanf( f, "%s", high_name );
  1300.         int i, len=strlen(high_name);
  1301.         for(i=0;i<len;i++)
  1302.         {
  1303.             if(high_name[i]=='_')
  1304.             {
  1305.                 high_name[i]=' ';
  1306.             }
  1307.         }
  1308.     }
  1309.     fclose(f);
  1310. }
  1311.  
  1312. void write_data()
  1313. {
  1314.     // data writing
  1315.     FILE *f;
  1316.     f = fopen("data/record.txt","w");
  1317.     fprintf(f,"%d\n",high_score);
  1318.     if(high_score)
  1319.     {
  1320.         int i, len=strlen(high_name);
  1321.         for(i=0;i<len;i++)
  1322.         {
  1323.             if(high_name[i]==' ')
  1324.             {
  1325.                 high_name[i]='_';
  1326.             }
  1327.         }
  1328.         fprintf(f,"%s",high_name);
  1329.     }
  1330.     fclose(f);
  1331. }
  1332.  
  1333. // controlling function -------------------------------------------------------------------------------------------------------------------------
  1334.  
  1335. void iMouseMove(int mx, int my)
  1336. {}
  1337.  
  1338. void iMouse(int button, int state, int mx, int my)
  1339. {
  1340.     /*if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  1341.     {
  1342.         // demo debug assist
  1343.         point_x = point_x_2;
  1344.         point_x_2 = mx;
  1345.         point_y = point_y_2;
  1346.         point_y_2 = my;
  1347.     }*/
  1348.     if( LEVEL == MAIN_MENU )
  1349.     {
  1350.         if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  1351.         {
  1352.             if( mx>=76 && mx<=198 && my>=336 && my<=375 )
  1353.             {
  1354.                 load_sec = 1;
  1355.                 LEVEL = LOAD_GAME;
  1356.             }
  1357.             else if( mx>=75 && mx<=312 && my>=285 && my<= 323 )
  1358.             {
  1359.                 LEVEL = HIGH_SCORE;
  1360.             }
  1361.             else if( mx>=76 && mx<=197 && my>=232 && my<=269 )
  1362.             {
  1363.                 LEVEL = HELP;
  1364.             }
  1365.             else if( mx>=76 && mx<=254 && my>=179 && my<= 217 )
  1366.             {
  1367.                 LEVEL = CREDIT;
  1368.             }
  1369.             else if( mx>= 817 && mx<=928 && my>=71 && my<=109 )
  1370.             {
  1371.                 // data writing
  1372.                 write_data();
  1373.  
  1374.                 exit(0);
  1375.             }
  1376.         }
  1377.     }
  1378.     else if( LEVEL == HIGH_SCORE )
  1379.     {
  1380.         if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  1381.         {
  1382.             if( mx>=767 && mx<=888 && my>=53 && my<=92 )
  1383.             {
  1384.                 LEVEL = MAIN_MENU;
  1385.             }
  1386.         }
  1387.     }
  1388.     else if( LEVEL == HELP )
  1389.     {
  1390.         if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  1391.         {
  1392.             if( mx>=818 && mx<=956 && my>=135 && my<=174 )
  1393.             {
  1394.                 LEVEL = MAIN_MENU;
  1395.             }
  1396.         }
  1397.     }
  1398.     else if( LEVEL == CREDIT )
  1399.     {
  1400.         if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  1401.         {
  1402.             if( mx>=126 && mx<=264 && my>=44 && my<=84 )
  1403.             {
  1404.                 LEVEL = MAIN_MENU;
  1405.             }
  1406.         }
  1407.     }
  1408. }  
  1409.  
  1410. void pause_all()
  1411. {
  1412.     game_pause = true;
  1413.  
  1414.     iPauseTimer( back_timer );
  1415.     iPauseTimer( fire_timer );
  1416.     iPauseTimer( sec_timer );
  1417.     iPauseTimer( enemy_timer );
  1418. }
  1419.  
  1420. void resume_all()
  1421. {
  1422.     game_pause = false;
  1423.  
  1424.     iResumeTimer( back_timer );
  1425.     iResumeTimer( fire_timer );
  1426.     iResumeTimer( sec_timer );
  1427.     iResumeTimer( enemy_timer );
  1428. }
  1429.  
  1430. void iKeyboard(unsigned char key)
  1431. {
  1432.     if( LEVEL == GAME_PLAY )
  1433.     {
  1434.         if( game_pause )
  1435.         {
  1436.             if( key == 'e' )
  1437.             {
  1438.                 resume_all();
  1439.                 return;
  1440.             }
  1441.             else if( key == 'r' )
  1442.             {
  1443.                 resume_all();
  1444.                 after_game();
  1445.             }
  1446.             else return;
  1447.         }
  1448.  
  1449.         if( key == 'e' ) pause_all();
  1450.  
  1451.         if( key == 'q' ) //fire
  1452.         {
  1453.             plane.fire = true;
  1454.  
  1455.             pmf.push_back( bullet( plane.x, plane.y+30+15 ) );
  1456.  
  1457.             if( plane.fireLevel )
  1458.             {
  1459.                 plf.push_back( bullet( plane.x, plane.y+30+15 ) );
  1460.                 prf.push_back( bullet( plane.x, plane.y+30+15 ) );
  1461.             }
  1462.         }
  1463.     }
  1464.     else if( LEVEL == NEW_HIGH_SCORE )
  1465.     {
  1466.         if(key=='\r')
  1467.         {
  1468.             strcpy( high_name, name );
  1469.             write_data();
  1470.  
  1471.             LEVEL = MAIN_MENU;
  1472.             return;
  1473.         }
  1474.    
  1475.         if( (key>='a'&&key<='z') || (key>='A'&&key<='Z') || (key>='0'&&key<='9') || key==' ' )
  1476.         {
  1477.             int len = strlen(name);
  1478.             if( len<30 )
  1479.             {
  1480.                 name[ len ] = key;
  1481.                 name[ ++len ] = '\0';
  1482.             }
  1483.         }
  1484.     }
  1485. }
  1486.  
  1487. void iSpecialKeyboard(unsigned char key)
  1488. {
  1489.     if( LEVEL == GAME_PLAY )
  1490.     {
  1491.         if( game_pause ) return;
  1492.  
  1493.         int amount = 15;
  1494.         if( key == GLUT_KEY_DOWN )
  1495.         {
  1496.             plane.y -= amount;
  1497.             if( plane.y < 0 ) plane.y=0;
  1498.         }
  1499.         else if(  key == GLUT_KEY_UP )
  1500.         {
  1501.             plane.y += amount;
  1502.             if( plane.y > GAME_Y ) plane.y=GAME_Y;
  1503.             plane.boost = true;
  1504.         }
  1505.         else if( key == GLUT_KEY_LEFT )
  1506.         {
  1507.             plane.x -= amount;
  1508.             if( plane.x < 0 ) plane.x = 0;
  1509.         }
  1510.         else if( key == GLUT_KEY_RIGHT )
  1511.         {
  1512.             plane.x += amount;
  1513.             if( plane.x > GAME_X ) plane.x = GAME_X;
  1514.         }
  1515.     }
  1516.     else if( LEVEL == NEW_HIGH_SCORE )
  1517.     {
  1518.         if(key == GLUT_KEY_LEFT)
  1519.         {
  1520.             int len = strlen(name);
  1521.             if( len )
  1522.             {
  1523.                 name[ --len ] = '\0';
  1524.             }
  1525.         }
  1526.     }
  1527. }
  1528.  
  1529. //-------------------------------------------------------------------------------------------------------------------------
  1530. void basic_enemy_add()
  1531. {
  1532.     int x = rand()%601 + 100;
  1533.     int y = 800;
  1534.     basics.push_back( basic( x, y ) );
  1535. }
  1536.  
  1537. void middle_enemy_add()
  1538. {
  1539.     int x = rand()%601 + 100;
  1540.     int y = 800;
  1541.     middles.push_back( middle( x, y ) );
  1542. }
  1543.  
  1544. void final_enemy_add()
  1545. {
  1546.     int x = rand()%601 + 100;
  1547.     int y = 800;
  1548.     finals.push_back( final( x, y ) );
  1549. }
  1550.  
  1551. void food_add(int ind)
  1552. {
  1553.     int x = rand()%601 + 100;
  1554.     int y = 800;
  1555.     foods.push_back( food(x,y,ind) );
  1556. }
  1557.  
  1558. void enemy_change()
  1559. {
  1560.     if( LEVEL == GAME_PLAY )
  1561.     {
  1562.         int i;
  1563.         // changing foods
  1564.         for( i = 0; i<foods.size(); )
  1565.         {
  1566.             foods[i].y -= 2;
  1567.             if( foods[i].y+100 < 0 )
  1568.             {
  1569.                 foods.erase( foods.begin() + i );
  1570.             }
  1571.             else i++;
  1572.         }
  1573.         // changing the basic enemies
  1574.         for( i = 0; i<basics.size(); )
  1575.         {
  1576.             basics[i].y -= 2;
  1577.             if( basics[i].y+100 < 0 )
  1578.             {
  1579.                 basics.erase( basics.begin() + i );
  1580.             }
  1581.             else i++;
  1582.         }
  1583.         // changing the middle enemies
  1584.         for( i = 0; i<middles.size(); )
  1585.         {
  1586.             //if( middles[i].y>=650 && middles[i].y<=653 )
  1587.             if( middles[i].y >= 600 && middles[i].y <= 603 )
  1588.             {
  1589.                 ef.push_back( bullet( middles[i].x, middles[i].y ));
  1590.             }
  1591.             middles[i].y -= 3;
  1592.             if( middles[i].y+100 < 0 )
  1593.             {
  1594.                 middles.erase( middles.begin() + i );
  1595.             }
  1596.             else i++;
  1597.         }
  1598.         // changing the final enemies
  1599.         for( i = 0; i<finals.size(); )
  1600.         {
  1601.             if( finals[i].x != plane.x )
  1602.             {
  1603.                 finals[i].fire = true;
  1604.                 if( plane.x > finals[i].x )
  1605.                 {
  1606.                     if( plane.x - finals[i].x >=2 ) finals[i].x += 2;
  1607.                     else finals[i].x = plane.x;
  1608.                 }
  1609.                 else
  1610.                 {
  1611.                     if( finals[i].x - plane.x >= 2 ) finals[i].x -= 2;
  1612.                     else finals[i].x = plane.x;
  1613.                 }
  1614.             }
  1615.             else if( finals[i].fire )
  1616.             {
  1617.                 ef.push_back( bullet(finals[i].x, finals[i].y) );
  1618.                 finals[i].fire = false;
  1619.             }
  1620.  
  1621.             finals[i].y -= 4;
  1622.             if( finals[i].y+100 < 0 )
  1623.             {
  1624.                 finals.erase( finals.begin() + i );
  1625.             }
  1626.             else i++;
  1627.         }
  1628.     }
  1629. }
  1630.  
  1631. void fireChange()
  1632. {
  1633.     if( LEVEL == GAME_PLAY )
  1634.     {
  1635.         int i;
  1636.         int amount = 5;
  1637.  
  1638.         //plane's fire change
  1639.         //left
  1640.         for( i=0; i<plf.size(); )
  1641.         {
  1642.             plf[i].x -= amount;
  1643.             plf[i].y += amount;
  1644.             if( plf[i].x<0 || plf[i].y>GAME_Y ) plf.erase( plf.begin()+i );
  1645.             else i++;
  1646.         }
  1647.         //middle
  1648.         for( i=0; i<pmf.size(); )
  1649.         {
  1650.             pmf[i].y += amount;
  1651.             if( pmf[i].y>GAME_Y ) pmf.erase( pmf.begin()+i );
  1652.             else i++;
  1653.         }
  1654.         //right
  1655.         for( i=0; i<prf.size(); )
  1656.         {
  1657.             prf[i].x += amount;
  1658.             prf[i].y += amount;
  1659.             if( prf[i].x>GAME_X || prf[i].y>GAME_Y ) prf.erase( prf.begin()+i );
  1660.             else i++;
  1661.         }
  1662.         // enemy fire
  1663.         for( i=0; i<ef.size(); )
  1664.         {
  1665.             ef[i].y -= amount;
  1666.             if( ef[i].y>GAME_Y ) ef.erase( ef.begin()+i );
  1667.             else i++;
  1668.         }
  1669.     }
  1670. }
  1671.  
  1672. void secTimer()
  1673. {
  1674.     if( LEVEL == GAME_PLAY )
  1675.     {
  1676.         if( sec < 250 ) sec++;
  1677.         else sec=1;
  1678.  
  1679.         // managing foods
  1680.         if( sec % 120 == 0 ) food_add(1);
  1681.         if( sec % 17 == 0 ) food_add(2);
  1682.         if( plane.fireLevel ) plane.fireLevel++;
  1683.         if( plane.fireLevel == 11 ) plane.fireLevel = 0;
  1684.  
  1685.         // adding basic enemies
  1686.         if( sec % 6 == 0 ) basic_enemy_add();
  1687.         // adding middle enemies
  1688.         if( sec % 5 == 0 && score >= 0 ) middle_enemy_add();
  1689.         // adding final enemies
  1690.         if( sec % 7 == 0 && score >= 0 ) final_enemy_add();
  1691.  
  1692.         // managing player down
  1693.         if( player_down )
  1694.         {
  1695.             player_down++;
  1696.             sec = 1;
  1697.         }
  1698.         if( player_down == 5 )
  1699.         {
  1700.             if( !life ) after_game() ;
  1701.             player_down = 0;
  1702.             sec = 1;
  1703.         }
  1704.     }
  1705.     else if( LEVEL == INTRODUCTION )
  1706.     {
  1707.         intro_sec++;
  1708.         if( intro_sec == 11 )
  1709.         {
  1710.             LEVEL = MAIN_MENU;
  1711.         }
  1712.     }
  1713.     else if( LEVEL == LOAD_GAME )
  1714.     {
  1715.         load_sec++;
  1716.         if( load_sec == 7 )
  1717.         {
  1718.             initializeGame();
  1719.             LEVEL = GAME_PLAY;
  1720.         }
  1721.     }
  1722. }
  1723.  
  1724. //---------------------------------------------------------------------------------------------------------------------------
  1725.  
  1726. void initializeGame()
  1727. {
  1728.     sec = 1;
  1729.     plane.initialize();
  1730.     score = 0;
  1731.     clear_all();
  1732.     player_down = 0;
  1733.     life = 2;
  1734.     game_pause = false;
  1735.  
  1736.     // initialize background
  1737.     first_time_background = true;
  1738.     point_add_count = 0;
  1739. }
  1740.  
  1741. int main()
  1742. {
  1743.     // launching introduction
  1744.     intro_sec = 1;
  1745.     LEVEL = INTRODUCTION;
  1746.  
  1747.     // data reading initialization
  1748.     read_data();
  1749.  
  1750.     //initializeGame();
  1751.  
  1752.     back_timer = iSetTimer( 5, changeBackGround ); // setting the background
  1753.     fire_timer = iSetTimer( 3, fireChange ); // spreading bullet fires both from player's ship and enemy ship
  1754.     sec_timer = iSetTimer( 1000, secTimer ); // general purpose 1 second timer
  1755.     enemy_timer = iSetTimer( 6, enemy_change ); // advancing enemy positions
  1756.  
  1757.     iInitialize(MAIN_X, MAIN_Y, "Star Wars");
  1758.     return 0;
  1759. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement