Rex Raj / Mbed 2 deprecated el17rrrs

Dependencies:   mbed Gamepad N5110 mbed-rtos

Committer:
RexRoshan
Date:
Sun May 05 17:41:46 2019 +0000
Revision:
4:4d673fb2d9dc
Parent:
3:bf9624e5b0c3
Child:
5:016a7315b75d
Commenting all the code;

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 0:99fa5a619081 82 if (pad.check_event(Gamepad::B_PRESSED) == true){
RexRoshan 0:99fa5a619081 83 currentState = Mission1;
RexRoshan 0:99fa5a619081 84 shoot.restart_game_stage();
RexRoshan 0:99fa5a619081 85 init();
RexRoshan 0:99fa5a619081 86 }
RexRoshan 0:99fa5a619081 87 break;
RexRoshan 0:99fa5a619081 88 case Mission1Pass:
RexRoshan 0:99fa5a619081 89 lcd.clear();
RexRoshan 2:b5c1bb7a39de 90 lcd.drawSprite(0,0,48,84,(int *)mission_pass);
RexRoshan 3:bf9624e5b0c3 91 lcd.drawSprite(53,11,7,19,(int *)m_one);
RexRoshan 0:99fa5a619081 92 lcd.refresh();
RexRoshan 0:99fa5a619081 93 if (pad.check_event(Gamepad::A_PRESSED) == true){
RexRoshan 0:99fa5a619081 94 currentState = Mission2;
RexRoshan 0:99fa5a619081 95 shoot.restart_game_stage();
RexRoshan 0:99fa5a619081 96 }
RexRoshan 0:99fa5a619081 97 break;
RexRoshan 0:99fa5a619081 98 case Mission2:
RexRoshan 0:99fa5a619081 99 shoot.read_input(pad);
RexRoshan 2:b5c1bb7a39de 100 shoot.update_mission_two(pad,lcd);
RexRoshan 0:99fa5a619081 101 render2();
RexRoshan 0:99fa5a619081 102 wait(1.0f/fps);
RexRoshan 0:99fa5a619081 103 if(shoot.get_game_stage() == 3 ){
RexRoshan 0:99fa5a619081 104 currentState = Mission2Fail;
RexRoshan 0:99fa5a619081 105 }
RexRoshan 0:99fa5a619081 106 if(shoot.get_game_stage() == 4){
RexRoshan 0:99fa5a619081 107 currentState = Mission2Pass;
RexRoshan 0:99fa5a619081 108 }
RexRoshan 0:99fa5a619081 109 break;
RexRoshan 0:99fa5a619081 110 case Mission2Fail:
RexRoshan 0:99fa5a619081 111 lcd.clear();
RexRoshan 2:b5c1bb7a39de 112 lcd.drawSprite(6,5,42,73,(int *)mission_fail);
RexRoshan 3:bf9624e5b0c3 113 lcd.drawSprite(53,12,5,18,(int *)m_two);
RexRoshan 0:99fa5a619081 114 lcd.refresh();
RexRoshan 0:99fa5a619081 115 if (pad.check_event(Gamepad::B_PRESSED) == true){
RexRoshan 0:99fa5a619081 116 currentState = Mission1;
RexRoshan 0:99fa5a619081 117 shoot.restart_game_stage();
RexRoshan 0:99fa5a619081 118 init();
RexRoshan 0:99fa5a619081 119 }
RexRoshan 0:99fa5a619081 120 break;
RexRoshan 0:99fa5a619081 121 case Mission2Pass:
RexRoshan 0:99fa5a619081 122 lcd.clear();
RexRoshan 2:b5c1bb7a39de 123 lcd.drawSprite(0,0,48,84,(int *)mission_pass);
RexRoshan 3:bf9624e5b0c3 124 lcd.drawSprite(53,12,5,18,(int *)m_two);
RexRoshan 0:99fa5a619081 125 lcd.refresh();
RexRoshan 1:45493d1d0689 126 if (pad.check_event(Gamepad::A_PRESSED) == true){
RexRoshan 1:45493d1d0689 127 currentState = Mission3;
RexRoshan 1:45493d1d0689 128 shoot.restart_game_stage();
RexRoshan 1:45493d1d0689 129 init();
RexRoshan 1:45493d1d0689 130 }
RexRoshan 0:99fa5a619081 131 break;
RexRoshan 1:45493d1d0689 132 case Mission3:
RexRoshan 1:45493d1d0689 133 shoot.read_input(pad);
RexRoshan 2:b5c1bb7a39de 134 shoot.update_mission_three(pad,lcd);
RexRoshan 1:45493d1d0689 135 render3();
RexRoshan 1:45493d1d0689 136 wait(1.0f/fps);
RexRoshan 1:45493d1d0689 137 if(shoot.get_game_stage() == 5 ){
RexRoshan 1:45493d1d0689 138 currentState = Mission3Fail;
RexRoshan 1:45493d1d0689 139 }
RexRoshan 1:45493d1d0689 140 if(shoot.get_game_stage() == 6){
RexRoshan 1:45493d1d0689 141 currentState = Mission3Pass;
RexRoshan 1:45493d1d0689 142 }
RexRoshan 1:45493d1d0689 143 break;
RexRoshan 1:45493d1d0689 144 case Mission3Fail:
RexRoshan 1:45493d1d0689 145 lcd.clear();
RexRoshan 2:b5c1bb7a39de 146 lcd.drawSprite(6,5,42,73,(int *)mission_fail);
RexRoshan 3:bf9624e5b0c3 147 lcd.drawSprite(51,12,5,29,(int *)m_three);
RexRoshan 1:45493d1d0689 148 lcd.refresh();
RexRoshan 1:45493d1d0689 149 if (pad.check_event(Gamepad::B_PRESSED) == true){
RexRoshan 1:45493d1d0689 150 currentState = Mission1;
RexRoshan 1:45493d1d0689 151 shoot.restart_game_stage();
RexRoshan 1:45493d1d0689 152 init();
RexRoshan 1:45493d1d0689 153 }
RexRoshan 1:45493d1d0689 154 break;
RexRoshan 1:45493d1d0689 155 case Mission3Pass:
RexRoshan 1:45493d1d0689 156 lcd.clear();
RexRoshan 2:b5c1bb7a39de 157 lcd.drawSprite(0,0,48,84,(int *)mission_pass);
RexRoshan 3:bf9624e5b0c3 158 lcd.drawSprite(51,12,5,29,(int *)m_three);
RexRoshan 1:45493d1d0689 159 lcd.refresh();
RexRoshan 1:45493d1d0689 160 if (pad.check_event(Gamepad::A_PRESSED) == true){
RexRoshan 1:45493d1d0689 161 currentState = Congratulations;
RexRoshan 1:45493d1d0689 162 }
RexRoshan 1:45493d1d0689 163 break;
RexRoshan 1:45493d1d0689 164 case Congratulations:
RexRoshan 1:45493d1d0689 165 for(int i = 0; i < 3; i++){
RexRoshan 1:45493d1d0689 166 lcd.clear();
RexRoshan 1:45493d1d0689 167 lcd.drawSprite(0,0,48,84,(int *) congrats);
RexRoshan 1:45493d1d0689 168 lcd.drawSprite(0,23,25,84,(int *)party_popper);
RexRoshan 1:45493d1d0689 169 lcd.refresh();
RexRoshan 1:45493d1d0689 170 wait(0.5);
RexRoshan 1:45493d1d0689 171 lcd.clear();
RexRoshan 1:45493d1d0689 172 lcd.drawSprite(0,0,48,84,(int *) congrats);
RexRoshan 1:45493d1d0689 173 lcd.refresh();
RexRoshan 1:45493d1d0689 174 wait(0.5);
RexRoshan 1:45493d1d0689 175 }
RexRoshan 1:45493d1d0689 176 while(pad.check_event(Gamepad::A_PRESSED) == false){
RexRoshan 1:45493d1d0689 177 lcd.clear();
RexRoshan 1:45493d1d0689 178 lcd.drawSprite(0,0,48,84,(int *) congrats);
RexRoshan 1:45493d1d0689 179 lcd.drawSprite(25,28,13,36,(int *) exit1);
RexRoshan 1:45493d1d0689 180 lcd.refresh();
RexRoshan 1:45493d1d0689 181 if (pad.check_event(Gamepad::A_PRESSED) == true){
RexRoshan 1:45493d1d0689 182 currentState = Intro;
RexRoshan 1:45493d1d0689 183 break;
RexRoshan 1:45493d1d0689 184 }
RexRoshan 1:45493d1d0689 185 }
RexRoshan 1:45493d1d0689 186 break;
RexRoshan 1:45493d1d0689 187 case Intro:
RexRoshan 1:45493d1d0689 188 int fps = 8;
RexRoshan 1:45493d1d0689 189 lcd.setContrast(0.4);
RexRoshan 1:45493d1d0689 190 // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
RexRoshan 1:45493d1d0689 191 init();
RexRoshan 1:45493d1d0689 192 // 0.4 appears to be a good starting point
RexRoshan 1:45493d1d0689 193 thread.start(welcome);
RexRoshan 1:45493d1d0689 194 intro();
RexRoshan 1:45493d1d0689 195 thread.terminate();
RexRoshan 1:45493d1d0689 196 instruction();
RexRoshan 1:45493d1d0689 197 render();
RexRoshan 1:45493d1d0689 198 wait(1.0f/fps);
RexRoshan 1:45493d1d0689 199 currentState = Mission1;
RexRoshan 1:45493d1d0689 200 break;
RexRoshan 0:99fa5a619081 201 }
RexRoshan 0:99fa5a619081 202 }
RexRoshan 0:99fa5a619081 203 }
RexRoshan 0:99fa5a619081 204
RexRoshan 0:99fa5a619081 205
RexRoshan 0:99fa5a619081 206 void init(){
RexRoshan 4:4d673fb2d9dc 207 // need to initialise LCD,Gamepad and the Game
RexRoshan 0:99fa5a619081 208 lcd.init();
RexRoshan 0:99fa5a619081 209 pad.init();
RexRoshan 1:45493d1d0689 210 shoot.init(WIDTH/12,HEIGHT/2-5,12,4,12,10,20,7,3,2,4);
RexRoshan 0:99fa5a619081 211
RexRoshan 0:99fa5a619081 212 }
RexRoshan 0:99fa5a619081 213
RexRoshan 4:4d673fb2d9dc 214
RexRoshan 0:99fa5a619081 215 void render()
RexRoshan 0:99fa5a619081 216 {
RexRoshan 0:99fa5a619081 217 // clear screen, re-draw and refresh
RexRoshan 4:4d673fb2d9dc 218 // Mission 1
RexRoshan 0:99fa5a619081 219 lcd.clear();
RexRoshan 2:b5c1bb7a39de 220 shoot.draw_mission_one(pad,lcd);
RexRoshan 0:99fa5a619081 221 lcd.refresh();
RexRoshan 0:99fa5a619081 222 }
RexRoshan 0:99fa5a619081 223
RexRoshan 0:99fa5a619081 224 void render2()
RexRoshan 0:99fa5a619081 225 {
RexRoshan 0:99fa5a619081 226 // clear screen, re-draw and refresh
RexRoshan 4:4d673fb2d9dc 227 // Mission 2
RexRoshan 0:99fa5a619081 228 lcd.clear();
RexRoshan 2:b5c1bb7a39de 229 shoot.draw_mission_two(pad,lcd);
RexRoshan 0:99fa5a619081 230 lcd.refresh();
RexRoshan 0:99fa5a619081 231 }
RexRoshan 0:99fa5a619081 232
RexRoshan 1:45493d1d0689 233 void render3()
RexRoshan 1:45493d1d0689 234 {
RexRoshan 1:45493d1d0689 235 // clear screen, re-draw and refresh
RexRoshan 4:4d673fb2d9dc 236 // Mission 3
RexRoshan 1:45493d1d0689 237 lcd.clear();
RexRoshan 2:b5c1bb7a39de 238 shoot.draw_mission_three(pad,lcd);
RexRoshan 1:45493d1d0689 239 lcd.refresh();
RexRoshan 1:45493d1d0689 240 }
RexRoshan 1:45493d1d0689 241
RexRoshan 4:4d673fb2d9dc 242 void welcome()
RexRoshan 4:4d673fb2d9dc 243 {
RexRoshan 0:99fa5a619081 244
RexRoshan 4:4d673fb2d9dc 245 // draws the sprites until the Start button is pressed
RexRoshan 4:4d673fb2d9dc 246
RexRoshan 0:99fa5a619081 247 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
RexRoshan 0:99fa5a619081 248 if(pad.check_event(Gamepad::START_PRESSED) == true){break;}
RexRoshan 0:99fa5a619081 249 pad.leds_on();
RexRoshan 2:b5c1bb7a39de 250 lcd.drawSprite(6,0,48,71,(int *)screen);
RexRoshan 0:99fa5a619081 251 lcd.drawSprite(21,43,5,40,(int *)start);
RexRoshan 0:99fa5a619081 252 lcd.refresh();
RexRoshan 0:99fa5a619081 253 Thread::wait(800);
RexRoshan 0:99fa5a619081 254 if(pad.check_event(Gamepad::START_PRESSED) == true){break;}
RexRoshan 0:99fa5a619081 255 pad.leds_off();
RexRoshan 2:b5c1bb7a39de 256 lcd.drawSprite(6,0,48,71,(int *)screen);
RexRoshan 0:99fa5a619081 257 lcd.refresh();
RexRoshan 0:99fa5a619081 258 Thread::wait(800);
RexRoshan 0:99fa5a619081 259 if(pad.check_event(Gamepad::START_PRESSED) == true){break;}
RexRoshan 0:99fa5a619081 260
RexRoshan 0:99fa5a619081 261 }
RexRoshan 0:99fa5a619081 262 pad.leds_off();
RexRoshan 0:99fa5a619081 263 }
RexRoshan 0:99fa5a619081 264
RexRoshan 4:4d673fb2d9dc 265 void intro()
RexRoshan 4:4d673fb2d9dc 266 {
RexRoshan 4:4d673fb2d9dc 267 //plays the introduction music for game
RexRoshan 4:4d673fb2d9dc 268 play.intro_song(pad);
RexRoshan 4:4d673fb2d9dc 269
RexRoshan 4:4d673fb2d9dc 270 }
RexRoshan 0:99fa5a619081 271
RexRoshan 4:4d673fb2d9dc 272 void instruction()
RexRoshan 4:4d673fb2d9dc 273 {
RexRoshan 4:4d673fb2d9dc 274 // clears, draws the instrcution and game rules page and refreshes the page
RexRoshan 0:99fa5a619081 275 pad.leds_off();
RexRoshan 1:45493d1d0689 276 lcd.clear();
RexRoshan 1:45493d1d0689 277 instruct.rules(lcd,pad);
RexRoshan 1:45493d1d0689 278 lcd.refresh();
RexRoshan 1:45493d1d0689 279
RexRoshan 0:99fa5a619081 280
RexRoshan 1:45493d1d0689 281 }
RexRoshan 4:4d673fb2d9dc 282