ELEC2645 (2018/19) / Mbed 2 deprecated el17aj

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:Adam Jones
00006 Username:el17aj
00007 Student ID Number:201147452
00008 Date:17/03/2019
00009 */
00010 
00011 #include "main.h"
00012 
00013 #define CROSSHAIRS_SPEED 3
00014 #define FPS 16
00015 
00016 
00017 int menu_cursor[7][7] =   {
00018     { 0,0,1,1,1,0,0 },
00019     { 0,1,0,0,0,1,0 },
00020     { 1,0,0,1,0,0,1 },
00021     { 1,0,1,1,1,0,1 },
00022     { 1,0,0,1,0,0,1 },
00023     { 0,1,0,0,0,1,0 },
00024     { 0,0,1,1,1,0,0 },
00025 };
00026 
00027 
00028 
00029 /////////////// structs /////////////////
00030 struct UserInput {
00031     float angle;
00032     float mag;
00033 };
00034 
00035 
00036 
00037 /////////////// objects ///////////////
00038 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00039 Gamepad pad;
00040 GameEngine game;
00041 
00042 ///////////// prototypes ///////////////
00043 void init();
00044 void update_game(UserInput input);
00045 void render();
00046 void welcome();
00047 int check_button_press(int option);
00048 void draw_menu_cursor(int option);
00049 void play_game();
00050 void game_over();
00051 
00052 void how_to_play();
00053 void instruction_1();
00054 void instruction_2();
00055 void instruction_3();
00056 
00057 ///////////// functions ////////////////
00058 int main()
00059 {
00060 
00061     int fps = FPS;  // frames per second
00062 
00063     init();     // initialise and then display welcome screen...
00064     welcome();  // creates welcome menu
00065     
00066     render();  // first draw the initial frame 
00067     wait(1.0f/fps);  // and wait for one frame period
00068 
00069 
00070 }
00071 
00072 // initialies all classes and libraries
00073 void init()
00074 {
00075     // need to initialise LCD and Gamepad 
00076     lcd.init();
00077     pad.init();
00078 }
00079 
00080 // this function draws each frame on the LCD
00081 void render()
00082 {
00083     // clear screen, re-draw and refresh
00084     lcd.clear();  
00085     game.draw(lcd);
00086     lcd.refresh();
00087 }
00088 
00089 // menu screen displayed on start-up
00090 void welcome() {
00091     int option = 1; //integer to track the users selected menu item
00092     while (1) {
00093         lcd.clear();
00094         lcd.setContrast(pad.read_pot());    //in menu loop, allow contrast adjustment
00095         
00096         //draw menu on lcd
00097         lcd.printString(" Wall Defence!",0,0); 
00098         lcd.printString("  Play ",0,2);
00099         lcd.printString("  How To Play ",0,4);
00100         lcd.printString("(use Y/A/B) ", 0 , 5);
00101         
00102         //check button press to change or select option 
00103         option = check_button_press(option);
00104         
00105         //draw menu cursor next to new option
00106         draw_menu_cursor(option);
00107         
00108         //refresh the lcd to add new items then wait 1 frame
00109         lcd.refresh();
00110         wait(1/FPS);
00111     }
00112 }
00113 
00114 int check_button_press(int option)
00115 {
00116     if ( pad.check_event(Gamepad::Y_PRESSED) == true) {
00117         option = 1; //play game option
00118     } else if ( pad.check_event(Gamepad::A_PRESSED) == true) {
00119         option = 2; //instructions option
00120     } else if ( pad.check_event(Gamepad::B_PRESSED) == true) {
00121         //pick current option
00122         if (option == 1) {
00123             play_game();
00124         } else if (option == 2) {
00125             how_to_play();
00126         }
00127     }
00128     return option;
00129 }
00130 
00131 void draw_menu_cursor(int option)
00132 {
00133     if (option == 1) {
00134         lcd.drawSprite(0,15,7,7,(int *)menu_cursor);
00135     } else if (option == 2) {
00136         lcd.drawSprite(0,33,7,7,(int *)menu_cursor);
00137     }
00138 }
00139 
00140 void play_game() {
00141     //game initialised
00142     game.init(CROSSHAIRS_SPEED, FPS);
00143     //while game isnt over continue running game loop
00144     while (!game.get_game_over()) {
00145         lcd.setContrast(pad.read_pot());    //allows contrast adjustment throughout game, incase display can not be seen
00146         game.read_input(pad);   
00147         game.update(pad);
00148         render();
00149         wait(1.0f/FPS);
00150     }   
00151     game_over();    //displays game is over
00152 }
00153 
00154 void game_over() {
00155     lcd.clear();
00156     lcd.printString("  GAME OVER!  ",0,2);
00157     pad.tone(100, 1.0);
00158     lcd.refresh();
00159     wait(2.0);
00160 }
00161 
00162 
00163 void how_to_play() {
00164     instruction_1();    //joystick instruction message
00165     instruction_2();    //shoot instruction message
00166     instruction_3();    //games aim message 
00167 }
00168 
00169 void instruction_1()
00170 {
00171     bool contPressed = false;
00172     
00173     lcd.clear();
00174     lcd.printString("Use Joystick",0,0);
00175     lcd.printString("To Aim",0,1);
00176     lcd.printString("(press B)",0,4);
00177     lcd.refresh();
00178     while (contPressed == false) {
00179         wait(0.2);
00180         if ( pad.check_event(Gamepad::B_PRESSED) == true) {
00181             contPressed = true;
00182         }
00183     }
00184 }
00185 
00186 void instruction_2()
00187 {
00188     bool contPressed = false;
00189     
00190     lcd.clear();
00191     lcd.printString("Use B To Shoot",0,0);
00192     lcd.printString("(press B)",0,4);
00193     lcd.refresh();
00194     while (contPressed == false) {
00195         wait(0.2);
00196         if ( pad.check_event(Gamepad::B_PRESSED) == true) {
00197             contPressed = true;
00198         }
00199     }
00200 }
00201 
00202 void instruction_3()
00203 {
00204     bool contPressed = false;
00205     
00206     lcd.clear();
00207     lcd.printString("Protect Your",0,0);
00208     lcd.printString("Wall",0,1);
00209     lcd.printString("(press B)",0,4);
00210     lcd.refresh();
00211     while (contPressed == false) {
00212         wait(0.2);
00213         if ( pad.check_event(Gamepad::B_PRESSED) == true) {
00214             contPressed = true;
00215         }
00216     }
00217 }
00218