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.
enemies.h
00001 #include "mbed.h" 00002 #include "Gamepad.h" 00003 #include "N5110.h" 00004 #include "stdlib.h" 00005 00006 /** The integers to show whether the enemies could be used again */ 00007 struct eninumber{ 00008 int p1; /**< The first integer to show whether the ship is been hit*/ 00009 int p2; /**< The second integer to show whether the ship is been hit*/ 00010 }; 00011 00012 /** enemies Class 00013 @brief Library for setting the numbers that shows whether there is a ship on the lcd and if not, generate one. 00014 @brief The library also could set random ship coordinates. 00015 @brief The library can generate two types of ships. 00016 00017 00018 @brief Revision 1.0 00019 00020 @author Miao Sixiang 00021 @date 18th April 2019 00022 00023 @code 00024 #include "mbed.h" 00025 #include "Gamepad.h" 00026 #include "N5110.h" 00027 #include "enemies.h" 00028 #include "math.h" 00029 #include "ship.h" 00030 #include "Bitmap.h" 00031 00032 00033 #ifdef WITH_TESTING 00034 #include "tests.h" 00035 #endif 00036 00037 00038 DigitalOut r_led(LED_RED); 00039 DigitalOut g_led(LED_GREEN); 00040 DigitalOut b_led(LED_BLUE); 00041 DigitalOut led1(PTA1); 00042 DigitalOut led2(PTA2); 00043 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); 00044 00045 Gamepad pad; 00046 enemies eni; 00047 ship shi; 00048 00049 void init(); 00050 void welcome(); 00051 bool explosionjudge(float x1,float y1,float x2,float y2); 00052 void explosion(int x, int y); 00053 void game_over(); 00054 00055 float xp1,xp2,yp1,yp2,xn1,yn1,xn2,yn2,x,y,x1,y1,x2,y2,v; 00056 int p1,p2,score,high_score,count; 00057 Vector2D enic1 = {xn1, yn1}; 00058 Vector2D enic2 = {xn2, yn2}; 00059 const int explosion123[10][10] = { 00060 { 0,0,0,0,1,0,0,0,0,0 }, 00061 { 0,1,0,1,0,1,0,0,1,0 }, 00062 { 0,1,0,1,0,1,1,1,1,0 }, 00063 { 0,0,1,0,0,0,0,1,0,0 }, 00064 { 0,1,0,0,0,0,0,0,1,0 }, 00065 { 0,1,0,0,0,0,0,0,1,0 }, 00066 { 0,0,1,0,0,0,0,1,0,0 }, 00067 { 0,1,0,1,0,0,1,1,0,0 }, 00068 { 1,0,0,0,1,1,0,0,1,0 }, 00069 { 0,0,0,0,0,0,0,0,0,0 }, 00070 }; 00071 00072 int main() { 00073 00074 //initial all the components 00075 init();//the lcd is initialed 00076 lcd.init(); 00077 lcd.normalMode(); 00078 lcd.setContrast(0.5); 00079 00080 00081 count = 0; 00082 score = 0; 00083 x = 5.0; 00084 y = 21.0; 00085 x1 = 0.0; 00086 y1 = 0.0; 00087 x2 = 0.0; 00088 y2 = 0.0; 00089 v = 4.0; 00090 p1 = p2 = 0; 00091 xp1 = xp2 = xn1 =xn2 =0.0; 00092 yp1 = yp2 = yn1 = yn2 =0.0; 00093 enic1.x = enic2.x = 0.0; 00094 00095 high_score = 0; 00096 00097 shi.set_ship(y,x,lcd); 00098 00099 welcome(); 00100 while (1) { 00101 lcd.clear(); 00102 Vector2D coord = pad.get_mapped_coord(); 00103 x = x + coord.x; 00104 y = y - coord.y; 00105 shi.set_ship(y,x,lcd); 00106 00107 if (pad.check_event(Gamepad::A_PRESSED)){ 00108 if (x1 == 0){ 00109 x1 = x; 00110 y1 = y; 00111 x1 = shi.open_fire(x1,y1,v,lcd); 00112 if (!x2 ==0){ 00113 x2 = shi.open_fire(x2,y2,v,lcd); 00114 } 00115 } else if (x1 > 0 and x2 == 0){ 00116 x2 = x; 00117 y2 = y; 00118 x2 = shi.open_fire(x2,y2,v,lcd); 00119 x1 = shi.open_fire(x1,y1,v,lcd); 00120 } else if (x1 > 0 and x2 > 0){ 00121 x2 = shi.open_fire(x2,y2,v,lcd); 00122 x1 = shi.open_fire(x1,y1,v,lcd); 00123 } 00124 } else if (x1 > 0 or x2 > 0) { 00125 if (x1 > 0){ 00126 x1 = shi.open_fire(x1,y1,v,lcd); 00127 } 00128 if (x2 > 0){ 00129 x2 = shi.open_fire(x2,y2,v,lcd); 00130 } 00131 } 00132 if (x1 >= 84){ 00133 x1 = 0; 00134 } 00135 if (x2 >= 84) { 00136 x2 = 0; 00137 } 00138 00139 00140 00141 eninumber enin = eni.enemiesjudge(enic1.x, enic2.x); 00142 p1 = enin.p1; 00143 p2 = enin.p2; 00144 if (p1 == 1){ 00145 enic1 = eni.set_coord1(p1); 00146 } 00147 if (p2 == 1){ 00148 enic2 = eni.set_coord1(p2); 00149 } 00150 eni.drawenemies_small(enic1.x, enic1.y,lcd); 00151 eni.drawenemies_large(enic2.x, enic2.y,lcd); 00152 00153 00154 if (explosionjudge(enic1.x,enic1.y,x1,y1)) { 00155 explosion(x1, y1); 00156 x1 = 0; 00157 count = count + 1; 00158 enic1.x = 0; 00159 } 00160 if (explosionjudge(enic2.x,enic2.y,x1,y1)) { 00161 explosion(x1, y1); 00162 x1= 0; 00163 count = count + 1; 00164 enic2.x = 0; 00165 } 00166 if (explosionjudge(enic1.x,enic1.y,x2,y2)) { 00167 explosion(x1, y1); 00168 x2 = 0; 00169 count = count + 1; 00170 enic1.x = 0; 00171 } 00172 if (explosionjudge(enic2.x,enic2.y,x2,y2)) { 00173 explosion(x2, y2); 00174 count = count + 1; 00175 x2 = 0; 00176 enic2.x = 0; 00177 } 00178 00179 00180 score = count *50; 00181 00182 00183 if (xp1 <= 4) { 00184 xp1 = enic1.x; 00185 yp1 = enic1.y; 00186 }else if (xp1 > 0) { 00187 xp1 = eni.open_fire_en(xp1, yp1,lcd); 00188 } 00189 if (xp2 <= 4){ 00190 xp2 = enic2.x; 00191 yp2 = enic2.y; 00192 }else if (xp2 > 0) { 00193 xp2 = eni.open_fire_en(xp2, yp2,lcd); 00194 } 00195 00196 if (score > high_score){ 00197 high_score = score; 00198 } 00199 00200 if (explosionjudge(x,y,xp1,yp1)) { 00201 explosion(xp1, yp1); 00202 xp1 = 0; 00203 game_over(); 00204 } 00205 if (explosionjudge(x,y,xp2,yp2)) { 00206 explosion(xp2, yp2); 00207 xp2 = 0; 00208 game_over(); 00209 } 00210 00211 00212 00213 00214 wait(0.1); 00215 lcd.refresh(); 00216 } 00217 } 00218 00219 00220 00221 00222 00223 00224 void init() 00225 { 00226 // need to initialise LCD and Gamepad 00227 pad.init(); 00228 00229 count = 0; 00230 score = 0; 00231 x = 5.0; 00232 y = 21.0; 00233 x1 = 0.0; 00234 y1 = 0.0; 00235 x2 = 0.0; 00236 y2 = 0.0; 00237 v = 4.0; 00238 p1 = p2 = 0; 00239 xp1 = xp2 = xn1 =xn2 =0.0; 00240 yp1 = yp2 = yn1 = yn2 =0.0; 00241 enic1.x = enic2.x = 0.0; 00242 00243 // initialise the game with correct ball and paddle sizes 00244 00245 } 00246 00247 00248 00249 void welcome() { 00250 00251 lcd.printString(" HALO WAR ",0,1); 00252 lcd.printString(" Press Start ",0,4); 00253 lcd.refresh(); 00254 00255 // wait flashing LEDs until start button is pressed 00256 while ( pad.check_event(Gamepad::START_PRESSED) == false) { 00257 pad.leds_on(); 00258 wait(0.1); 00259 pad.leds_off(); 00260 wait(0.1); 00261 00262 } 00263 } 00264 00265 bool explosionjudge(float x1,float y1,float x2,float y2){ 00266 float d; 00267 d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); 00268 if (d <= 9){ 00269 return true; 00270 }else { 00271 return false; 00272 } 00273 } 00274 00275 void explosion(int x, int y){ 00276 00277 lcd.drawSprite(x, y, 10, 10, (int *)explosion123); 00278 } 00279 00280 00281 void game_over(){ 00282 while(!pad.check_event(Gamepad::START_PRESSED)){ 00283 char buffer1[20]; 00284 sprintf(buffer1, "High Score=%d", high_score); 00285 lcd.printString(buffer1,0,0); 00286 char buffer[14]; 00287 sprintf(buffer, " Score = %d ", score); 00288 lcd.printString(buffer,0,1); 00289 lcd.printString(" Wasted ",0,2); 00290 lcd.printString("Press start to",0,3); 00291 lcd.printString(" new compaign",0,4); 00292 lcd.refresh(); 00293 lcd.clear(); 00294 } 00295 init(); 00296 } 00297 00298 @endcode 00299 */ 00300 00301 class enemies 00302 { 00303 public: 00304 00305 /** Judge whether there are any enemy ships in the lcd. 00306 * 00307 * @param xp1 The horizontal position of the first ship, if xp1 <= 0, means that the fisrt ship is not on the lcd. 00308 * @param xp2 The horizontal position of the second ship, if xp2 <= 0, means that the second ship is not on the lcd. 00309 * @returns 00310 * There are two elements in the struct, both 1 or 0; 00311 * 0 - There is already a ship on the lcd. 00312 * 1 - The ship could be draw on the lcd again. 00313 * 00314 */ 00315 eninumber enemiesjudge(int xp1, int xp2); 00316 00317 00318 00319 /** To draw an enemy ship on the lcd, the smaller one, at the given position. 00320 * 00321 * @param x The horizontal position of the ship, set the core of the ship at x. 00322 * @param y The vertical position of the ship, set the core of the ship at y. 00323 * 00324 */ 00325 void drawenemies_small(float x, float y, N5110 &lcd); 00326 00327 00328 /** To set new coordinates for the new ships. 00329 * 00330 * @param p1 The integer that could ensure whether the ship could be generated. 00331 * @returns 00332 * There are two elements in the vector. 00333 * x -The horizontal coordinate of the enemy ship 00334 * y -The vertical coordinate of the enemy ship 00335 */ 00336 Vector2D set_coord1(int p1); 00337 00338 00339 /** To set new coordinates for the new ships. 00340 * 00341 * @param p2 The integer that could ensure whether the ship could be generated. 00342 * @returns 00343 * There are two elements in the vector. 00344 * x -The horizontal coordinate of the enemy ship 00345 * y -The vertical coordinate of the enemy ship 00346 */ 00347 Vector2D set_coord2(int p2); 00348 00349 00350 /** To set a missile released by the enemies. 00351 * 00352 * @param x The horizontal coordinate of the enemy ship that will release missile. 00353 * @param y The vertical coordinate of the enemy ship that will release missile. 00354 * @returns 00355 * The last coordinate of the missile. 00356 * x -The horizontal coordinate of the missile 00357 * 00358 */ 00359 int open_fire_en(int x, int y, N5110 &lcd); 00360 00361 00362 /** To draw an enemy ship on the lcd, the larger one, at the given position. 00363 * 00364 * @param x The horizontal position of the ship, set the core of the ship at x. 00365 * @param y The vertical position of the ship, set the core of the ship at y. 00366 * 00367 */ 00368 void drawenemies_large(float x, float y, N5110 &lcd); 00369 00370 private: 00371 int _p1; 00372 int _p2; 00373 float _xp1; 00374 float _yp1; 00375 float _xp2; 00376 float _yp2; 00377 int _vp; 00378 };
Generated on Fri Jul 15 2022 11:33:55 by
1.7.2