Reham Faqehi / Mbed 2 deprecated fy15raf

Dependencies:   mbed

Fork of fy15raf by ELEC2645 (2017/18)

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 ELEC2645 Embedded Systems Project
00003 School of Electronic & Electrical Engineering
00004 University of Leeds
00005 Name: Reham Faqehi
00006 Username: Fy15raf
00007 Student ID Number: 200982112
00008 Date: 03/05/2018
00009 */
00010 
00011 
00012 ///////// pre-processor directives ////////
00013 #include "mbed.h"
00014 #include "Gamepad.h"
00015 #include "N5110.h"
00016 #include "GameEngine.h"
00017 #include "tests.h"
00018 /////////////// objects ///////////////
00019 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00020 Gamepad g_pad;
00021 GameEngine game;
00022 
00023 ///////////// prototypes ///////////////
00024 void init();
00025 void render();
00026 void welcome();
00027 void gameOver();
00028 
00029 ///////////// functions ////////////////
00030 int main()
00031 {
00032     //game will excute if all tests passed
00033 #ifdef ALL_TESTS_H
00034     int failures = Run_tests();
00035 
00036     if(failures > 0) return failures;
00037 #endif
00038 
00039     int fps = 8;  // frames per second
00040     int sleep=0;
00041     init();     // initialise
00042     welcome();  // display welcome screen till the user press start to start the game
00043     
00044     //debugging
00045     printf("\n....THE GAME STARTED :)..... \n\n");
00046 
00047     render();  // drawing the initial frame
00048     wait(1.0f/fps);  // wait for one frame period
00049 
00050 
00051     // game loop executes
00052     while (1) {
00053         
00054 
00055         // green LEDs turn on and red LEDs turn off
00056         g_pad.led(1, 0);
00057         g_pad.led(4, 0);
00058         g_pad.led(3, 2);
00059         g_pad.led(6, 2);
00060 
00061         game.read_input(g_pad);
00062         game.update(g_pad);
00063         render();
00064         wait(1.0f/fps);
00065 
00066         while(g_pad.check_event(Gamepad::BACK_PRESSED) == true || sleep ==1) {
00067 
00068             printf("Waiting to contienue.....\n");
00069 
00070             sleep=1;
00071             // green LEDs turn off and red LED turn on
00072             g_pad.led(1, 2);
00073             g_pad.led(4, 2);
00074             g_pad.led(3, 0);
00075             g_pad.led(6, 0);
00076             //stop the game seconds
00077             game.time_stop();
00078             //sleep();
00079             //check if start button has been pressed to continue playing
00080             if(g_pad.check_event(Gamepad::START_PRESSED) == true) {
00081                 sleep=0;
00082                 break;
00083             }
00084         }
00085         if (game.check_gameOver()==1) { // if check_gameOver() function return 1 gameOver screen will be shown 
00086             //debugging
00087             printf("\n\n ....End of the game :)..... \n\n");
00088 
00089             lcd.clear();
00090             gameOver(); //game over screen will be shown with travelling period that has been reached 
00091             
00092             // reset to play again 
00093             game.reset_gameOver();
00094             init(); 
00095             render();
00096             
00097             //debugging
00098             printf("\n\n ....playing the game again :)..... \n\n");
00099             
00100         }
00101     }
00102 
00103 }
00104 
00105 // initialies all classes and libraries
00106 void init()
00107 {
00108     //initialise LCD and Gamepad first
00109     lcd.init();
00110     lcd.setContrast(0.5);
00111     g_pad.init();
00112     // initialise the game
00113     game.init();
00114 }
00115 
00116 //function to draw the frames on the LCD
00117 void render()
00118 {
00119     // first clear screen, re-draw and then refresh the LCD
00120     lcd.clear();
00121     game.draw(lcd);
00122     lcd.refresh();
00123 }
00124 
00125 // simple welcoming screen before starting the game
00126 void welcome()
00127 {
00128 
00129     lcd.printString("   Spaceship!    ",0,1);
00130     lcd.printString("   Press Start ",0,4);
00131     lcd.refresh();
00132 
00133     //flashing LEDs until start button is pressed
00134     while ( g_pad.check_event(Gamepad::START_PRESSED) == false) {
00135         g_pad.leds_on();
00136         wait(0.1);
00137         g_pad.leds_off();
00138         wait(0.1);
00139     }
00140 }
00141 
00142 // game over screen after the end of the game
00143 void gameOver()
00144 {
00145     //game over secreen will be shown until start button is pressed
00146     while ( g_pad.check_event(Gamepad::START_PRESSED) == false) {
00147     lcd.printString("  Game Over!! ",0,1);
00148     //print the maximum recorded seconds
00149     game.print_travel_time(lcd);
00150     lcd.refresh();
00151     }
00152 
00153 }
00154 
00155 
00156 
00157