![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Adventure game written for ECE2035 at the Georgia Institute of Technology
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
Diff: startmenu.cpp
- Revision:
- 5:93a4c396c1af
- Parent:
- 4:cdc54191ff07
diff -r cdc54191ff07 -r 93a4c396c1af startmenu.cpp --- a/startmenu.cpp Tue May 22 19:13:03 2018 +0000 +++ b/startmenu.cpp Sat Oct 26 15:44:26 2019 +0000 @@ -20,28 +20,102 @@ #define GAME_OVER 10 #define FULL_DRAW 11 -/*This initializes the menu. i.e. this is the first drawing on the screen.*/ -Menu::Menu(int num_options){ - - this->num_options = num_options; - this->start = 0; - this->quit = 0; - uLCD.locate(1,2); + +int Menu::get_action(GameInputs inputs){ + if(!inputs.b1){ + return 1; + } + if(!inputs.b2){ + return 2; + } + if (!inputs.b3){ + return BUTTON3; + } + return NO_ACTION; +} + + +int Menu::display(){ + ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); + // Menu loop + while(1) + { + // Timer to measure game update speed + Timer t; t.start(); + + // Actually do the game update: + // 1. Read inputs + GameInputs in = read_inputs(); + // 2. Determine action (get_action) + int a = this->get_action(in); + // 3. Update game (update_game) + int u = this->update(a); + + // 4. Draw frame (draw_game) + this->draw(u); + + //check for start + if (terminate){ + uLCD.filled_rectangle(0, 127, 127, 0, 0x000000); + return terminate; + } + + // 5. Frame delay + t.stop(); + int dt = t.read_ms(); + if (dt < 500) wait_ms(500 - dt); + } +} + +StartMenu::StartMenu() : start(0), quit(0), start_color(BLACK), quit_color(BLACK), third_color(BLACK){ + //draw pink rectangle on whole screen uLCD.filled_rectangle(0, 127, 127, 0, PINK1); + // write title + uLCD.locate(1,2); uLCD.textbackground_color(YELLOW); uLCD.color(BLACK); uLCD.printf("Issa Quest"); - if (current_item%3 == 0){ +} + + +int StartMenu::update(int action){ //this might need to rewritten + + switch(action) + { + case 1: + //if button 1 pressed move to next menu item + button_presses = button_presses++; + break; + case 2: + //if button 2 pressed select menu item + if (button_presses > 0 && button_presses%3 == 0) { //Start game + start = true; + terminate = true; + } else if (button_presses%3 == 1){ //Quit (screen cut to black) + quit = true; + } else { + o_instructions(); + } + break; + } + return NO_RESULT; +} + +void StartMenu::draw(int init){ + + //if statement selects which option will be highlighted + if (button_presses%3 == 0){ start_color = GREEN; - } else if (current_item%3 == 1){ + } else if (button_presses%3 == 1){ quit_color = GREEN; } else{ third_color = GREEN; } + //draws the options on the screen uLCD.locate(1,4); uLCD.color(start_color); uLCD.printf("Start da game"); @@ -53,10 +127,15 @@ uLCD.locate(1,8); uLCD.color(third_color); uLCD.printf("Instructions"); + + start_color = BLACK; + quit_color = BLACK; + third_color = BLACK; } + //Option Definition// -int Menu::o_instructions(){ +int StartMenu::o_instructions(){ uLCD.filled_rectangle(0, 128, 128, 80, PINK1); uLCD.locate(1,4); uLCD.textbackground_color(YELLOW); @@ -80,108 +159,8 @@ } } -int Menu::get_action(GameInputs inputs){ - if(!inputs.b1){ - uLCD.locate(1,2); - char but[5]; - sprintf(but, "%d: %d\n", !inputs.b1, inputs.b1); - uLCD.printf(but); - return 1; - } - if(!inputs.b2){ - return 2; - } - if (!inputs.b3){ - return BUTTON3; - } - return NO_ACTION; -} - -int Menu::update(int action){ //this might need to rewritten - - switch(action) - { - case 1: - //if button 1 pressed move to next menu item - current_item = current_item++; - break; - case 2: - //if button 2 pressed select menu item - //b2presses++; - if (current_item > 0 && current_item%this->num_options == 0) {//Start game - start = true; - } else if (current_item%this->num_options == 1){ //Quit (screen cut to black) - quit = true; - } else { //screen go white - o_instructions(); - } - break; - case MENU_BUTTON: - break; - default: break; - } - return NO_RESULT; +int StartMenu::display(){ + int result = Menu::display(); + return result; } -void Menu::draw(int init){ - if (current_item%this->num_options == 0){ - start_color = GREEN; - } else if (current_item%this->num_options == 1){ - quit_color = GREEN; - } else{ - third_color = GREEN; - } - - uLCD.locate(1,4); - uLCD.color(start_color); - uLCD.printf("Start da game"); - - uLCD.locate(1,6); - uLCD.color(quit_color); - uLCD.printf("Quit?"); - - uLCD.locate(1,8); - uLCD.color(third_color); - uLCD.printf("Instructions"); - - start_color = BLACK; - quit_color = BLACK; - third_color = BLACK; -} - -int Menu::display(){ - ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); - // Menu loop - while(1) - { - // Timer to measure game update speed - Timer t; t.start(); - - // Actually do the game update: - // 1. Read inputs - GameInputs in = read_inputs(); - // 2. Determine action (get_action) - int a = this->get_action(in); - char act[5]; - sprintf(act, "%d", a); - uLCD.printf(act); - // 3. Update game (update_game) - int u = this->update(a); - - // 4. Draw frame (draw_game) - this->draw(u); - - //check for start - if (start){ - uLCD.filled_rectangle(0, 127, 127, 0, 0x000000); - return start; - } - - // 5. Frame delay - t.stop(); - int dt = t.read_ms(); - if (dt < 500) wait_ms(500 - dt); - } -} - -