Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Menus/Menus.cpp@22:9e9685856ce1, 2019-05-09 (annotated)
- Committer:
- el17mcd
- Date:
- Thu May 09 13:45:05 2019 +0000
- Revision:
- 22:9e9685856ce1
- Parent:
- 21:44e87d88afe2
I have read and understood the university's guidelines on plagiarism.; I here by declare that all work is my own and no one else's.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17mcd | 15:fa5282fcd134 | 1 | /* Menus.cpp |
el17mcd | 15:fa5282fcd134 | 2 | Governs all the Menus |
el17mcd | 15:fa5282fcd134 | 3 | and outside-of-game articles. |
el17mcd | 15:fa5282fcd134 | 4 | 13.4.19 |
el17mcd | 15:fa5282fcd134 | 5 | */ |
el17mcd | 15:fa5282fcd134 | 6 | #include "Menus.h" |
el17mcd | 15:fa5282fcd134 | 7 | |
el17mcd | 15:fa5282fcd134 | 8 | Menus::Menus() |
el17mcd | 15:fa5282fcd134 | 9 | { |
el17mcd | 17:cb39d9fa08dc | 10 | _health = 6; |
el17mcd | 17:cb39d9fa08dc | 11 | _mute = false; |
el17mcd | 17:cb39d9fa08dc | 12 | _contrast = 0.4; |
el17mcd | 15:fa5282fcd134 | 13 | } |
el17mcd | 15:fa5282fcd134 | 14 | |
el17mcd | 15:fa5282fcd134 | 15 | Menus::~Menus() |
el17mcd | 15:fa5282fcd134 | 16 | { |
el17mcd | 15:fa5282fcd134 | 17 | |
el17mcd | 15:fa5282fcd134 | 18 | } |
el17mcd | 15:fa5282fcd134 | 19 | |
el17mcd | 15:fa5282fcd134 | 20 | void Menus::start_up_screen(Graphics &graphics, N5110 &lcd, Gamepad &pad) |
el17mcd | 15:fa5282fcd134 | 21 | { |
el17mcd | 17:cb39d9fa08dc | 22 | while(pad.check_event(Gamepad::START_PRESSED) == false) { |
el17mcd | 17:cb39d9fa08dc | 23 | _read_inputs(pad); |
el17mcd | 17:cb39d9fa08dc | 24 | lcd.clear(); |
el17mcd | 21:44e87d88afe2 | 25 | graphics.draw_start_up_screen(lcd); |
el17mcd | 17:cb39d9fa08dc | 26 | lcd.printString(" Press Start ",0,3); |
el17mcd | 17:cb39d9fa08dc | 27 | _start_up_visuals(graphics, lcd, pad); |
el17mcd | 17:cb39d9fa08dc | 28 | lcd.drawRect(0, 0, 84 , 48, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 29 | lcd.drawRect(1, 1, 82 , 46, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 30 | lcd.refresh(); |
el17mcd | 17:cb39d9fa08dc | 31 | } |
el17mcd | 15:fa5282fcd134 | 32 | } |
el17mcd | 15:fa5282fcd134 | 33 | |
el17mcd | 17:cb39d9fa08dc | 34 | void Menus::main_menu(Graphics &graphics, N5110 &lcd, Gamepad &pad, |
el17mcd | 17:cb39d9fa08dc | 35 | Scores &scores) |
el17mcd | 15:fa5282fcd134 | 36 | { |
el17mcd | 17:cb39d9fa08dc | 37 | while(pad.check_event(Gamepad::Y_PRESSED) == false) { |
el17mcd | 17:cb39d9fa08dc | 38 | pad.leds_off(); |
el17mcd | 17:cb39d9fa08dc | 39 | lcd.clear(); |
el17mcd | 17:cb39d9fa08dc | 40 | _main_menu_controls(lcd, pad, scores); |
el17mcd | 17:cb39d9fa08dc | 41 | lcd.printString(" (Y) Play",0,1); |
el17mcd | 17:cb39d9fa08dc | 42 | lcd.printString(" (X) Controls",0,2); |
el17mcd | 17:cb39d9fa08dc | 43 | lcd.printString(" (A) Settings",0,3); |
el17mcd | 17:cb39d9fa08dc | 44 | lcd.printString(" (B) Scores",0,4); |
el17mcd | 17:cb39d9fa08dc | 45 | lcd.drawRect(0, 0, 84 , 48, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 46 | lcd.drawRect(1, 1, 82 , 46, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 47 | lcd.refresh(); |
el17mcd | 17:cb39d9fa08dc | 48 | wait_ms(1000/60); |
el17mcd | 17:cb39d9fa08dc | 49 | } |
el17mcd | 17:cb39d9fa08dc | 50 | } |
el17mcd | 17:cb39d9fa08dc | 51 | |
el17mcd | 22:9e9685856ce1 | 52 | void Menus::_main_menu_controls(N5110 &lcd, Gamepad &pad, Scores &scores) |
el17mcd | 17:cb39d9fa08dc | 53 | { |
el17mcd | 20:3c58ae38d6bc | 54 | pad.check_event(Gamepad::BACK_PRESSED); |
el17mcd | 20:3c58ae38d6bc | 55 | if (pad.check_event(Gamepad::X_PRESSED) == true) { |
el17mcd | 20:3c58ae38d6bc | 56 | _controls_screen(lcd, pad); |
el17mcd | 20:3c58ae38d6bc | 57 | } |
el17mcd | 20:3c58ae38d6bc | 58 | else if (pad.check_event(Gamepad::A_PRESSED) == true) { |
el17mcd | 20:3c58ae38d6bc | 59 | _settings_screen(lcd, pad); |
el17mcd | 20:3c58ae38d6bc | 60 | } |
el17mcd | 20:3c58ae38d6bc | 61 | else if (pad.check_event(Gamepad::B_PRESSED) == true) { |
el17mcd | 20:3c58ae38d6bc | 62 | _scores_screen(lcd, pad, scores); |
el17mcd | 17:cb39d9fa08dc | 63 | } |
el17mcd | 15:fa5282fcd134 | 64 | } |
el17mcd | 15:fa5282fcd134 | 65 | |
el17mcd | 22:9e9685856ce1 | 66 | void Menus::_start_up_visuals(Graphics &graphics, N5110 &lcd, Gamepad &pad) // Draw start up visuals |
el17mcd | 15:fa5282fcd134 | 67 | { |
el17mcd | 17:cb39d9fa08dc | 68 | if (_counter >= 0) { |
el17mcd | 17:cb39d9fa08dc | 69 | graphics.start_up(1, pad); |
el17mcd | 17:cb39d9fa08dc | 70 | graphics.draw_tank_l(10, 5, lcd); |
el17mcd | 17:cb39d9fa08dc | 71 | graphics.draw_turret_l(10, 5, 10, lcd); |
el17mcd | 17:cb39d9fa08dc | 72 | graphics.draw_tank_r(64, 5, lcd); |
el17mcd | 17:cb39d9fa08dc | 73 | graphics.draw_turret_r(64, 5, 180, lcd); |
el17mcd | 17:cb39d9fa08dc | 74 | } else { |
el17mcd | 17:cb39d9fa08dc | 75 | graphics.start_up(0, pad); |
el17mcd | 17:cb39d9fa08dc | 76 | graphics.draw_tank_l(10, 5, lcd); |
el17mcd | 17:cb39d9fa08dc | 77 | graphics.draw_turret_l(10, 5, 180, lcd); |
el17mcd | 17:cb39d9fa08dc | 78 | graphics.draw_tank_r(64, 5, lcd); |
el17mcd | 17:cb39d9fa08dc | 79 | graphics.draw_turret_r(64, 5, 340, lcd); |
el17mcd | 17:cb39d9fa08dc | 80 | } |
el17mcd | 17:cb39d9fa08dc | 81 | if (_counter >= 199) { |
el17mcd | 17:cb39d9fa08dc | 82 | _counter = -200; |
el17mcd | 17:cb39d9fa08dc | 83 | } |
el17mcd | 17:cb39d9fa08dc | 84 | _counter++; |
el17mcd | 15:fa5282fcd134 | 85 | } |
el17mcd | 15:fa5282fcd134 | 86 | |
el17mcd | 22:9e9685856ce1 | 87 | void Menus::_read_inputs(Gamepad &pad) // Clear the triggers |
el17mcd | 15:fa5282fcd134 | 88 | { |
el17mcd | 17:cb39d9fa08dc | 89 | pad.check_event(Gamepad::Y_PRESSED); |
el17mcd | 17:cb39d9fa08dc | 90 | pad.check_event(Gamepad::X_PRESSED); |
el17mcd | 17:cb39d9fa08dc | 91 | pad.check_event(Gamepad::A_PRESSED); |
el17mcd | 17:cb39d9fa08dc | 92 | pad.check_event(Gamepad::B_PRESSED); |
el17mcd | 15:fa5282fcd134 | 93 | } |
el17mcd | 15:fa5282fcd134 | 94 | |
el17mcd | 17:cb39d9fa08dc | 95 | void Menus::_controls_screen(N5110 &lcd, Gamepad &pad) |
el17mcd | 15:fa5282fcd134 | 96 | { |
el17mcd | 17:cb39d9fa08dc | 97 | while(pad.check_event(Gamepad::BACK_PRESSED) == false) { |
el17mcd | 17:cb39d9fa08dc | 98 | _read_inputs(pad); |
el17mcd | 17:cb39d9fa08dc | 99 | lcd.clear(); |
el17mcd | 17:cb39d9fa08dc | 100 | lcd.printString(" Stick: Aim",0,1); |
el17mcd | 17:cb39d9fa08dc | 101 | lcd.printString(" A : Fire",0,2); |
el17mcd | 17:cb39d9fa08dc | 102 | lcd.printString(" L/R : Move",0,3); |
el17mcd | 17:cb39d9fa08dc | 103 | lcd.printString(" Pot : Power",0,4); |
el17mcd | 17:cb39d9fa08dc | 104 | lcd.drawRect(0, 0, 84 , 48, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 105 | lcd.drawRect(1, 1, 82 , 46, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 106 | lcd.refresh(); |
el17mcd | 17:cb39d9fa08dc | 107 | wait_ms(1000/60); |
el17mcd | 17:cb39d9fa08dc | 108 | } |
el17mcd | 15:fa5282fcd134 | 109 | } |
el17mcd | 15:fa5282fcd134 | 110 | |
el17mcd | 17:cb39d9fa08dc | 111 | void Menus::_settings_screen(N5110 &lcd, Gamepad &pad) |
el17mcd | 15:fa5282fcd134 | 112 | { |
el17mcd | 17:cb39d9fa08dc | 113 | while(pad.check_event(Gamepad::BACK_PRESSED) == false) { |
el17mcd | 17:cb39d9fa08dc | 114 | lcd.clear(); |
el17mcd | 17:cb39d9fa08dc | 115 | _mute_brightness_lives(lcd, pad); |
el17mcd | 17:cb39d9fa08dc | 116 | _read_inputs(pad); |
el17mcd | 17:cb39d9fa08dc | 117 | lcd.printString("(O) Health:",4,1); |
el17mcd | 17:cb39d9fa08dc | 118 | lcd.printString("(A) Mute:",4,2); |
el17mcd | 17:cb39d9fa08dc | 119 | lcd.printString("(B) Brightness",4,3); |
el17mcd | 17:cb39d9fa08dc | 120 | lcd.printString("tune with pot",4,4); |
el17mcd | 17:cb39d9fa08dc | 121 | lcd.drawRect(0, 0, 84 , 48, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 122 | lcd.drawRect(1, 1, 82 , 46, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 123 | if (_mute == true ) { |
el17mcd | 17:cb39d9fa08dc | 124 | lcd.drawRect(70, 17, 7 , 7, FILL_BLACK); |
el17mcd | 17:cb39d9fa08dc | 125 | } else { |
el17mcd | 17:cb39d9fa08dc | 126 | lcd.drawRect(70, 17, 7 , 7, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 127 | } |
el17mcd | 17:cb39d9fa08dc | 128 | lcd.refresh(); |
el17mcd | 17:cb39d9fa08dc | 129 | wait_ms(1000/60); |
el17mcd | 17:cb39d9fa08dc | 130 | } |
el17mcd | 15:fa5282fcd134 | 131 | } |
el17mcd | 15:fa5282fcd134 | 132 | |
el17mcd | 16:a2c945279b79 | 133 | void Menus::_mute_brightness_lives(N5110 &lcd, Gamepad &pad) |
el17mcd | 15:fa5282fcd134 | 134 | { |
el17mcd | 17:cb39d9fa08dc | 135 | _counter--; |
el17mcd | 17:cb39d9fa08dc | 136 | if (_counter < 0) { // counter stops multiple consecutive button clicks |
el17mcd | 17:cb39d9fa08dc | 137 | _counter = 10; |
el17mcd | 17:cb39d9fa08dc | 138 | if (pad.get_direction() == W && _health > 1) { |
el17mcd | 17:cb39d9fa08dc | 139 | _health-- ; |
el17mcd | 17:cb39d9fa08dc | 140 | } else if (pad.get_direction() == E && _health < 6) { |
el17mcd | 17:cb39d9fa08dc | 141 | _health++ ; |
el17mcd | 17:cb39d9fa08dc | 142 | } else if (pad.check_event(Gamepad::A_PRESSED) == true) { |
el17mcd | 17:cb39d9fa08dc | 143 | _mute = !_mute; |
el17mcd | 17:cb39d9fa08dc | 144 | } else if (pad.check_event(Gamepad::B_PRESSED) == true) { |
el17mcd | 17:cb39d9fa08dc | 145 | lcd.setBrightness(pad.read_pot()); |
el17mcd | 17:cb39d9fa08dc | 146 | } else { |
el17mcd | 22:9e9685856ce1 | 147 | _counter = -1; //reset counter |
el17mcd | 17:cb39d9fa08dc | 148 | } |
el17mcd | 17:cb39d9fa08dc | 149 | } |
el17mcd | 17:cb39d9fa08dc | 150 | char buffer[14]; |
el17mcd | 17:cb39d9fa08dc | 151 | sprintf(buffer,"%d",_health); |
el17mcd | 17:cb39d9fa08dc | 152 | lcd.printString(buffer, 74, 1); |
el17mcd | 15:fa5282fcd134 | 153 | } |
el17mcd | 15:fa5282fcd134 | 154 | |
el17mcd | 17:cb39d9fa08dc | 155 | void Menus::_scores_screen(N5110 &lcd, Gamepad &pad, Scores &scores) |
el17mcd | 16:a2c945279b79 | 156 | { |
el17mcd | 17:cb39d9fa08dc | 157 | while(pad.check_event(Gamepad::BACK_PRESSED) == false) { |
el17mcd | 17:cb39d9fa08dc | 158 | lcd.clear(); |
el17mcd | 17:cb39d9fa08dc | 159 | scores.display_top_scores(lcd); |
el17mcd | 17:cb39d9fa08dc | 160 | lcd.printString("High Scores", 10, 1); |
el17mcd | 17:cb39d9fa08dc | 161 | lcd.drawRect(0, 0, 84 , 48, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 162 | lcd.drawRect(1, 1, 82 , 46, FILL_TRANSPARENT); |
el17mcd | 17:cb39d9fa08dc | 163 | _read_inputs(pad); |
el17mcd | 17:cb39d9fa08dc | 164 | lcd.refresh(); |
el17mcd | 17:cb39d9fa08dc | 165 | wait_ms(1000/60); |
el17mcd | 17:cb39d9fa08dc | 166 | } |
el17mcd | 16:a2c945279b79 | 167 | } |
el17mcd | 16:a2c945279b79 | 168 | |
el17mcd | 15:fa5282fcd134 | 169 | bool Menus::get_mute() |
el17mcd | 15:fa5282fcd134 | 170 | { |
el17mcd | 17:cb39d9fa08dc | 171 | return _mute; |
el17mcd | 15:fa5282fcd134 | 172 | } |
el17mcd | 15:fa5282fcd134 | 173 | |
el17mcd | 16:a2c945279b79 | 174 | int Menus::get_health() |
el17mcd | 15:fa5282fcd134 | 175 | { |
el17mcd | 17:cb39d9fa08dc | 176 | return _health; |
el17mcd | 15:fa5282fcd134 | 177 | } |