Miao Sixiang 201089108

Dependencies:   mbed N5110

Committer:
Rubio
Date:
Sun May 05 16:54:20 2019 +0000
Revision:
0:7f86012fb8b5
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rubio 0:7f86012fb8b5 1 #include "mbed.h"
Rubio 0:7f86012fb8b5 2 #include "Gamepad.h"
Rubio 0:7f86012fb8b5 3 #include "N5110.h"
Rubio 0:7f86012fb8b5 4 #include "stdlib.h"
Rubio 0:7f86012fb8b5 5
Rubio 0:7f86012fb8b5 6
Rubio 0:7f86012fb8b5 7 /** ship Class
Rubio 0:7f86012fb8b5 8 @brief Library for setting the numbers that shows whether there is a ship on the lcd and if not, generate one.
Rubio 0:7f86012fb8b5 9 @brief The library also could set random ship coordinates.
Rubio 0:7f86012fb8b5 10 @brief The library can generate two types of ships.
Rubio 0:7f86012fb8b5 11
Rubio 0:7f86012fb8b5 12
Rubio 0:7f86012fb8b5 13 @brief Revision 1.0
Rubio 0:7f86012fb8b5 14
Rubio 0:7f86012fb8b5 15 @author Miao Sixiang
Rubio 0:7f86012fb8b5 16 @date 18th April 2019
Rubio 0:7f86012fb8b5 17
Rubio 0:7f86012fb8b5 18 @code
Rubio 0:7f86012fb8b5 19 #include "mbed.h"
Rubio 0:7f86012fb8b5 20 #include "Gamepad.h"
Rubio 0:7f86012fb8b5 21 #include "N5110.h"
Rubio 0:7f86012fb8b5 22 #include "enemies.h"
Rubio 0:7f86012fb8b5 23 #include "math.h"
Rubio 0:7f86012fb8b5 24 #include "ship.h"
Rubio 0:7f86012fb8b5 25 #include "Bitmap.h"
Rubio 0:7f86012fb8b5 26
Rubio 0:7f86012fb8b5 27
Rubio 0:7f86012fb8b5 28 #ifdef WITH_TESTING
Rubio 0:7f86012fb8b5 29 #include "tests.h"
Rubio 0:7f86012fb8b5 30 #endif
Rubio 0:7f86012fb8b5 31
Rubio 0:7f86012fb8b5 32
Rubio 0:7f86012fb8b5 33 DigitalOut r_led(LED_RED);
Rubio 0:7f86012fb8b5 34 DigitalOut g_led(LED_GREEN);
Rubio 0:7f86012fb8b5 35 DigitalOut b_led(LED_BLUE);
Rubio 0:7f86012fb8b5 36 DigitalOut led1(PTA1);
Rubio 0:7f86012fb8b5 37 DigitalOut led2(PTA2);
Rubio 0:7f86012fb8b5 38 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Rubio 0:7f86012fb8b5 39
Rubio 0:7f86012fb8b5 40 Gamepad pad;
Rubio 0:7f86012fb8b5 41 enemies eni;
Rubio 0:7f86012fb8b5 42 ship shi;
Rubio 0:7f86012fb8b5 43
Rubio 0:7f86012fb8b5 44 void init();
Rubio 0:7f86012fb8b5 45 void welcome();
Rubio 0:7f86012fb8b5 46 bool explosionjudge(float x1,float y1,float x2,float y2);
Rubio 0:7f86012fb8b5 47 void explosion(int x, int y);
Rubio 0:7f86012fb8b5 48 void game_over();
Rubio 0:7f86012fb8b5 49
Rubio 0:7f86012fb8b5 50 float xp1,xp2,yp1,yp2,xn1,yn1,xn2,yn2,x,y,x1,y1,x2,y2,v;
Rubio 0:7f86012fb8b5 51 int p1,p2,score,high_score,count;
Rubio 0:7f86012fb8b5 52 Vector2D enic1 = {xn1, yn1};
Rubio 0:7f86012fb8b5 53 Vector2D enic2 = {xn2, yn2};
Rubio 0:7f86012fb8b5 54 const int explosion123[10][10] = {
Rubio 0:7f86012fb8b5 55 { 0,0,0,0,1,0,0,0,0,0 },
Rubio 0:7f86012fb8b5 56 { 0,1,0,1,0,1,0,0,1,0 },
Rubio 0:7f86012fb8b5 57 { 0,1,0,1,0,1,1,1,1,0 },
Rubio 0:7f86012fb8b5 58 { 0,0,1,0,0,0,0,1,0,0 },
Rubio 0:7f86012fb8b5 59 { 0,1,0,0,0,0,0,0,1,0 },
Rubio 0:7f86012fb8b5 60 { 0,1,0,0,0,0,0,0,1,0 },
Rubio 0:7f86012fb8b5 61 { 0,0,1,0,0,0,0,1,0,0 },
Rubio 0:7f86012fb8b5 62 { 0,1,0,1,0,0,1,1,0,0 },
Rubio 0:7f86012fb8b5 63 { 1,0,0,0,1,1,0,0,1,0 },
Rubio 0:7f86012fb8b5 64 { 0,0,0,0,0,0,0,0,0,0 },
Rubio 0:7f86012fb8b5 65 };
Rubio 0:7f86012fb8b5 66
Rubio 0:7f86012fb8b5 67 int main() {
Rubio 0:7f86012fb8b5 68
Rubio 0:7f86012fb8b5 69 //initial all the components
Rubio 0:7f86012fb8b5 70 init();//the lcd is initialed
Rubio 0:7f86012fb8b5 71 lcd.init();
Rubio 0:7f86012fb8b5 72 lcd.normalMode();
Rubio 0:7f86012fb8b5 73 lcd.setContrast(0.5);
Rubio 0:7f86012fb8b5 74
Rubio 0:7f86012fb8b5 75
Rubio 0:7f86012fb8b5 76 count = 0;
Rubio 0:7f86012fb8b5 77 score = 0;
Rubio 0:7f86012fb8b5 78 x = 5.0;
Rubio 0:7f86012fb8b5 79 y = 21.0;
Rubio 0:7f86012fb8b5 80 x1 = 0.0;
Rubio 0:7f86012fb8b5 81 y1 = 0.0;
Rubio 0:7f86012fb8b5 82 x2 = 0.0;
Rubio 0:7f86012fb8b5 83 y2 = 0.0;
Rubio 0:7f86012fb8b5 84 v = 4.0;
Rubio 0:7f86012fb8b5 85 p1 = p2 = 0;
Rubio 0:7f86012fb8b5 86 xp1 = xp2 = xn1 =xn2 =0.0;
Rubio 0:7f86012fb8b5 87 yp1 = yp2 = yn1 = yn2 =0.0;
Rubio 0:7f86012fb8b5 88 enic1.x = enic2.x = 0.0;
Rubio 0:7f86012fb8b5 89
Rubio 0:7f86012fb8b5 90 high_score = 0;
Rubio 0:7f86012fb8b5 91
Rubio 0:7f86012fb8b5 92 shi.set_ship(y,x,lcd);
Rubio 0:7f86012fb8b5 93
Rubio 0:7f86012fb8b5 94 welcome();
Rubio 0:7f86012fb8b5 95 while (1) {
Rubio 0:7f86012fb8b5 96 lcd.clear();
Rubio 0:7f86012fb8b5 97 Vector2D coord = pad.get_mapped_coord();
Rubio 0:7f86012fb8b5 98 x = x + coord.x;
Rubio 0:7f86012fb8b5 99 y = y - coord.y;
Rubio 0:7f86012fb8b5 100 shi.set_ship(y,x,lcd);
Rubio 0:7f86012fb8b5 101
Rubio 0:7f86012fb8b5 102 if (pad.check_event(Gamepad::A_PRESSED)){
Rubio 0:7f86012fb8b5 103 if (x1 == 0){
Rubio 0:7f86012fb8b5 104 x1 = x;
Rubio 0:7f86012fb8b5 105 y1 = y;
Rubio 0:7f86012fb8b5 106 x1 = shi.open_fire(x1,y1,v,lcd);
Rubio 0:7f86012fb8b5 107 if (!x2 ==0){
Rubio 0:7f86012fb8b5 108 x2 = shi.open_fire(x2,y2,v,lcd);
Rubio 0:7f86012fb8b5 109 }
Rubio 0:7f86012fb8b5 110 } else if (x1 > 0 and x2 == 0){
Rubio 0:7f86012fb8b5 111 x2 = x;
Rubio 0:7f86012fb8b5 112 y2 = y;
Rubio 0:7f86012fb8b5 113 x2 = shi.open_fire(x2,y2,v,lcd);
Rubio 0:7f86012fb8b5 114 x1 = shi.open_fire(x1,y1,v,lcd);
Rubio 0:7f86012fb8b5 115 } else if (x1 > 0 and x2 > 0){
Rubio 0:7f86012fb8b5 116 x2 = shi.open_fire(x2,y2,v,lcd);
Rubio 0:7f86012fb8b5 117 x1 = shi.open_fire(x1,y1,v,lcd);
Rubio 0:7f86012fb8b5 118 }
Rubio 0:7f86012fb8b5 119 } else if (x1 > 0 or x2 > 0) {
Rubio 0:7f86012fb8b5 120 if (x1 > 0){
Rubio 0:7f86012fb8b5 121 x1 = shi.open_fire(x1,y1,v,lcd);
Rubio 0:7f86012fb8b5 122 }
Rubio 0:7f86012fb8b5 123 if (x2 > 0){
Rubio 0:7f86012fb8b5 124 x2 = shi.open_fire(x2,y2,v,lcd);
Rubio 0:7f86012fb8b5 125 }
Rubio 0:7f86012fb8b5 126 }
Rubio 0:7f86012fb8b5 127 if (x1 >= 84){
Rubio 0:7f86012fb8b5 128 x1 = 0;
Rubio 0:7f86012fb8b5 129 }
Rubio 0:7f86012fb8b5 130 if (x2 >= 84) {
Rubio 0:7f86012fb8b5 131 x2 = 0;
Rubio 0:7f86012fb8b5 132 }
Rubio 0:7f86012fb8b5 133
Rubio 0:7f86012fb8b5 134
Rubio 0:7f86012fb8b5 135
Rubio 0:7f86012fb8b5 136 eninumber enin = eni.enemiesjudge(enic1.x, enic2.x);
Rubio 0:7f86012fb8b5 137 p1 = enin.p1;
Rubio 0:7f86012fb8b5 138 p2 = enin.p2;
Rubio 0:7f86012fb8b5 139 if (p1 == 1){
Rubio 0:7f86012fb8b5 140 enic1 = eni.set_coord1(p1);
Rubio 0:7f86012fb8b5 141 }
Rubio 0:7f86012fb8b5 142 if (p2 == 1){
Rubio 0:7f86012fb8b5 143 enic2 = eni.set_coord1(p2);
Rubio 0:7f86012fb8b5 144 }
Rubio 0:7f86012fb8b5 145 eni.drawenemies_small(enic1.x, enic1.y,lcd);
Rubio 0:7f86012fb8b5 146 eni.drawenemies_large(enic2.x, enic2.y,lcd);
Rubio 0:7f86012fb8b5 147
Rubio 0:7f86012fb8b5 148
Rubio 0:7f86012fb8b5 149 if (explosionjudge(enic1.x,enic1.y,x1,y1)) {
Rubio 0:7f86012fb8b5 150 explosion(x1, y1);
Rubio 0:7f86012fb8b5 151 x1 = 0;
Rubio 0:7f86012fb8b5 152 count = count + 1;
Rubio 0:7f86012fb8b5 153 enic1.x = 0;
Rubio 0:7f86012fb8b5 154 }
Rubio 0:7f86012fb8b5 155 if (explosionjudge(enic2.x,enic2.y,x1,y1)) {
Rubio 0:7f86012fb8b5 156 explosion(x1, y1);
Rubio 0:7f86012fb8b5 157 x1= 0;
Rubio 0:7f86012fb8b5 158 count = count + 1;
Rubio 0:7f86012fb8b5 159 enic2.x = 0;
Rubio 0:7f86012fb8b5 160 }
Rubio 0:7f86012fb8b5 161 if (explosionjudge(enic1.x,enic1.y,x2,y2)) {
Rubio 0:7f86012fb8b5 162 explosion(x1, y1);
Rubio 0:7f86012fb8b5 163 x2 = 0;
Rubio 0:7f86012fb8b5 164 count = count + 1;
Rubio 0:7f86012fb8b5 165 enic1.x = 0;
Rubio 0:7f86012fb8b5 166 }
Rubio 0:7f86012fb8b5 167 if (explosionjudge(enic2.x,enic2.y,x2,y2)) {
Rubio 0:7f86012fb8b5 168 explosion(x2, y2);
Rubio 0:7f86012fb8b5 169 count = count + 1;
Rubio 0:7f86012fb8b5 170 x2 = 0;
Rubio 0:7f86012fb8b5 171 enic2.x = 0;
Rubio 0:7f86012fb8b5 172 }
Rubio 0:7f86012fb8b5 173
Rubio 0:7f86012fb8b5 174
Rubio 0:7f86012fb8b5 175 score = count *50;
Rubio 0:7f86012fb8b5 176
Rubio 0:7f86012fb8b5 177
Rubio 0:7f86012fb8b5 178 if (xp1 <= 4) {
Rubio 0:7f86012fb8b5 179 xp1 = enic1.x;
Rubio 0:7f86012fb8b5 180 yp1 = enic1.y;
Rubio 0:7f86012fb8b5 181 }else if (xp1 > 0) {
Rubio 0:7f86012fb8b5 182 xp1 = eni.open_fire_en(xp1, yp1,lcd);
Rubio 0:7f86012fb8b5 183 }
Rubio 0:7f86012fb8b5 184 if (xp2 <= 4){
Rubio 0:7f86012fb8b5 185 xp2 = enic2.x;
Rubio 0:7f86012fb8b5 186 yp2 = enic2.y;
Rubio 0:7f86012fb8b5 187 }else if (xp2 > 0) {
Rubio 0:7f86012fb8b5 188 xp2 = eni.open_fire_en(xp2, yp2,lcd);
Rubio 0:7f86012fb8b5 189 }
Rubio 0:7f86012fb8b5 190
Rubio 0:7f86012fb8b5 191 if (score > high_score){
Rubio 0:7f86012fb8b5 192 high_score = score;
Rubio 0:7f86012fb8b5 193 }
Rubio 0:7f86012fb8b5 194
Rubio 0:7f86012fb8b5 195 if (explosionjudge(x,y,xp1,yp1)) {
Rubio 0:7f86012fb8b5 196 explosion(xp1, yp1);
Rubio 0:7f86012fb8b5 197 xp1 = 0;
Rubio 0:7f86012fb8b5 198 game_over();
Rubio 0:7f86012fb8b5 199 }
Rubio 0:7f86012fb8b5 200 if (explosionjudge(x,y,xp2,yp2)) {
Rubio 0:7f86012fb8b5 201 explosion(xp2, yp2);
Rubio 0:7f86012fb8b5 202 xp2 = 0;
Rubio 0:7f86012fb8b5 203 game_over();
Rubio 0:7f86012fb8b5 204 }
Rubio 0:7f86012fb8b5 205
Rubio 0:7f86012fb8b5 206
Rubio 0:7f86012fb8b5 207
Rubio 0:7f86012fb8b5 208
Rubio 0:7f86012fb8b5 209 wait(0.1);
Rubio 0:7f86012fb8b5 210 lcd.refresh();
Rubio 0:7f86012fb8b5 211 }
Rubio 0:7f86012fb8b5 212 }
Rubio 0:7f86012fb8b5 213
Rubio 0:7f86012fb8b5 214
Rubio 0:7f86012fb8b5 215
Rubio 0:7f86012fb8b5 216
Rubio 0:7f86012fb8b5 217
Rubio 0:7f86012fb8b5 218
Rubio 0:7f86012fb8b5 219 void init()
Rubio 0:7f86012fb8b5 220 {
Rubio 0:7f86012fb8b5 221 // need to initialise LCD and Gamepad
Rubio 0:7f86012fb8b5 222 pad.init();
Rubio 0:7f86012fb8b5 223
Rubio 0:7f86012fb8b5 224 count = 0;
Rubio 0:7f86012fb8b5 225 score = 0;
Rubio 0:7f86012fb8b5 226 x = 5.0;
Rubio 0:7f86012fb8b5 227 y = 21.0;
Rubio 0:7f86012fb8b5 228 x1 = 0.0;
Rubio 0:7f86012fb8b5 229 y1 = 0.0;
Rubio 0:7f86012fb8b5 230 x2 = 0.0;
Rubio 0:7f86012fb8b5 231 y2 = 0.0;
Rubio 0:7f86012fb8b5 232 v = 4.0;
Rubio 0:7f86012fb8b5 233 p1 = p2 = 0;
Rubio 0:7f86012fb8b5 234 xp1 = xp2 = xn1 =xn2 =0.0;
Rubio 0:7f86012fb8b5 235 yp1 = yp2 = yn1 = yn2 =0.0;
Rubio 0:7f86012fb8b5 236 enic1.x = enic2.x = 0.0;
Rubio 0:7f86012fb8b5 237
Rubio 0:7f86012fb8b5 238 // initialise the game with correct ball and paddle sizes
Rubio 0:7f86012fb8b5 239
Rubio 0:7f86012fb8b5 240 }
Rubio 0:7f86012fb8b5 241
Rubio 0:7f86012fb8b5 242
Rubio 0:7f86012fb8b5 243
Rubio 0:7f86012fb8b5 244 void welcome() {
Rubio 0:7f86012fb8b5 245
Rubio 0:7f86012fb8b5 246 lcd.printString(" HALO WAR ",0,1);
Rubio 0:7f86012fb8b5 247 lcd.printString(" Press Start ",0,4);
Rubio 0:7f86012fb8b5 248 lcd.refresh();
Rubio 0:7f86012fb8b5 249
Rubio 0:7f86012fb8b5 250 // wait flashing LEDs until start button is pressed
Rubio 0:7f86012fb8b5 251 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
Rubio 0:7f86012fb8b5 252 pad.leds_on();
Rubio 0:7f86012fb8b5 253 wait(0.1);
Rubio 0:7f86012fb8b5 254 pad.leds_off();
Rubio 0:7f86012fb8b5 255 wait(0.1);
Rubio 0:7f86012fb8b5 256
Rubio 0:7f86012fb8b5 257 }
Rubio 0:7f86012fb8b5 258 }
Rubio 0:7f86012fb8b5 259
Rubio 0:7f86012fb8b5 260 bool explosionjudge(float x1,float y1,float x2,float y2){
Rubio 0:7f86012fb8b5 261 float d;
Rubio 0:7f86012fb8b5 262 d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
Rubio 0:7f86012fb8b5 263 if (d <= 9){
Rubio 0:7f86012fb8b5 264 return true;
Rubio 0:7f86012fb8b5 265 }else {
Rubio 0:7f86012fb8b5 266 return false;
Rubio 0:7f86012fb8b5 267 }
Rubio 0:7f86012fb8b5 268 }
Rubio 0:7f86012fb8b5 269
Rubio 0:7f86012fb8b5 270 void explosion(int x, int y){
Rubio 0:7f86012fb8b5 271
Rubio 0:7f86012fb8b5 272 lcd.drawSprite(x, y, 10, 10, (int *)explosion123);
Rubio 0:7f86012fb8b5 273 }
Rubio 0:7f86012fb8b5 274
Rubio 0:7f86012fb8b5 275
Rubio 0:7f86012fb8b5 276 void game_over(){
Rubio 0:7f86012fb8b5 277 while(!pad.check_event(Gamepad::START_PRESSED)){
Rubio 0:7f86012fb8b5 278 char buffer1[20];
Rubio 0:7f86012fb8b5 279 sprintf(buffer1, "High Score=%d", high_score);
Rubio 0:7f86012fb8b5 280 lcd.printString(buffer1,0,0);
Rubio 0:7f86012fb8b5 281 char buffer[14];
Rubio 0:7f86012fb8b5 282 sprintf(buffer, " Score = %d ", score);
Rubio 0:7f86012fb8b5 283 lcd.printString(buffer,0,1);
Rubio 0:7f86012fb8b5 284 lcd.printString(" Wasted ",0,2);
Rubio 0:7f86012fb8b5 285 lcd.printString("Press start to",0,3);
Rubio 0:7f86012fb8b5 286 lcd.printString(" new compaign",0,4);
Rubio 0:7f86012fb8b5 287 lcd.refresh();
Rubio 0:7f86012fb8b5 288 lcd.clear();
Rubio 0:7f86012fb8b5 289 }
Rubio 0:7f86012fb8b5 290 init();
Rubio 0:7f86012fb8b5 291 }
Rubio 0:7f86012fb8b5 292
Rubio 0:7f86012fb8b5 293 @endcode
Rubio 0:7f86012fb8b5 294
Rubio 0:7f86012fb8b5 295 */
Rubio 0:7f86012fb8b5 296
Rubio 0:7f86012fb8b5 297 class ship
Rubio 0:7f86012fb8b5 298 {
Rubio 0:7f86012fb8b5 299 public:
Rubio 0:7f86012fb8b5 300 /** Set the ship at given coordinate x and y.
Rubio 0:7f86012fb8b5 301 *
Rubio 0:7f86012fb8b5 302 * @param a The horizontal position of our ship, it will be given a pre-set number to draw the ship in the welcome interface.
Rubio 0:7f86012fb8b5 303 * @param b The vertical position our ship, it will be given apreset number to draw the ship at first.
Rubio 0:7f86012fb8b5 304 *
Rubio 0:7f86012fb8b5 305 */
Rubio 0:7f86012fb8b5 306 void set_ship(int a,int b, N5110 &lcd);
Rubio 0:7f86012fb8b5 307
Rubio 0:7f86012fb8b5 308 /** To set a missile released by the enemies.
Rubio 0:7f86012fb8b5 309 *
Rubio 0:7f86012fb8b5 310 * @param x The horizontal coordinate of the enemy ship that will release missile.
Rubio 0:7f86012fb8b5 311 * @param y The vertical coordinate of the enemy ship that will release missile.
Rubio 0:7f86012fb8b5 312 * @param v The speed of the missile.
Rubio 0:7f86012fb8b5 313 * @returns
Rubio 0:7f86012fb8b5 314 * The last coordinate of the missile.
Rubio 0:7f86012fb8b5 315 * x -The horizontal coordinate of the missile
Rubio 0:7f86012fb8b5 316 *
Rubio 0:7f86012fb8b5 317 */
Rubio 0:7f86012fb8b5 318 int open_fire(int x,int y,int v, N5110 &lcd);
Rubio 0:7f86012fb8b5 319
Rubio 0:7f86012fb8b5 320
Rubio 0:7f86012fb8b5 321
Rubio 0:7f86012fb8b5 322
Rubio 0:7f86012fb8b5 323 private:
Rubio 0:7f86012fb8b5 324 int _a;
Rubio 0:7f86012fb8b5 325 int _b;
Rubio 0:7f86012fb8b5 326 int _x;
Rubio 0:7f86012fb8b5 327 int _y;
Rubio 0:7f86012fb8b5 328 int _v;
Rubio 0:7f86012fb8b5 329 };
Rubio 0:7f86012fb8b5 330