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 /* ELEC2645 Embedded Systems Project
Rubio 0:7f86012fb8b5 2 School of Electronic & Electrical Engineering
Rubio 0:7f86012fb8b5 3 University of Leeds
Rubio 0:7f86012fb8b5 4 Name: Miao Sixiang
Rubio 0:7f86012fb8b5 5 Username: el16sm
Rubio 0:7f86012fb8b5 6 Student ID Number: 201089108
Rubio 0:7f86012fb8b5 7 Date:
Rubio 0:7f86012fb8b5 8 */
Rubio 0:7f86012fb8b5 9
Rubio 0:7f86012fb8b5 10 #include "mbed.h"
Rubio 0:7f86012fb8b5 11 #include "Gamepad.h"
Rubio 0:7f86012fb8b5 12 #include "N5110.h"
Rubio 0:7f86012fb8b5 13 #include "enemies.h"
Rubio 0:7f86012fb8b5 14 #include "math.h"
Rubio 0:7f86012fb8b5 15 #include "ship.h"
Rubio 0:7f86012fb8b5 16 #include "Bitmap.h"
Rubio 0:7f86012fb8b5 17
Rubio 0:7f86012fb8b5 18
Rubio 0:7f86012fb8b5 19 #ifdef WITH_TESTING
Rubio 0:7f86012fb8b5 20 #include "tests.h"
Rubio 0:7f86012fb8b5 21 #endif
Rubio 0:7f86012fb8b5 22
Rubio 0:7f86012fb8b5 23
Rubio 0:7f86012fb8b5 24 DigitalOut r_led(LED_RED);
Rubio 0:7f86012fb8b5 25 DigitalOut g_led(LED_GREEN);
Rubio 0:7f86012fb8b5 26 DigitalOut b_led(LED_BLUE);
Rubio 0:7f86012fb8b5 27 DigitalOut led1(PTA1);
Rubio 0:7f86012fb8b5 28 DigitalOut led2(PTA2);
Rubio 0:7f86012fb8b5 29 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Rubio 0:7f86012fb8b5 30
Rubio 0:7f86012fb8b5 31 Gamepad pad;
Rubio 0:7f86012fb8b5 32 enemies eni;
Rubio 0:7f86012fb8b5 33 ship shi;
Rubio 0:7f86012fb8b5 34
Rubio 0:7f86012fb8b5 35 void init();
Rubio 0:7f86012fb8b5 36 void welcome();
Rubio 0:7f86012fb8b5 37 bool explosionjudge(float x1,float y1,float x2,float y2);
Rubio 0:7f86012fb8b5 38 void explosion(int x, int y);
Rubio 0:7f86012fb8b5 39 void game_over();
Rubio 0:7f86012fb8b5 40
Rubio 0:7f86012fb8b5 41 float xp1,xp2,yp1,yp2,xn1,yn1,xn2,yn2,x,y,x1,y1,x2,y2,v;
Rubio 0:7f86012fb8b5 42 int p1,p2,score,high_score,count;
Rubio 0:7f86012fb8b5 43 Vector2D enic1 = {xn1, yn1};
Rubio 0:7f86012fb8b5 44 Vector2D enic2 = {xn2, yn2};
Rubio 0:7f86012fb8b5 45 const int explosion123[10][10] = {
Rubio 0:7f86012fb8b5 46 { 0,0,0,0,1,0,0,0,0,0 },
Rubio 0:7f86012fb8b5 47 { 0,1,0,1,0,1,0,0,1,0 },
Rubio 0:7f86012fb8b5 48 { 0,1,0,1,0,1,1,1,1,0 },
Rubio 0:7f86012fb8b5 49 { 0,0,1,0,0,0,0,1,0,0 },
Rubio 0:7f86012fb8b5 50 { 0,1,0,0,0,0,0,0,1,0 },
Rubio 0:7f86012fb8b5 51 { 0,1,0,0,0,0,0,0,1,0 },
Rubio 0:7f86012fb8b5 52 { 0,0,1,0,0,0,0,1,0,0 },
Rubio 0:7f86012fb8b5 53 { 0,1,0,1,0,0,1,1,0,0 },
Rubio 0:7f86012fb8b5 54 { 1,0,0,0,1,1,0,0,1,0 },
Rubio 0:7f86012fb8b5 55 { 0,0,0,0,0,0,0,0,0,0 },
Rubio 0:7f86012fb8b5 56 };
Rubio 0:7f86012fb8b5 57
Rubio 0:7f86012fb8b5 58 int main() {
Rubio 0:7f86012fb8b5 59
Rubio 0:7f86012fb8b5 60 //initial all the components
Rubio 0:7f86012fb8b5 61 init();//the lcd is initialed
Rubio 0:7f86012fb8b5 62 lcd.init();
Rubio 0:7f86012fb8b5 63 lcd.normalMode();
Rubio 0:7f86012fb8b5 64 lcd.setContrast(0.5);
Rubio 0:7f86012fb8b5 65
Rubio 0:7f86012fb8b5 66
Rubio 0:7f86012fb8b5 67 count = 0;
Rubio 0:7f86012fb8b5 68 score = 0;
Rubio 0:7f86012fb8b5 69 x = 5.0;
Rubio 0:7f86012fb8b5 70 y = 21.0;
Rubio 0:7f86012fb8b5 71 x1 = 0.0;
Rubio 0:7f86012fb8b5 72 y1 = 0.0;
Rubio 0:7f86012fb8b5 73 x2 = 0.0;
Rubio 0:7f86012fb8b5 74 y2 = 0.0;
Rubio 0:7f86012fb8b5 75 v = 4.0;
Rubio 0:7f86012fb8b5 76 p1 = p2 = 0;
Rubio 0:7f86012fb8b5 77 xp1 = xp2 = xn1 =xn2 =0.0;
Rubio 0:7f86012fb8b5 78 yp1 = yp2 = yn1 = yn2 =0.0;
Rubio 0:7f86012fb8b5 79 enic1.x = enic2.x = 0.0;
Rubio 0:7f86012fb8b5 80
Rubio 0:7f86012fb8b5 81 high_score = 0;
Rubio 0:7f86012fb8b5 82
Rubio 0:7f86012fb8b5 83 shi.set_ship(y,x,lcd);
Rubio 0:7f86012fb8b5 84
Rubio 0:7f86012fb8b5 85 welcome();
Rubio 0:7f86012fb8b5 86 while (1) {
Rubio 0:7f86012fb8b5 87 lcd.clear();
Rubio 0:7f86012fb8b5 88 Vector2D coord = pad.get_mapped_coord();
Rubio 0:7f86012fb8b5 89 x = x + coord.x;
Rubio 0:7f86012fb8b5 90 y = y - coord.y;
Rubio 0:7f86012fb8b5 91 shi.set_ship(y,x,lcd);
Rubio 0:7f86012fb8b5 92
Rubio 0:7f86012fb8b5 93 if (pad.check_event(Gamepad::A_PRESSED)){
Rubio 0:7f86012fb8b5 94 if (x1 == 0){
Rubio 0:7f86012fb8b5 95 x1 = x;
Rubio 0:7f86012fb8b5 96 y1 = y;
Rubio 0:7f86012fb8b5 97 x1 = shi.open_fire(x1,y1,v,lcd);
Rubio 0:7f86012fb8b5 98 if (!x2 ==0){
Rubio 0:7f86012fb8b5 99 x2 = shi.open_fire(x2,y2,v,lcd);
Rubio 0:7f86012fb8b5 100 }
Rubio 0:7f86012fb8b5 101 } else if (x1 > 0 and x2 == 0){
Rubio 0:7f86012fb8b5 102 x2 = x;
Rubio 0:7f86012fb8b5 103 y2 = y;
Rubio 0:7f86012fb8b5 104 x2 = shi.open_fire(x2,y2,v,lcd);
Rubio 0:7f86012fb8b5 105 x1 = shi.open_fire(x1,y1,v,lcd);
Rubio 0:7f86012fb8b5 106 } else if (x1 > 0 and x2 > 0){
Rubio 0:7f86012fb8b5 107 x2 = shi.open_fire(x2,y2,v,lcd);
Rubio 0:7f86012fb8b5 108 x1 = shi.open_fire(x1,y1,v,lcd);
Rubio 0:7f86012fb8b5 109 }
Rubio 0:7f86012fb8b5 110 } else if (x1 > 0 or x2 > 0) {
Rubio 0:7f86012fb8b5 111 if (x1 > 0){
Rubio 0:7f86012fb8b5 112 x1 = shi.open_fire(x1,y1,v,lcd);
Rubio 0:7f86012fb8b5 113 }
Rubio 0:7f86012fb8b5 114 if (x2 > 0){
Rubio 0:7f86012fb8b5 115 x2 = shi.open_fire(x2,y2,v,lcd);
Rubio 0:7f86012fb8b5 116 }
Rubio 0:7f86012fb8b5 117 }
Rubio 0:7f86012fb8b5 118 if (x1 >= 84){
Rubio 0:7f86012fb8b5 119 x1 = 0;
Rubio 0:7f86012fb8b5 120 }
Rubio 0:7f86012fb8b5 121 if (x2 >= 84) {
Rubio 0:7f86012fb8b5 122 x2 = 0;
Rubio 0:7f86012fb8b5 123 }
Rubio 0:7f86012fb8b5 124
Rubio 0:7f86012fb8b5 125
Rubio 0:7f86012fb8b5 126
Rubio 0:7f86012fb8b5 127 eninumber enin = eni.enemiesjudge(enic1.x, enic2.x);
Rubio 0:7f86012fb8b5 128 p1 = enin.p1;
Rubio 0:7f86012fb8b5 129 p2 = enin.p2;
Rubio 0:7f86012fb8b5 130 if (p1 == 1){
Rubio 0:7f86012fb8b5 131 enic1 = eni.set_coord1(p1);
Rubio 0:7f86012fb8b5 132 }
Rubio 0:7f86012fb8b5 133 if (p2 == 1){
Rubio 0:7f86012fb8b5 134 enic2 = eni.set_coord1(p2);
Rubio 0:7f86012fb8b5 135 }
Rubio 0:7f86012fb8b5 136 eni.drawenemies_small(enic1.x, enic1.y,lcd);
Rubio 0:7f86012fb8b5 137 eni.drawenemies_large(enic2.x, enic2.y,lcd);
Rubio 0:7f86012fb8b5 138
Rubio 0:7f86012fb8b5 139
Rubio 0:7f86012fb8b5 140 if (explosionjudge(enic1.x,enic1.y,x1,y1)) {
Rubio 0:7f86012fb8b5 141 explosion(x1, y1);
Rubio 0:7f86012fb8b5 142 x1 = 0;
Rubio 0:7f86012fb8b5 143 count = count + 1;
Rubio 0:7f86012fb8b5 144 enic1.x = 0;
Rubio 0:7f86012fb8b5 145 }
Rubio 0:7f86012fb8b5 146 if (explosionjudge(enic2.x,enic2.y,x1,y1)) {
Rubio 0:7f86012fb8b5 147 explosion(x1, y1);
Rubio 0:7f86012fb8b5 148 x1= 0;
Rubio 0:7f86012fb8b5 149 count = count + 1;
Rubio 0:7f86012fb8b5 150 enic2.x = 0;
Rubio 0:7f86012fb8b5 151 }
Rubio 0:7f86012fb8b5 152 if (explosionjudge(enic1.x,enic1.y,x2,y2)) {
Rubio 0:7f86012fb8b5 153 explosion(x1, y1);
Rubio 0:7f86012fb8b5 154 x2 = 0;
Rubio 0:7f86012fb8b5 155 count = count + 1;
Rubio 0:7f86012fb8b5 156 enic1.x = 0;
Rubio 0:7f86012fb8b5 157 }
Rubio 0:7f86012fb8b5 158 if (explosionjudge(enic2.x,enic2.y,x2,y2)) {
Rubio 0:7f86012fb8b5 159 explosion(x2, y2);
Rubio 0:7f86012fb8b5 160 count = count + 1;
Rubio 0:7f86012fb8b5 161 x2 = 0;
Rubio 0:7f86012fb8b5 162 enic2.x = 0;
Rubio 0:7f86012fb8b5 163 }
Rubio 0:7f86012fb8b5 164
Rubio 0:7f86012fb8b5 165
Rubio 0:7f86012fb8b5 166 score = count *50;
Rubio 0:7f86012fb8b5 167
Rubio 0:7f86012fb8b5 168
Rubio 0:7f86012fb8b5 169 if (xp1 <= 4) {
Rubio 0:7f86012fb8b5 170 xp1 = enic1.x;
Rubio 0:7f86012fb8b5 171 yp1 = enic1.y;
Rubio 0:7f86012fb8b5 172 }else if (xp1 > 0) {
Rubio 0:7f86012fb8b5 173 xp1 = eni.open_fire_en(xp1, yp1,lcd);
Rubio 0:7f86012fb8b5 174 }
Rubio 0:7f86012fb8b5 175 if (xp2 <= 4){
Rubio 0:7f86012fb8b5 176 xp2 = enic2.x;
Rubio 0:7f86012fb8b5 177 yp2 = enic2.y;
Rubio 0:7f86012fb8b5 178 }else if (xp2 > 0) {
Rubio 0:7f86012fb8b5 179 xp2 = eni.open_fire_en(xp2, yp2,lcd);
Rubio 0:7f86012fb8b5 180 }
Rubio 0:7f86012fb8b5 181
Rubio 0:7f86012fb8b5 182 if (score > high_score){
Rubio 0:7f86012fb8b5 183 high_score = score;
Rubio 0:7f86012fb8b5 184 }
Rubio 0:7f86012fb8b5 185
Rubio 0:7f86012fb8b5 186 if (explosionjudge(x,y,xp1,yp1)) {
Rubio 0:7f86012fb8b5 187 explosion(xp1, yp1);
Rubio 0:7f86012fb8b5 188 xp1 = 0;
Rubio 0:7f86012fb8b5 189 game_over();
Rubio 0:7f86012fb8b5 190 }
Rubio 0:7f86012fb8b5 191 if (explosionjudge(x,y,xp2,yp2)) {
Rubio 0:7f86012fb8b5 192 explosion(xp2, yp2);
Rubio 0:7f86012fb8b5 193 xp2 = 0;
Rubio 0:7f86012fb8b5 194 game_over();
Rubio 0:7f86012fb8b5 195 }
Rubio 0:7f86012fb8b5 196
Rubio 0:7f86012fb8b5 197
Rubio 0:7f86012fb8b5 198
Rubio 0:7f86012fb8b5 199
Rubio 0:7f86012fb8b5 200 wait(0.1);
Rubio 0:7f86012fb8b5 201 lcd.refresh();
Rubio 0:7f86012fb8b5 202 }
Rubio 0:7f86012fb8b5 203 }
Rubio 0:7f86012fb8b5 204
Rubio 0:7f86012fb8b5 205
Rubio 0:7f86012fb8b5 206
Rubio 0:7f86012fb8b5 207
Rubio 0:7f86012fb8b5 208
Rubio 0:7f86012fb8b5 209
Rubio 0:7f86012fb8b5 210 void init()
Rubio 0:7f86012fb8b5 211 {
Rubio 0:7f86012fb8b5 212 // need to initialise LCD and Gamepad
Rubio 0:7f86012fb8b5 213 pad.init();
Rubio 0:7f86012fb8b5 214
Rubio 0:7f86012fb8b5 215 count = 0;
Rubio 0:7f86012fb8b5 216 score = 0;
Rubio 0:7f86012fb8b5 217 x = 5.0;
Rubio 0:7f86012fb8b5 218 y = 21.0;
Rubio 0:7f86012fb8b5 219 x1 = 0.0;
Rubio 0:7f86012fb8b5 220 y1 = 0.0;
Rubio 0:7f86012fb8b5 221 x2 = 0.0;
Rubio 0:7f86012fb8b5 222 y2 = 0.0;
Rubio 0:7f86012fb8b5 223 v = 4.0;
Rubio 0:7f86012fb8b5 224 p1 = p2 = 0;
Rubio 0:7f86012fb8b5 225 xp1 = xp2 = xn1 =xn2 =0.0;
Rubio 0:7f86012fb8b5 226 yp1 = yp2 = yn1 = yn2 =0.0;
Rubio 0:7f86012fb8b5 227 enic1.x = enic2.x = 0.0;
Rubio 0:7f86012fb8b5 228
Rubio 0:7f86012fb8b5 229 // initialise the game with correct ball and paddle sizes
Rubio 0:7f86012fb8b5 230
Rubio 0:7f86012fb8b5 231 }
Rubio 0:7f86012fb8b5 232
Rubio 0:7f86012fb8b5 233
Rubio 0:7f86012fb8b5 234
Rubio 0:7f86012fb8b5 235 void welcome() {
Rubio 0:7f86012fb8b5 236
Rubio 0:7f86012fb8b5 237 lcd.printString(" HALO WAR ",0,1);
Rubio 0:7f86012fb8b5 238 lcd.printString(" Press Start ",0,4);
Rubio 0:7f86012fb8b5 239 lcd.refresh();
Rubio 0:7f86012fb8b5 240
Rubio 0:7f86012fb8b5 241 // wait flashing LEDs until start button is pressed
Rubio 0:7f86012fb8b5 242 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
Rubio 0:7f86012fb8b5 243 pad.leds_on();
Rubio 0:7f86012fb8b5 244 wait(0.1);
Rubio 0:7f86012fb8b5 245 pad.leds_off();
Rubio 0:7f86012fb8b5 246 wait(0.1);
Rubio 0:7f86012fb8b5 247
Rubio 0:7f86012fb8b5 248 }
Rubio 0:7f86012fb8b5 249 }
Rubio 0:7f86012fb8b5 250
Rubio 0:7f86012fb8b5 251 bool explosionjudge(float x1,float y1,float x2,float y2){
Rubio 0:7f86012fb8b5 252 float d;
Rubio 0:7f86012fb8b5 253 d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
Rubio 0:7f86012fb8b5 254 if (d <= 9){
Rubio 0:7f86012fb8b5 255 return true;
Rubio 0:7f86012fb8b5 256 }else {
Rubio 0:7f86012fb8b5 257 return false;
Rubio 0:7f86012fb8b5 258 }
Rubio 0:7f86012fb8b5 259 }
Rubio 0:7f86012fb8b5 260
Rubio 0:7f86012fb8b5 261 void explosion(int x, int y){
Rubio 0:7f86012fb8b5 262
Rubio 0:7f86012fb8b5 263 lcd.drawSprite(x, y, 10, 10, (int *)explosion123);
Rubio 0:7f86012fb8b5 264 }
Rubio 0:7f86012fb8b5 265
Rubio 0:7f86012fb8b5 266
Rubio 0:7f86012fb8b5 267 void game_over(){
Rubio 0:7f86012fb8b5 268 while(!pad.check_event(Gamepad::START_PRESSED)){
Rubio 0:7f86012fb8b5 269 char buffer1[20];
Rubio 0:7f86012fb8b5 270 sprintf(buffer1, "High Score=%d", high_score);
Rubio 0:7f86012fb8b5 271 lcd.printString(buffer1,0,0);
Rubio 0:7f86012fb8b5 272 char buffer[14];
Rubio 0:7f86012fb8b5 273 sprintf(buffer, " Score = %d ", score);
Rubio 0:7f86012fb8b5 274 lcd.printString(buffer,0,1);
Rubio 0:7f86012fb8b5 275 lcd.printString(" Wasted ",0,2);
Rubio 0:7f86012fb8b5 276 lcd.printString("Press start to",0,3);
Rubio 0:7f86012fb8b5 277 lcd.printString(" new compaign",0,4);
Rubio 0:7f86012fb8b5 278 lcd.refresh();
Rubio 0:7f86012fb8b5 279 lcd.clear();
Rubio 0:7f86012fb8b5 280 }
Rubio 0:7f86012fb8b5 281 init();
Rubio 0:7f86012fb8b5 282 }