ELEC2645 (2018/19) / Mbed 2 deprecated el17arm

Dependencies:   mbed

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: Andrew Milner
00006 Username: el17arm
00007 Student ID Number: 201162219
00008 Date: 18/03/19
00009 */
00010 
00011 ///////// pre-processor directives //////////
00012 
00013 
00014 #include "mbed.h"
00015 #include "N5110.h"
00016 #include "Gamepad.h"
00017 #include "Gameengine.h"
00018 
00019 #define WITH_TESTING
00020 
00021 #ifdef WITH_TESTING
00022 #include "tests.h"
00023 #endif
00024 
00025 
00026 /////////////// objects ///////////////
00027 
00028 Gameengine game;
00029 Gamepad pad;
00030 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00031 AnalogIn pot0(PTB2);
00032 BusOut oxygen(PTA1, PTA2, PTC2);
00033 BusOut lives(PTC3, PTC4, PTD3);
00034 
00035 ///////////// prototypes ///////////////
00036 
00037 /**
00038   * @brief sets the contrast of lcd screen
00039   * @details contrast is adjust with analog input PTB2
00040   */
00041 void contrast();
00042 /** Initialises all classes and libraries and default game settings
00043   * @details initialises LCD and gamepad library and sets screen brightness
00044   */
00045 void init();
00046 /** Displays opening screen
00047   * @details Screen displays title of game and instructs player to press start to begin
00048   */
00049 void start_screen();
00050 /** Controls LEDs on front panel.
00051   * @details Every time a player dies a RHS LED is turned off, as time reduces
00052   * LEDs turn off on LHS.
00053   */
00054 void leds();
00055 /** Initialises and renders all level objects
00056   * @details Contains functions for all levels and will display next level objects
00057   * once player has completed each level
00058   */
00059 void render();
00060 /** Displays screen if game over or game complete
00061   * @details Screen will display player's final score and display game over or game complete
00062   */
00063 void restart();
00064 /** Displays options screen
00065   * @details gives player controls for game
00066   */
00067 void option_screen();
00068 
00069 bool options_flag = false;
00070 
00071 ///////////// functions ////////////////
00072 
00073 int main()
00074 {   
00075     #ifdef WITH_TESTING
00076     run_tests(lcd);
00077     #endif
00078     
00079     init();     // initialises screen and default game settings
00080     start_screen();     // waits for user to press start to begin
00081     option_screen(); // displays options screen
00082 
00083     // game loop, reads input and updates game accordingly
00084     while (1) {
00085 
00086         contrast();     // adjusts contrast of screen
00087         render();       // draws all level objects on to screen
00088         game.update(lcd, pad); // updates movement, collected keys, lives, time left
00089         restart();      // restart screen if game over or end screen if game complete displays final score
00090         wait(0.1);      // wait function to control fps
00091     }
00092 }
00093 
00094 
00095 //initialises all classes and libraries and default game settings
00096 void init()
00097 {
00098     lcd.init();
00099     lcd.normalMode();      // normal colour mode
00100     lcd.refresh();
00101     lcd.setBrightness(1.0);
00102     pad.init();
00103     game.game_init();
00104 }
00105 
00106 // detects which level is being played and draws the level objects with collision settings
00107 void render()
00108 {
00109     game.draw_l1(lcd, pad); // level 1
00110     game.draw_l2(lcd, pad); // level 2
00111     game.draw_l3(lcd, pad); // level 3
00112     game.draw_l4(lcd, pad); // level 4
00113     leds();                 // function to update leds to reflect time and lives left
00114 }
00115 
00116 // controls contrast of the screen
00117 void contrast()
00118 {
00119     lcd.refresh();
00120     float con = pot0.read();
00121     lcd.setContrast(con);
00122     lcd.clear();
00123 }
00124 
00125 // displays start screen and waits for user to press start
00126 void start_screen()
00127 {
00128     while (pad.check_event(Gamepad::START_PRESSED) == false && options_flag == false) {
00129         lcd.clear();
00130         lcd.drawSprite(2,20,8,3,(int *)miner_right);
00131         lcd.drawSprite(79,20,8,3,(int *)miner_left);
00132         lcd.drawRect(0,0,84,48, FILL_TRANSPARENT);
00133         lcd.printString("MANIC MILNER",7,1);
00134         lcd.printString("Press start!",8,2);
00135         lcd.printString("A = Controls",6,4);
00136         contrast();
00137         //printf("start screen = %d \n",options_flag);
00138         if (pad.check_event(Gamepad::A_PRESSED) == true) {
00139             options_flag = true;
00140             //printf("options = %d \n",options_flag);
00141         }
00142     }
00143 }
00144 // displays options screen
00145 void option_screen()
00146 {
00147     while (options_flag == true) {
00148         lcd.clear();
00149         lcd.printString("Stick = L/R",0,0);
00150         lcd.printString("A = Jump ",0,1);
00151         lcd.printString("Get keys and",0,2);
00152         lcd.printString("return to exit",0,3);
00153         lcd.printString("Led=time/lives",0,4);
00154         lcd.printString("<< Press back",0,5);
00155         //printf("options screen = %d \n",options);
00156         lcd.refresh();
00157         if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
00158             options_flag = false;
00159             start_screen();
00160         }
00161     }
00162 }
00163 
00164 // displays appropriate screen with final score for game over and game complete
00165 void restart()
00166 {
00167     if (game.game_over() == true) {
00168         lcd.clear();
00169         game.get_score(lcd);
00170         lcd.drawRect(0,0,84,48, FILL_TRANSPARENT);
00171         lcd.printString("Game Over! ",14,1);
00172         lcd.printString("Score ",18,2);
00173         lcd.printString("Back = Replay",3,4);
00174     }
00175     if (game.game_complete(lcd) == true) {
00176         lcd.clear();
00177         game.get_score(lcd);
00178         lcd.printString("YOU WIN!!! ",12,1);
00179         lcd.printString("Score ",18,2);
00180         lcd.printString("Well done!!!",8,4);
00181     }
00182     if(pad.check_event(Gamepad::BACK_PRESSED) == true) {
00183         init();
00184     }
00185 }
00186 
00187 // leds indicate how many lives and time left
00188 void leds()
00189 {
00190     // fsm to display lives left
00191     int l_leds[4]  = {0b111,0b110,0b100,0b000};
00192     //fsm to display how much time (oxygen) left
00193     int r_leds[4]  = {0b111,0b110,0b100,0b000};
00194 
00195     // function keeps track of time elapsed and changes leds accordingly
00196     oxygen = l_leds[game.oxygen_leds()];
00197     // function keeps track of lives left and changes leds accordingly
00198     lives = r_leds[game.lives_leds()];
00199 }