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