Carter Montgomery / Mbed 2 deprecated 2035_Final_Project

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers startmenu.cpp Source File

startmenu.cpp

00001 #include "globals.h"
00002 #include "hardware.h"
00003 #include "map.h"
00004 #include "graphics.h"
00005 #include "speech.h"
00006 #include "startmenu.h"
00007 #include <vector>
00008 
00009 //note when you define stuff up here like this the methods of the class cant see it I think
00010 #define NO_ACTION 0
00011 #define BUTTON1 1
00012 #define BUTTON2 2
00013 #define BUTTON3 3
00014 #define MENU_BUTTON 4
00015 #define GO_LEFT 5
00016 #define GO_RIGHT 6
00017 #define GO_UP 7
00018 #define GO_DOWN 8
00019 #define NO_RESULT 9
00020 #define GAME_OVER 10
00021 #define FULL_DRAW 11
00022 
00023 
00024 int Menu::get_action(GameInputs inputs){
00025     if(!inputs.b1){
00026         return 1;
00027     }
00028     if(!inputs.b2){
00029         return 2;
00030     }
00031     if (!inputs.b3){
00032         return BUTTON3;
00033     }
00034     return NO_ACTION;
00035 }
00036 
00037 
00038 int Menu::display(){
00039     ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
00040     // Menu loop
00041     while(1)
00042     {
00043         // Timer to measure game update speed
00044         Timer t; t.start();
00045         
00046         // Actually do the game update:
00047         // 1. Read inputs  
00048             GameInputs in = read_inputs();
00049         // 2. Determine action (get_action) 
00050             int a = this->get_action(in);      
00051         // 3. Update game (update_game)
00052             int u = this->update(a);
00053         
00054         // 4. Draw frame (draw_game)
00055         this->draw(u);
00056         
00057         //check for start
00058         if (terminate){
00059             uLCD.filled_rectangle(0, 127, 127, 0, 0x000000);
00060             return terminate;
00061         }
00062         
00063         // 5. Frame delay
00064         t.stop();
00065         int dt = t.read_ms();
00066         if (dt < 500) wait_ms(500 - dt);
00067     }
00068 }
00069 
00070 StartMenu::StartMenu() : start(0), quit(0), start_color(BLACK), quit_color(BLACK), third_color(BLACK){
00071 
00072     //draw pink rectangle on whole screen
00073     uLCD.filled_rectangle(0, 127, 127, 0, PINK1);
00074 
00075     // write title
00076     uLCD.locate(1,2);
00077     uLCD.textbackground_color(YELLOW);
00078     uLCD.color(BLACK);
00079     uLCD.printf("Issa Quest");
00080     
00081 }
00082 
00083 
00084 int StartMenu::update(int action){ //this might need to rewritten
00085 
00086     switch(action) 
00087     {           
00088         case 1: 
00089             //if button 1 pressed move to next menu item
00090             button_presses = button_presses++;
00091             break;
00092         case 2: 
00093             //if button 2 pressed select menu item
00094             if (button_presses > 0 && button_presses%3 == 0) { //Start game
00095                 start = true;
00096                 terminate = true;
00097             } else if (button_presses%3 == 1){ //Quit (screen cut to black)
00098                 quit = true;
00099             } else { 
00100                 o_instructions();
00101             }
00102             break;
00103     }
00104     return NO_RESULT;
00105 }
00106 
00107 void StartMenu::draw(int init){
00108     
00109     //if statement selects which option will be highlighted
00110     if (button_presses%3 == 0){
00111         start_color = GREEN;
00112     } else if (button_presses%3 == 1){
00113         quit_color = GREEN;
00114     } else{
00115         third_color = GREEN;
00116     }
00117     
00118     //draws the options on the screen 
00119     uLCD.locate(1,4);
00120     uLCD.color(start_color);
00121     uLCD.printf("Start da game");
00122     
00123     uLCD.locate(1,6);
00124     uLCD.color(quit_color);
00125     uLCD.printf("Quit?");
00126     
00127     uLCD.locate(1,8);
00128     uLCD.color(third_color);
00129     uLCD.printf("Instructions");
00130     
00131     start_color = BLACK;
00132     quit_color = BLACK;
00133     third_color = BLACK;
00134 }
00135 
00136 
00137 //Option Definition//
00138 int StartMenu::o_instructions(){
00139     uLCD.filled_rectangle(0, 128, 128, 80, PINK1);
00140     uLCD.locate(1,4);
00141     uLCD.textbackground_color(YELLOW);
00142     uLCD.color(BLACK);
00143     
00144     uLCD.printf("Go into the house and talk to the red man to start your quest. Be careful not to step on the spikes.");
00145     
00146     uLCD.locate(10,15);
00147     uLCD.color(GREEN);
00148     uLCD.printf("Back");
00149     
00150     GameInputs inp = read_inputs();
00151     while(1){
00152         if(!inp.b2){
00153             uLCD.filled_rectangle(0, 128, 128, 30, PINK1); //erase option
00154             wait(1);
00155             return 0;
00156         }
00157         inp = read_inputs();
00158         wait(1);
00159     }
00160 }
00161 
00162 int StartMenu::display(){
00163     int result = Menu::display();
00164     return result;
00165 }
00166