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@17:cb39d9fa08dc, 2019-04-27 (annotated)
- Committer:
- el17mcd
- Date:
- Sat Apr 27 17:43:46 2019 +0000
- Revision:
- 17:cb39d9fa08dc
- Parent:
- 16:a2c945279b79
- Child:
- 20:3c58ae38d6bc
! Stylistic changes made to code so it conforms to google c++ guide. Beginning of inline commenting of source code.
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 | 17:cb39d9fa08dc | 25 | lcd.printString(" TANKS! ",0,1); | 
| 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 | 17:cb39d9fa08dc | 52 | void Menus::_main_menu_controls(N5110 &lcd, Gamepad &pad, Scores &scores) | 
| el17mcd | 17:cb39d9fa08dc | 53 | { | 
| el17mcd | 17:cb39d9fa08dc | 54 | while(pad.check_event(Gamepad::Y_PRESSED) == false) { | 
| el17mcd | 17:cb39d9fa08dc | 55 | pad.check_event(Gamepad::BACK_PRESSED); | 
| el17mcd | 17:cb39d9fa08dc | 56 | if (pad.check_event(Gamepad::X_PRESSED) == true) { | 
| el17mcd | 17:cb39d9fa08dc | 57 | _controls_screen(lcd, pad); | 
| el17mcd | 15:fa5282fcd134 | 58 | } | 
| el17mcd | 17:cb39d9fa08dc | 59 | else if (pad.check_event(Gamepad::A_PRESSED) == true) { | 
| el17mcd | 17:cb39d9fa08dc | 60 | _settings_screen(lcd, pad); | 
| el17mcd | 17:cb39d9fa08dc | 61 | } | 
| el17mcd | 17:cb39d9fa08dc | 62 | else if (pad.check_event(Gamepad::B_PRESSED) == true) { | 
| el17mcd | 17:cb39d9fa08dc | 63 | _scores_screen(lcd, pad, scores); | 
| el17mcd | 17:cb39d9fa08dc | 64 | } | 
| el17mcd | 17:cb39d9fa08dc | 65 | } | 
| el17mcd | 15:fa5282fcd134 | 66 | } | 
| el17mcd | 15:fa5282fcd134 | 67 | |
| el17mcd | 15:fa5282fcd134 | 68 | void Menus::_start_up_visuals(Graphics &graphics, N5110 &lcd, Gamepad &pad) | 
| el17mcd | 15:fa5282fcd134 | 69 | { | 
| el17mcd | 17:cb39d9fa08dc | 70 | if (_counter >= 0) { | 
| el17mcd | 17:cb39d9fa08dc | 71 | graphics.start_up(1, pad); | 
| el17mcd | 17:cb39d9fa08dc | 72 | graphics.draw_tank_l(10, 5, lcd); | 
| el17mcd | 17:cb39d9fa08dc | 73 | graphics.draw_turret_l(10, 5, 10, lcd); | 
| el17mcd | 17:cb39d9fa08dc | 74 | graphics.draw_tank_r(64, 5, lcd); | 
| el17mcd | 17:cb39d9fa08dc | 75 | graphics.draw_turret_r(64, 5, 180, lcd); | 
| el17mcd | 17:cb39d9fa08dc | 76 | } else { | 
| el17mcd | 17:cb39d9fa08dc | 77 | graphics.start_up(0, pad); | 
| el17mcd | 17:cb39d9fa08dc | 78 | graphics.draw_tank_l(10, 5, lcd); | 
| el17mcd | 17:cb39d9fa08dc | 79 | graphics.draw_turret_l(10, 5, 180, lcd); | 
| el17mcd | 17:cb39d9fa08dc | 80 | graphics.draw_tank_r(64, 5, lcd); | 
| el17mcd | 17:cb39d9fa08dc | 81 | graphics.draw_turret_r(64, 5, 340, lcd); | 
| el17mcd | 17:cb39d9fa08dc | 82 | } | 
| el17mcd | 17:cb39d9fa08dc | 83 | if (_counter >= 199) { | 
| el17mcd | 17:cb39d9fa08dc | 84 | _counter = -200; | 
| el17mcd | 17:cb39d9fa08dc | 85 | } | 
| el17mcd | 17:cb39d9fa08dc | 86 | _counter++; | 
| el17mcd | 15:fa5282fcd134 | 87 | } | 
| el17mcd | 15:fa5282fcd134 | 88 | |
| el17mcd | 15:fa5282fcd134 | 89 | void Menus::_read_inputs(Gamepad &pad) | 
| el17mcd | 15:fa5282fcd134 | 90 | { | 
| el17mcd | 17:cb39d9fa08dc | 91 | pad.check_event(Gamepad::Y_PRESSED); | 
| el17mcd | 17:cb39d9fa08dc | 92 | pad.check_event(Gamepad::X_PRESSED); | 
| el17mcd | 17:cb39d9fa08dc | 93 | pad.check_event(Gamepad::A_PRESSED); | 
| el17mcd | 17:cb39d9fa08dc | 94 | pad.check_event(Gamepad::B_PRESSED); | 
| el17mcd | 15:fa5282fcd134 | 95 | } | 
| el17mcd | 15:fa5282fcd134 | 96 | |
| el17mcd | 17:cb39d9fa08dc | 97 | void Menus::_controls_screen(N5110 &lcd, Gamepad &pad) | 
| el17mcd | 15:fa5282fcd134 | 98 | { | 
| el17mcd | 17:cb39d9fa08dc | 99 | while(pad.check_event(Gamepad::BACK_PRESSED) == false) { | 
| el17mcd | 17:cb39d9fa08dc | 100 | _read_inputs(pad); | 
| el17mcd | 17:cb39d9fa08dc | 101 | lcd.clear(); | 
| el17mcd | 17:cb39d9fa08dc | 102 | lcd.printString(" Stick: Aim",0,1); | 
| el17mcd | 17:cb39d9fa08dc | 103 | lcd.printString(" A : Fire",0,2); | 
| el17mcd | 17:cb39d9fa08dc | 104 | lcd.printString(" L/R : Move",0,3); | 
| el17mcd | 17:cb39d9fa08dc | 105 | lcd.printString(" Pot : Power",0,4); | 
| el17mcd | 17:cb39d9fa08dc | 106 | lcd.drawRect(0, 0, 84 , 48, FILL_TRANSPARENT); | 
| el17mcd | 17:cb39d9fa08dc | 107 | lcd.drawRect(1, 1, 82 , 46, FILL_TRANSPARENT); | 
| el17mcd | 17:cb39d9fa08dc | 108 | lcd.refresh(); | 
| el17mcd | 17:cb39d9fa08dc | 109 | wait_ms(1000/60); | 
| el17mcd | 17:cb39d9fa08dc | 110 | } | 
| el17mcd | 15:fa5282fcd134 | 111 | } | 
| el17mcd | 15:fa5282fcd134 | 112 | |
| el17mcd | 17:cb39d9fa08dc | 113 | void Menus::_settings_screen(N5110 &lcd, Gamepad &pad) | 
| el17mcd | 15:fa5282fcd134 | 114 | { | 
| el17mcd | 17:cb39d9fa08dc | 115 | while(pad.check_event(Gamepad::BACK_PRESSED) == false) { | 
| el17mcd | 17:cb39d9fa08dc | 116 | lcd.clear(); | 
| el17mcd | 17:cb39d9fa08dc | 117 | _mute_brightness_lives(lcd, pad); | 
| el17mcd | 17:cb39d9fa08dc | 118 | _read_inputs(pad); | 
| el17mcd | 17:cb39d9fa08dc | 119 | lcd.printString("(O) Health:",4,1); | 
| el17mcd | 17:cb39d9fa08dc | 120 | lcd.printString("(A) Mute:",4,2); | 
| el17mcd | 17:cb39d9fa08dc | 121 | lcd.printString("(B) Brightness",4,3); | 
| el17mcd | 17:cb39d9fa08dc | 122 | lcd.printString("tune with pot",4,4); | 
| el17mcd | 17:cb39d9fa08dc | 123 | lcd.drawRect(0, 0, 84 , 48, FILL_TRANSPARENT); | 
| el17mcd | 17:cb39d9fa08dc | 124 | lcd.drawRect(1, 1, 82 , 46, FILL_TRANSPARENT); | 
| el17mcd | 17:cb39d9fa08dc | 125 | if (_mute == true ) { | 
| el17mcd | 17:cb39d9fa08dc | 126 | lcd.drawRect(70, 17, 7 , 7, FILL_BLACK); | 
| el17mcd | 17:cb39d9fa08dc | 127 | } else { | 
| el17mcd | 17:cb39d9fa08dc | 128 | lcd.drawRect(70, 17, 7 , 7, FILL_TRANSPARENT); | 
| el17mcd | 17:cb39d9fa08dc | 129 | } | 
| el17mcd | 17:cb39d9fa08dc | 130 | lcd.refresh(); | 
| el17mcd | 17:cb39d9fa08dc | 131 | wait_ms(1000/60); | 
| el17mcd | 17:cb39d9fa08dc | 132 | } | 
| el17mcd | 15:fa5282fcd134 | 133 | } | 
| el17mcd | 15:fa5282fcd134 | 134 | |
| el17mcd | 16:a2c945279b79 | 135 | void Menus::_mute_brightness_lives(N5110 &lcd, Gamepad &pad) | 
| el17mcd | 15:fa5282fcd134 | 136 | { | 
| el17mcd | 17:cb39d9fa08dc | 137 | _counter--; | 
| el17mcd | 17:cb39d9fa08dc | 138 | if (_counter < 0) { // counter stops multiple consecutive button clicks | 
| el17mcd | 17:cb39d9fa08dc | 139 | _counter = 10; | 
| el17mcd | 17:cb39d9fa08dc | 140 | if (pad.get_direction() == W && _health > 1) { | 
| el17mcd | 17:cb39d9fa08dc | 141 | _health-- ; | 
| el17mcd | 17:cb39d9fa08dc | 142 | } else if (pad.get_direction() == E && _health < 6) { | 
| el17mcd | 17:cb39d9fa08dc | 143 | _health++ ; | 
| el17mcd | 17:cb39d9fa08dc | 144 | } else if (pad.check_event(Gamepad::A_PRESSED) == true) { | 
| el17mcd | 17:cb39d9fa08dc | 145 | _mute = !_mute; | 
| el17mcd | 17:cb39d9fa08dc | 146 | } else if (pad.check_event(Gamepad::B_PRESSED) == true) { | 
| el17mcd | 17:cb39d9fa08dc | 147 | lcd.setBrightness(pad.read_pot()); | 
| el17mcd | 17:cb39d9fa08dc | 148 | } else { | 
| el17mcd | 17:cb39d9fa08dc | 149 | _counter = -1; | 
| el17mcd | 17:cb39d9fa08dc | 150 | } | 
| el17mcd | 17:cb39d9fa08dc | 151 | } | 
| el17mcd | 17:cb39d9fa08dc | 152 | char buffer[14]; | 
| el17mcd | 17:cb39d9fa08dc | 153 | sprintf(buffer,"%d",_health); | 
| el17mcd | 17:cb39d9fa08dc | 154 | lcd.printString(buffer, 74, 1); | 
| el17mcd | 15:fa5282fcd134 | 155 | } | 
| el17mcd | 15:fa5282fcd134 | 156 | |
| el17mcd | 17:cb39d9fa08dc | 157 | void Menus::_scores_screen(N5110 &lcd, Gamepad &pad, Scores &scores) | 
| el17mcd | 16:a2c945279b79 | 158 | { | 
| el17mcd | 17:cb39d9fa08dc | 159 | while(pad.check_event(Gamepad::BACK_PRESSED) == false) { | 
| el17mcd | 17:cb39d9fa08dc | 160 | lcd.clear(); | 
| el17mcd | 17:cb39d9fa08dc | 161 | scores.display_top_scores(lcd); | 
| el17mcd | 17:cb39d9fa08dc | 162 | lcd.printString("High Scores", 10, 1); | 
| el17mcd | 17:cb39d9fa08dc | 163 | lcd.drawRect(0, 0, 84 , 48, FILL_TRANSPARENT); | 
| el17mcd | 17:cb39d9fa08dc | 164 | lcd.drawRect(1, 1, 82 , 46, FILL_TRANSPARENT); | 
| el17mcd | 17:cb39d9fa08dc | 165 | _read_inputs(pad); | 
| el17mcd | 17:cb39d9fa08dc | 166 | lcd.refresh(); | 
| el17mcd | 17:cb39d9fa08dc | 167 | wait_ms(1000/60); | 
| el17mcd | 17:cb39d9fa08dc | 168 | } | 
| el17mcd | 16:a2c945279b79 | 169 | } | 
| el17mcd | 16:a2c945279b79 | 170 | |
| el17mcd | 15:fa5282fcd134 | 171 | bool Menus::get_mute() | 
| el17mcd | 15:fa5282fcd134 | 172 | { | 
| el17mcd | 17:cb39d9fa08dc | 173 | return _mute; | 
| el17mcd | 15:fa5282fcd134 | 174 | } | 
| el17mcd | 15:fa5282fcd134 | 175 | |
| el17mcd | 16:a2c945279b79 | 176 | int Menus::get_health() | 
| el17mcd | 15:fa5282fcd134 | 177 | { | 
| el17mcd | 17:cb39d9fa08dc | 178 | return _health; | 
| el17mcd | 15:fa5282fcd134 | 179 | } |