Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of fy15raf by
GameEngine.cpp
00001 #include "GameEngine.h" 00002 00003 GameEngine::GameEngine() 00004 { 00005 00006 } 00007 00008 GameEngine::~GameEngine() 00009 { 00010 00011 } 00012 00013 00014 void GameEngine::init() 00015 { 00016 //initialise game objects Spaceship and 3 asteroids with diffrent speeds 00017 _Spaceship.init(); 00018 _asteroid1.init(2); 00019 _asteroid2.init(3); 00020 _asteroid3.init(4); 00021 time.reset(); 00022 gameOver=0; 00023 } 00024 00025 00026 void GameEngine::read_input(Gamepad &pad) 00027 { 00028 _d = pad.get_direction(); 00029 _mag = pad.get_mag(); 00030 } 00031 00032 00033 void GameEngine::draw(N5110 &lcd) 00034 { 00035 // draw the game elements in the LCD 00036 _Spaceship.draw(lcd); 00037 _asteroid1.draw(lcd); 00038 _asteroid2.draw(lcd); 00039 _asteroid3.draw(lcd); 00040 00041 //print the 3 hearts life 00042 draw_hearts(lcd); 00043 00044 //print the recorded time in sec 00045 print_time(lcd); 00046 } 00047 00048 00049 void GameEngine::update(Gamepad &pad) 00050 { 00051 00052 //checking collisions with Asteroid 1, 2 and 3 00053 check_collision1(pad); 00054 check_collision2(pad); 00055 check_collision3(pad); 00056 00057 //updating the display 00058 _Spaceship.update(_d,_mag); 00059 _asteroid1.update(); 00060 _asteroid2.update(); 00061 _asteroid3.update(); 00062 00063 //updating the time 00064 time_increment(); 00065 } 00066 00067 00068 void GameEngine::check_collision1(Gamepad &pad) 00069 { 00070 Vector2D _asteroid1_pos = _asteroid1.get_pos() ; 00071 Vector2D _Spaceship_pos = _Spaceship.get_pos(); 00072 00073 // check if the collision occers with Asteroid 1 00074 if ((_Spaceship_pos.x+10 >=_asteroid1_pos.x) &&( _Spaceship_pos.x+8 <_asteroid1_pos.x )) { 00075 00076 if( ( _asteroid1_pos.y >= _Spaceship_pos.y) && ( _asteroid1_pos.y <= _Spaceship_pos.y+7) || 00077 (_asteroid1_pos.y+6 >= _Spaceship_pos.y) &&(_asteroid1_pos.y+6 <= _Spaceship_pos.y+7 )) { 00078 00079 _Spaceship.add_collisions(); 00080 pad.tone(1500.0,0.5); //tone 00081 pad.leds_on(); //LEDs flashing 00082 wait(0.5); 00083 pad.leds_off(); 00084 //debugging 00085 printf("((collision occurs))\n" ); 00086 } 00087 } 00088 } 00089 00090 00091 void GameEngine::check_collision2(Gamepad &pad) 00092 { 00093 00094 Vector2D _asteroid2_pos = _asteroid2.get_pos(); 00095 Vector2D _Spaceship_pos = _Spaceship.get_pos(); 00096 00097 // check if the collision occers with Asteroid 2 00098 if ((_Spaceship_pos.x+10 >=_asteroid2_pos.x) && (_Spaceship_pos.x+8 <_asteroid2_pos.x )) { 00099 00100 if( (_asteroid2_pos.y >= _Spaceship_pos.y) && ( _asteroid2_pos.y <= _Spaceship_pos.y+7) || 00101 (_asteroid2_pos.y+6 >= _Spaceship_pos.y) &&( _asteroid2_pos.y+6 <= _Spaceship_pos.y+7 )) { 00102 00103 _Spaceship.add_collisions(); 00104 pad.tone(1500.0,0.5); //tone 00105 pad.leds_on(); //LEDs flashing 00106 wait(0.5); 00107 pad.leds_off(); 00108 //debugging 00109 printf("((collision occurs))\n"); 00110 } 00111 } 00112 } 00113 00114 00115 void GameEngine::check_collision3(Gamepad &pad) 00116 { 00117 00118 Vector2D _asteroid3_pos = _asteroid3.get_pos(); 00119 Vector2D _Spaceship_pos = _Spaceship.get_pos(); 00120 00121 // check if the collision occers with Asteroid 3 00122 if ((_Spaceship_pos.x+10 >= _asteroid3_pos.x) &&( _Spaceship_pos.x+6 < _asteroid3_pos.x )) { 00123 00124 if( (_asteroid3_pos.y >= _Spaceship_pos.y) &&( _asteroid3_pos.y <= _Spaceship_pos.y+7) || 00125 ( _asteroid3_pos.y+6 >= _Spaceship_pos.y) &&(_asteroid3_pos.y+6 <= _Spaceship_pos.y+7 )) { 00126 00127 _Spaceship.add_collisions(); 00128 pad.tone(1500.0,0.5); //tone 00129 pad.leds_on(); //LEDs flashing 00130 wait(0.5); 00131 pad.leds_off(); 00132 //debugging 00133 printf("((collision occurs))\n"); 00134 } 00135 } 00136 } 00137 00138 00139 void GameEngine::draw_hearts(N5110 &lcd) 00140 { 00141 // get collision from Spaceship class 00142 int _Spaceship_collisions= _Spaceship.get_collisions(); 00143 00144 // reduce the hearts with each collision till the third collision the game ends 00145 if (_Spaceship_collisions==0) { 00146 _Spaceship.drawFullHearts(lcd); 00147 00148 } else if (_Spaceship_collisions ==1) { 00149 _Spaceship.drawTwoHearts(lcd); 00150 00151 } else if (_Spaceship_collisions == 2) { 00152 _Spaceship.drawOneHeart(lcd); 00153 00154 } else { 00155 gameOver=1; 00156 00157 } 00158 } 00159 00160 00161 int GameEngine::check_gameOver() 00162 { 00163 //stop the time and return if game over 00164 time.stop (); 00165 return gameOver; 00166 } 00167 00168 00169 void GameEngine::reset_gameOver(){ 00170 00171 //rest time and game over value to play again 00172 time.reset(); 00173 gameOver=0; 00174 } 00175 00176 00177 void GameEngine::time_increment() 00178 { 00179 //start and incrementing the time and read it 00180 time.start(); 00181 _time = time.read(); 00182 } 00183 00184 00185 void GameEngine::time_stop() 00186 { 00187 //stop the time 00188 time.stop(); 00189 } 00190 00191 00192 void GameEngine::print_time(N5110 &lcd) 00193 { 00194 // print the time 00195 char buffer1[11]; 00196 sprintf(buffer1,"%4d",_time); //maximum time 9999 00197 lcd.printString(buffer1,60, 0); 00198 } 00199 00200 00201 void GameEngine::print_travel_time(N5110 &lcd) 00202 { 00203 // print the time at the end of the game 00204 char buffer1[11]; 00205 sprintf(buffer1," %4d sec",_time); //maximum time 9999 00206 lcd.printString(buffer1,0, 3); 00207 } 00208
Generated on Thu Jul 14 2022 18:15:41 by
