Yingjian Guo / GameEngine
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GameEngine.cpp Source File

GameEngine.cpp

00001 #include "GameEngine.h"
00002 //funtion to construct
00003 GameEngine::GameEngine()
00004 {
00005 
00006 }
00007 
00008 //function to destruct
00009 GameEngine::~GameEngine()
00010 {
00011     
00012 }
00013 
00014 //function to initialise the game parameters
00015 void GameEngine::init()
00016 {
00017     monster_number = 50;                // monster number is set to 50
00018     life = 5;                           // 3 chances to miss the monster 
00019     _A = 0;                             // A button check 
00020     
00021     _Jet.init();                        // initialise the jet
00022     _Monster.init();                    // initialise the monster
00023 }
00024 
00025 //function to read inputs
00026 void GameEngine::read_input(Gamepad &pad)
00027 {
00028     _d = pad.get_direction();           // get the move direction of the joystick
00029     _mag = pad.get_mag();               // get the move magnitude of the joystick
00030     check_fire(pad);                    // check if the fire button is pressed
00031 }
00032 
00033 //function to draw all elements
00034 void GameEngine::draw(N5110 &lcd)
00035 {
00036     // the bitmap of the cloud
00037     static int cloud[] = 
00038     {    
00039         0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,
00040         0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,
00041         0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,
00042         0,1,0,0,0,0,0,0,0,1,0,1,1,0,0,
00043         1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,
00044         1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
00045         0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,
00046         0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,
00047         0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,
00048         0,0,0,0,0,1,1,0,0,0,1,1,0,0,0, 
00049     };
00050     lcd.clear();
00051     Bitmap sprite(cloud, 10, 15);                   // rows & columns of the cloud
00052     sprite.render(lcd, 60, 5);                      // draw clouds
00053     sprite.render(lcd, 10, 32);
00054     _Jet.draw(lcd);                                 // draw jet
00055     _Monster.draw(lcd);                             // draw monster
00056     if(_A == 1)                                     // if fire button is  pressed
00057     {
00058         _Ammo.draw(lcd);                            // draw bullet
00059     }
00060     sprite.render(lcd, 35, 19);
00061     
00062     sprintf(life_buffer,"x%d",life);
00063     lcd.printString(life_buffer,1,0);               // draw life value
00064     sprintf(number_buffer,"%d",monster_number);
00065     lcd.printString(number_buffer,25,0);            // draw monster number
00066     lcd.refresh();
00067 }
00068 
00069 //function to update game parameters
00070 void GameEngine::update(Gamepad &pad)
00071 {
00072     _Jet.update(_d,_mag);                           // update jet position
00073     _Ammo.update();                                 // update ammo position
00074     _Monster.update();                              // update monster position
00075     check_hit_monster(pad);                         // check if the monster is killed
00076     check_Jet_collision(pad);                       // check if the jet crashed
00077     check_hit_wall(pad);                            // check if the monster hits the wall
00078 }
00079 
00080 //function to check the fire button
00081 void GameEngine::check_fire(Gamepad &pad)
00082 {
00083     // intialise the bullet if the fire button if pressed
00084     // the coordinates of the bullet is according to the coordinates of the jet
00085     if (pad.check_event(Gamepad::A_PRESSED) == true)
00086         {
00087           pad.tone(1000.0,0.1);
00088           Vector2D Jet_pos = _Jet.get_pos();
00089           //printf("x=%f,y=%f\n", Jet_pos.x, Jet_pos.y);     
00090           _Ammo.init(Jet_pos.x+7, Jet_pos.y+5);
00091           _A = 1;
00092         }  
00093 }
00094 
00095 //function to check if the jet crashs
00096 void GameEngine::check_Jet_collision(Gamepad &pad)
00097 {
00098     Vector2D Jet_pos = _Jet.get_pos();
00099     Vector2D Monster_pos = _Monster.get_pos();
00100     //printf("Jet_x=%f,Jet_y=%f\n",Jet_pos.x,Jet_pos.y);
00101     //printf("M_x=%f,M_y=%f\n",Monster_pos.x, Monster_pos.y);
00102     
00103     // reset jet when the monster hits the jet
00104     if(
00105     (Jet_pos.x+13 > Monster_pos.x)&&    // right
00106     (Jet_pos.x < Monster_pos.x+10)&&    // left
00107     (Jet_pos.y+12 > Monster_pos.y)&&    // top
00108     (Jet_pos.y < Monster_pos.y+10))     // bottom
00109     {
00110         life -= 1;
00111         pad.tone(1500.0,0.2);
00112         _Jet.init();
00113         _Monster.init();
00114     }    
00115 }
00116 
00117 //function to check if the monster is killed
00118 void GameEngine::check_hit_monster(Gamepad &pad)
00119 {
00120     Vector2D Ammo_pos = _Ammo.get_pos();
00121     Vector2D Monster_pos = _Monster.get_pos();
00122     
00123     // monster diappears if the bullet hits the monster
00124     // set new monster
00125     if(
00126     (Ammo_pos.x+1 > Monster_pos.x)&&
00127     (Ammo_pos.y+1 >= Monster_pos.y)&&
00128     (Ammo_pos.y+1< Monster_pos.y+10)&&
00129     (_A ==1))
00130     {
00131         pad.tone(100.0,0.2);
00132         _A = 0; //Ammo disappears after hit the monster
00133         monster_number -= 1;
00134         _Monster.init();
00135         _Jet.~Jet(); 
00136     }
00137     // monster reaches the left edge of the screen
00138     // can miss 3 monsters maximum
00139     else if (Monster_pos.x+9 <= 1)
00140     {
00141         life -= 1;
00142         pad.tone(300.0,0.2);
00143         _Monster.init();
00144     }
00145 }
00146 
00147 //function to check if the monster hits the wall
00148 void GameEngine::check_hit_wall(Gamepad &pad)
00149 {
00150     Vector2D Monster_pos = _Monster.get_pos();
00151     Vector2D Monster_velocity = _Monster.get_velocity();
00152     
00153     // the monster bounces back when it hits the wall
00154     if(Monster_pos.y <= 0)
00155     {   
00156         Monster_pos.y = 0;                          // top
00157         Monster_velocity.y = -Monster_velocity.y;
00158         pad.tone(500.0,0.2);
00159     }
00160     else if(Monster_pos.y >= HEIGHT - 8)
00161     {
00162         Monster_pos.y = HEIGHT - 8;                 // bottom
00163         Monster_velocity.y = -Monster_velocity.y;
00164         pad.tone(500.0,0.2);
00165     }
00166     _Monster.set_velocity(Monster_velocity);        // set velocity
00167     _Monster.set_pos(Monster_pos);                  //monster stays on the screen
00168 }
00169 
00170 //function to get the value of life
00171 int GameEngine::get_life()
00172 {
00173     return life;   
00174 }
00175 
00176 //function to get the monster number
00177 int GameEngine::get_monster_number()
00178 {
00179     return monster_number;    
00180 }