Rex Raj / Mbed 2 deprecated el17rrrs

Dependencies:   mbed Gamepad N5110 mbed-rtos

Committer:
RexRoshan
Date:
Wed May 08 10:03:24 2019 +0000
Revision:
6:1fcfd331c047
Parent:
5:016a7315b75d
Child:
7:574c66ebd8b0
Documentation has been completed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RexRoshan 6:1fcfd331c047 1 /*
RexRoshan 6:1fcfd331c047 2 ELEC2645 Embedded Systems Project
RexRoshan 6:1fcfd331c047 3 School of Electronic & Electrical Engineering
RexRoshan 6:1fcfd331c047 4 University of Leeds
RexRoshan 6:1fcfd331c047 5 Name: Rex Roshan Raj
RexRoshan 6:1fcfd331c047 6 Username: el17rrs
RexRoshan 6:1fcfd331c047 7 Student ID Number: 201184290
RexRoshan 6:1fcfd331c047 8 Start Date: 25/03/2019
RexRoshan 6:1fcfd331c047 9 End Date: 8/05/2019
RexRoshan 6:1fcfd331c047 10 */
RexRoshan 6:1fcfd331c047 11
RexRoshan 0:99fa5a619081 12 #include "mbed.h"
RexRoshan 0:99fa5a619081 13 #include "N5110.h"
RexRoshan 0:99fa5a619081 14 #include "Gamepad.h"
RexRoshan 0:99fa5a619081 15 #include "Instruction.h"
RexRoshan 0:99fa5a619081 16 #include "Spacecraft.h"
RexRoshan 0:99fa5a619081 17 #include "GameEngine.h"
RexRoshan 0:99fa5a619081 18 #include "Solar.h"
RexRoshan 0:99fa5a619081 19 #include "Music.h"
RexRoshan 6:1fcfd331c047 20 #include "rtos.h"
RexRoshan 0:99fa5a619081 21
RexRoshan 6:1fcfd331c047 22 /** @file main.cpp
RexRoshan 6:1fcfd331c047 23 * @brief Contains the main code of the program.
RexRoshan 6:1fcfd331c047 24 */
RexRoshan 6:1fcfd331c047 25
RexRoshan 6:1fcfd331c047 26 // Structs
RexRoshan 0:99fa5a619081 27 struct UserInput {
RexRoshan 0:99fa5a619081 28 Direction d;
RexRoshan 0:99fa5a619081 29 float mag;
RexRoshan 0:99fa5a619081 30 };
RexRoshan 0:99fa5a619081 31
RexRoshan 4:4d673fb2d9dc 32 // enum state for instruction page
RexRoshan 4:4d673fb2d9dc 33 enum States { Life1,Life2,Life3,Life4,Life5,Life6,Life7,Life8,Life9,Life10,Life11 };
RexRoshan 4:4d673fb2d9dc 34 // enum state for Mission state
RexRoshan 1:45493d1d0689 35 enum MissionState {Mission1,Mission1Pass,Mission1Fail,Mission2,Mission2Pass,Mission2Fail,Mission3,Mission3Pass,Mission3Fail,Congratulations,Intro };
RexRoshan 0:99fa5a619081 36
RexRoshan 6:1fcfd331c047 37 // Objects
RexRoshan 0:99fa5a619081 38 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3
RexRoshan 0:99fa5a619081 39 Gamepad pad;
RexRoshan 0:99fa5a619081 40 Music play;
RexRoshan 1:45493d1d0689 41 Instruction instruct;
RexRoshan 0:99fa5a619081 42 Thread thread;
RexRoshan 0:99fa5a619081 43 Spacecraft game;
RexRoshan 0:99fa5a619081 44 GameEngine shoot;
RexRoshan 0:99fa5a619081 45
RexRoshan 6:1fcfd331c047 46 // Prototypes
RexRoshan 0:99fa5a619081 47 void init();
RexRoshan 0:99fa5a619081 48 void welcome();
RexRoshan 1:45493d1d0689 49 void rend();
RexRoshan 0:99fa5a619081 50 void render();
RexRoshan 0:99fa5a619081 51 void render2();
RexRoshan 1:45493d1d0689 52 void render3();
RexRoshan 0:99fa5a619081 53 void intro();
RexRoshan 0:99fa5a619081 54 void instruction();
RexRoshan 0:99fa5a619081 55
RexRoshan 0:99fa5a619081 56
RexRoshan 0:99fa5a619081 57 int main()
RexRoshan 0:99fa5a619081 58 {
RexRoshan 6:1fcfd331c047 59 MissionState currentState = Mission3;
RexRoshan 0:99fa5a619081 60 // first need to initialise display
RexRoshan 0:99fa5a619081 61 int fps = 8;
RexRoshan 4:4d673fb2d9dc 62 // setting the contrast at 0.4 as it appears to be a good starting point
RexRoshan 0:99fa5a619081 63 lcd.setContrast(0.4);
RexRoshan 0:99fa5a619081 64 // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
RexRoshan 0:99fa5a619081 65 init();
RexRoshan 4:4d673fb2d9dc 66 //runs welcome loop and intro loop in parallel
RexRoshan 3:bf9624e5b0c3 67 thread.start(welcome);
RexRoshan 3:bf9624e5b0c3 68 intro();
RexRoshan 3:bf9624e5b0c3 69 thread.terminate();
RexRoshan 4:4d673fb2d9dc 70 // instruction page for the game
RexRoshan 0:99fa5a619081 71 instruction();
RexRoshan 4:4d673fb2d9dc 72 // clears the lcd
RexRoshan 0:99fa5a619081 73 render();
RexRoshan 1:45493d1d0689 74 wait(1.0f/fps);
RexRoshan 0:99fa5a619081 75
RexRoshan 1:45493d1d0689 76
RexRoshan 1:45493d1d0689 77 while(1) {
RexRoshan 0:99fa5a619081 78 switch(currentState){
RexRoshan 0:99fa5a619081 79 case Mission1:
RexRoshan 0:99fa5a619081 80 shoot.read_input(pad);
RexRoshan 2:b5c1bb7a39de 81 shoot.update_mission_one(pad,lcd);
RexRoshan 0:99fa5a619081 82 render();
RexRoshan 0:99fa5a619081 83 wait(1.0f/fps);
RexRoshan 0:99fa5a619081 84 if(shoot.get_game_stage() == 1){
RexRoshan 0:99fa5a619081 85 currentState = Mission1Fail;
RexRoshan 0:99fa5a619081 86 break;
RexRoshan 0:99fa5a619081 87 }
RexRoshan 0:99fa5a619081 88 else if(shoot.get_game_stage() == 2){
RexRoshan 0:99fa5a619081 89 currentState = Mission1Pass;
RexRoshan 0:99fa5a619081 90 break;
RexRoshan 0:99fa5a619081 91 } else {
RexRoshan 0:99fa5a619081 92 currentState = Mission1;
RexRoshan 0:99fa5a619081 93 }
RexRoshan 0:99fa5a619081 94 break;
RexRoshan 0:99fa5a619081 95 case Mission1Fail:
RexRoshan 0:99fa5a619081 96 lcd.clear();
RexRoshan 2:b5c1bb7a39de 97 lcd.drawSprite(6,5,42,73,(int *)mission_fail);
RexRoshan 6:1fcfd331c047 98 lcd.drawSprite(51,11,7,19,(int *)m_one);
RexRoshan 0:99fa5a619081 99 lcd.refresh();
RexRoshan 6:1fcfd331c047 100 play.mission_fail(pad);
RexRoshan 0:99fa5a619081 101 if (pad.check_event(Gamepad::B_PRESSED) == true){
RexRoshan 0:99fa5a619081 102 currentState = Mission1;
RexRoshan 0:99fa5a619081 103 shoot.restart_game_stage();
RexRoshan 0:99fa5a619081 104 init();
RexRoshan 0:99fa5a619081 105 }
RexRoshan 0:99fa5a619081 106 break;
RexRoshan 0:99fa5a619081 107 case Mission1Pass:
RexRoshan 0:99fa5a619081 108 lcd.clear();
RexRoshan 2:b5c1bb7a39de 109 lcd.drawSprite(0,0,48,84,(int *)mission_pass);
RexRoshan 6:1fcfd331c047 110 lcd.drawSprite(51,11,7,19,(int *)m_one);
RexRoshan 0:99fa5a619081 111 lcd.refresh();
RexRoshan 6:1fcfd331c047 112 play.mission_success(pad);
RexRoshan 0:99fa5a619081 113 if (pad.check_event(Gamepad::A_PRESSED) == true){
RexRoshan 0:99fa5a619081 114 currentState = Mission2;
RexRoshan 0:99fa5a619081 115 shoot.restart_game_stage();
RexRoshan 6:1fcfd331c047 116 init();
RexRoshan 0:99fa5a619081 117 }
RexRoshan 0:99fa5a619081 118 break;
RexRoshan 0:99fa5a619081 119 case Mission2:
RexRoshan 0:99fa5a619081 120 shoot.read_input(pad);
RexRoshan 2:b5c1bb7a39de 121 shoot.update_mission_two(pad,lcd);
RexRoshan 0:99fa5a619081 122 render2();
RexRoshan 0:99fa5a619081 123 wait(1.0f/fps);
RexRoshan 0:99fa5a619081 124 if(shoot.get_game_stage() == 3 ){
RexRoshan 0:99fa5a619081 125 currentState = Mission2Fail;
RexRoshan 0:99fa5a619081 126 }
RexRoshan 0:99fa5a619081 127 if(shoot.get_game_stage() == 4){
RexRoshan 0:99fa5a619081 128 currentState = Mission2Pass;
RexRoshan 0:99fa5a619081 129 }
RexRoshan 0:99fa5a619081 130 break;
RexRoshan 0:99fa5a619081 131 case Mission2Fail:
RexRoshan 0:99fa5a619081 132 lcd.clear();
RexRoshan 2:b5c1bb7a39de 133 lcd.drawSprite(6,5,42,73,(int *)mission_fail);
RexRoshan 6:1fcfd331c047 134 lcd.drawSprite(51,12,5,18,(int *)m_two);
RexRoshan 0:99fa5a619081 135 lcd.refresh();
RexRoshan 6:1fcfd331c047 136 play.mission_fail(pad);
RexRoshan 0:99fa5a619081 137 if (pad.check_event(Gamepad::B_PRESSED) == true){
RexRoshan 0:99fa5a619081 138 currentState = Mission1;
RexRoshan 0:99fa5a619081 139 shoot.restart_game_stage();
RexRoshan 0:99fa5a619081 140 init();
RexRoshan 0:99fa5a619081 141 }
RexRoshan 0:99fa5a619081 142 break;
RexRoshan 0:99fa5a619081 143 case Mission2Pass:
RexRoshan 0:99fa5a619081 144 lcd.clear();
RexRoshan 2:b5c1bb7a39de 145 lcd.drawSprite(0,0,48,84,(int *)mission_pass);
RexRoshan 6:1fcfd331c047 146 lcd.drawSprite(51,12,5,18,(int *)m_two);
RexRoshan 0:99fa5a619081 147 lcd.refresh();
RexRoshan 6:1fcfd331c047 148 play.mission_success(pad);
RexRoshan 1:45493d1d0689 149 if (pad.check_event(Gamepad::A_PRESSED) == true){
RexRoshan 1:45493d1d0689 150 currentState = Mission3;
RexRoshan 1:45493d1d0689 151 shoot.restart_game_stage();
RexRoshan 1:45493d1d0689 152 init();
RexRoshan 1:45493d1d0689 153 }
RexRoshan 0:99fa5a619081 154 break;
RexRoshan 1:45493d1d0689 155 case Mission3:
RexRoshan 1:45493d1d0689 156 shoot.read_input(pad);
RexRoshan 2:b5c1bb7a39de 157 shoot.update_mission_three(pad,lcd);
RexRoshan 1:45493d1d0689 158 render3();
RexRoshan 1:45493d1d0689 159 wait(1.0f/fps);
RexRoshan 1:45493d1d0689 160 if(shoot.get_game_stage() == 5 ){
RexRoshan 1:45493d1d0689 161 currentState = Mission3Fail;
RexRoshan 1:45493d1d0689 162 }
RexRoshan 1:45493d1d0689 163 if(shoot.get_game_stage() == 6){
RexRoshan 1:45493d1d0689 164 currentState = Mission3Pass;
RexRoshan 1:45493d1d0689 165 }
RexRoshan 1:45493d1d0689 166 break;
RexRoshan 1:45493d1d0689 167 case Mission3Fail:
RexRoshan 1:45493d1d0689 168 lcd.clear();
RexRoshan 2:b5c1bb7a39de 169 lcd.drawSprite(6,5,42,73,(int *)mission_fail);
RexRoshan 6:1fcfd331c047 170 lcd.drawSprite(48,12,5,29,(int *)m_three);
RexRoshan 1:45493d1d0689 171 lcd.refresh();
RexRoshan 6:1fcfd331c047 172 play.mission_fail(pad);
RexRoshan 1:45493d1d0689 173 if (pad.check_event(Gamepad::B_PRESSED) == true){
RexRoshan 1:45493d1d0689 174 currentState = Mission1;
RexRoshan 1:45493d1d0689 175 shoot.restart_game_stage();
RexRoshan 1:45493d1d0689 176 init();
RexRoshan 1:45493d1d0689 177 }
RexRoshan 1:45493d1d0689 178 break;
RexRoshan 1:45493d1d0689 179 case Mission3Pass:
RexRoshan 1:45493d1d0689 180 lcd.clear();
RexRoshan 2:b5c1bb7a39de 181 lcd.drawSprite(0,0,48,84,(int *)mission_pass);
RexRoshan 6:1fcfd331c047 182 lcd.drawSprite(48,12,5,29,(int *)m_three);
RexRoshan 1:45493d1d0689 183 lcd.refresh();
RexRoshan 6:1fcfd331c047 184 play.mission_success(pad);
RexRoshan 1:45493d1d0689 185 if (pad.check_event(Gamepad::A_PRESSED) == true){
RexRoshan 1:45493d1d0689 186 currentState = Congratulations;
RexRoshan 1:45493d1d0689 187 }
RexRoshan 1:45493d1d0689 188 break;
RexRoshan 1:45493d1d0689 189 case Congratulations:
RexRoshan 1:45493d1d0689 190 for(int i = 0; i < 3; i++){
RexRoshan 1:45493d1d0689 191 lcd.clear();
RexRoshan 1:45493d1d0689 192 lcd.drawSprite(0,0,48,84,(int *) congrats);
RexRoshan 1:45493d1d0689 193 lcd.drawSprite(0,23,25,84,(int *)party_popper);
RexRoshan 1:45493d1d0689 194 lcd.refresh();
RexRoshan 1:45493d1d0689 195 wait(0.5);
RexRoshan 1:45493d1d0689 196 lcd.clear();
RexRoshan 1:45493d1d0689 197 lcd.drawSprite(0,0,48,84,(int *) congrats);
RexRoshan 1:45493d1d0689 198 lcd.refresh();
RexRoshan 1:45493d1d0689 199 wait(0.5);
RexRoshan 1:45493d1d0689 200 }
RexRoshan 1:45493d1d0689 201 while(pad.check_event(Gamepad::A_PRESSED) == false){
RexRoshan 1:45493d1d0689 202 lcd.clear();
RexRoshan 1:45493d1d0689 203 lcd.drawSprite(0,0,48,84,(int *) congrats);
RexRoshan 1:45493d1d0689 204 lcd.drawSprite(25,28,13,36,(int *) exit1);
RexRoshan 1:45493d1d0689 205 lcd.refresh();
RexRoshan 1:45493d1d0689 206 if (pad.check_event(Gamepad::A_PRESSED) == true){
RexRoshan 1:45493d1d0689 207 currentState = Intro;
RexRoshan 1:45493d1d0689 208 break;
RexRoshan 1:45493d1d0689 209 }
RexRoshan 1:45493d1d0689 210 }
RexRoshan 1:45493d1d0689 211 break;
RexRoshan 1:45493d1d0689 212 case Intro:
RexRoshan 1:45493d1d0689 213 int fps = 8;
RexRoshan 6:1fcfd331c047 214 lcd.setContrast(0.4); // 0.4 appears to be a good starting point
RexRoshan 1:45493d1d0689 215 init();
RexRoshan 1:45493d1d0689 216 thread.start(welcome);
RexRoshan 1:45493d1d0689 217 intro();
RexRoshan 1:45493d1d0689 218 thread.terminate();
RexRoshan 1:45493d1d0689 219 instruction();
RexRoshan 1:45493d1d0689 220 render();
RexRoshan 1:45493d1d0689 221 wait(1.0f/fps);
RexRoshan 1:45493d1d0689 222 currentState = Mission1;
RexRoshan 1:45493d1d0689 223 break;
RexRoshan 0:99fa5a619081 224 }
RexRoshan 0:99fa5a619081 225 }
RexRoshan 0:99fa5a619081 226 }
RexRoshan 0:99fa5a619081 227
RexRoshan 0:99fa5a619081 228
RexRoshan 0:99fa5a619081 229 void init(){
RexRoshan 4:4d673fb2d9dc 230 // need to initialise LCD,Gamepad and the Game
RexRoshan 0:99fa5a619081 231 lcd.init();
RexRoshan 0:99fa5a619081 232 pad.init();
RexRoshan 6:1fcfd331c047 233 //Initialise (x pos of enemy stage 1,y pos of enemy stage 1, x pos of enemy1 stage 2, y pos of enemy1 stage 2, x pos of enemy2 stage 2, y pos of enemy2 stage 2, x pos of boss stage 3, y pos of boss stage 3, beam size 1, beam size 2, motion speed)
RexRoshan 6:1fcfd331c047 234 shoot.init(WIDTH/12,HEIGHT/2-5,16,4,12,10,20,7,3,2,4);
RexRoshan 0:99fa5a619081 235
RexRoshan 0:99fa5a619081 236 }
RexRoshan 0:99fa5a619081 237
RexRoshan 4:4d673fb2d9dc 238
RexRoshan 0:99fa5a619081 239 void render()
RexRoshan 0:99fa5a619081 240 {
RexRoshan 0:99fa5a619081 241 // clear screen, re-draw and refresh
RexRoshan 4:4d673fb2d9dc 242 // Mission 1
RexRoshan 0:99fa5a619081 243 lcd.clear();
RexRoshan 2:b5c1bb7a39de 244 shoot.draw_mission_one(pad,lcd);
RexRoshan 0:99fa5a619081 245 lcd.refresh();
RexRoshan 0:99fa5a619081 246 }
RexRoshan 0:99fa5a619081 247
RexRoshan 0:99fa5a619081 248 void render2()
RexRoshan 0:99fa5a619081 249 {
RexRoshan 0:99fa5a619081 250 // clear screen, re-draw and refresh
RexRoshan 4:4d673fb2d9dc 251 // Mission 2
RexRoshan 0:99fa5a619081 252 lcd.clear();
RexRoshan 2:b5c1bb7a39de 253 shoot.draw_mission_two(pad,lcd);
RexRoshan 0:99fa5a619081 254 lcd.refresh();
RexRoshan 0:99fa5a619081 255 }
RexRoshan 0:99fa5a619081 256
RexRoshan 1:45493d1d0689 257 void render3()
RexRoshan 1:45493d1d0689 258 {
RexRoshan 1:45493d1d0689 259 // clear screen, re-draw and refresh
RexRoshan 4:4d673fb2d9dc 260 // Mission 3
RexRoshan 1:45493d1d0689 261 lcd.clear();
RexRoshan 2:b5c1bb7a39de 262 shoot.draw_mission_three(pad,lcd);
RexRoshan 1:45493d1d0689 263 lcd.refresh();
RexRoshan 1:45493d1d0689 264 }
RexRoshan 1:45493d1d0689 265
RexRoshan 4:4d673fb2d9dc 266 void welcome()
RexRoshan 4:4d673fb2d9dc 267 {
RexRoshan 0:99fa5a619081 268
RexRoshan 4:4d673fb2d9dc 269 // draws the sprites until the Start button is pressed
RexRoshan 4:4d673fb2d9dc 270
RexRoshan 0:99fa5a619081 271 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
RexRoshan 0:99fa5a619081 272 if(pad.check_event(Gamepad::START_PRESSED) == true){break;}
RexRoshan 0:99fa5a619081 273 pad.leds_on();
RexRoshan 2:b5c1bb7a39de 274 lcd.drawSprite(6,0,48,71,(int *)screen);
RexRoshan 0:99fa5a619081 275 lcd.drawSprite(21,43,5,40,(int *)start);
RexRoshan 0:99fa5a619081 276 lcd.refresh();
RexRoshan 0:99fa5a619081 277 Thread::wait(800);
RexRoshan 0:99fa5a619081 278 if(pad.check_event(Gamepad::START_PRESSED) == true){break;}
RexRoshan 0:99fa5a619081 279 pad.leds_off();
RexRoshan 2:b5c1bb7a39de 280 lcd.drawSprite(6,0,48,71,(int *)screen);
RexRoshan 0:99fa5a619081 281 lcd.refresh();
RexRoshan 0:99fa5a619081 282 Thread::wait(800);
RexRoshan 0:99fa5a619081 283 if(pad.check_event(Gamepad::START_PRESSED) == true){break;}
RexRoshan 0:99fa5a619081 284
RexRoshan 0:99fa5a619081 285 }
RexRoshan 0:99fa5a619081 286 pad.leds_off();
RexRoshan 0:99fa5a619081 287 }
RexRoshan 0:99fa5a619081 288
RexRoshan 4:4d673fb2d9dc 289 void intro()
RexRoshan 4:4d673fb2d9dc 290 {
RexRoshan 4:4d673fb2d9dc 291 //plays the introduction music for game
RexRoshan 4:4d673fb2d9dc 292 play.intro_song(pad);
RexRoshan 4:4d673fb2d9dc 293
RexRoshan 4:4d673fb2d9dc 294 }
RexRoshan 0:99fa5a619081 295
RexRoshan 4:4d673fb2d9dc 296 void instruction()
RexRoshan 4:4d673fb2d9dc 297 {
RexRoshan 4:4d673fb2d9dc 298 // clears, draws the instrcution and game rules page and refreshes the page
RexRoshan 0:99fa5a619081 299 pad.leds_off();
RexRoshan 1:45493d1d0689 300 lcd.clear();
RexRoshan 1:45493d1d0689 301 instruct.rules(lcd,pad);
RexRoshan 1:45493d1d0689 302 lcd.refresh();
RexRoshan 1:45493d1d0689 303
RexRoshan 0:99fa5a619081 304
RexRoshan 1:45493d1d0689 305 }
RexRoshan 4:4d673fb2d9dc 306