Rex Raj / Mbed 2 deprecated el17rrrs

Dependencies:   mbed Gamepad N5110 mbed-rtos

Committer:
RexRoshan
Date:
Mon May 06 18:29:49 2019 +0000
Revision:
5:016a7315b75d
Parent:
4:4d673fb2d9dc
Child:
6:1fcfd331c047
Commented almost every class

Who changed what in which revision?

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