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 FATFileSystem
Menu/Menu.cpp@3:a8960004d261, 2019-03-29 (annotated)
- Committer:
- ellisbhastroud
- Date:
- Fri Mar 29 18:45:40 2019 +0000
- Revision:
- 3:a8960004d261
- Parent:
- 2:81cfa8310f55
- Child:
- 4:035448357749
Menu Working, Game Mechanisms Developed. Can now use joystick and button to shoot golf ball with power dependant on joystick magnitude.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ellisbhastroud | 1:6179c2d67d19 | 1 | #include "Menu.h" |
| ellisbhastroud | 1:6179c2d67d19 | 2 | |
| ellisbhastroud | 1:6179c2d67d19 | 3 | // constructor |
| ellisbhastroud | 1:6179c2d67d19 | 4 | |
| ellisbhastroud | 1:6179c2d67d19 | 5 | Menu::Menu() |
| ellisbhastroud | 1:6179c2d67d19 | 6 | { |
| ellisbhastroud | 1:6179c2d67d19 | 7 | |
| ellisbhastroud | 1:6179c2d67d19 | 8 | } |
| ellisbhastroud | 1:6179c2d67d19 | 9 | |
| ellisbhastroud | 1:6179c2d67d19 | 10 | //deconstructor |
| ellisbhastroud | 1:6179c2d67d19 | 11 | |
| ellisbhastroud | 1:6179c2d67d19 | 12 | Menu::~Menu() |
| ellisbhastroud | 1:6179c2d67d19 | 13 | { |
| ellisbhastroud | 1:6179c2d67d19 | 14 | |
| ellisbhastroud | 1:6179c2d67d19 | 15 | } |
| ellisbhastroud | 1:6179c2d67d19 | 16 | |
| ellisbhastroud | 1:6179c2d67d19 | 17 | //public methods |
| ellisbhastroud | 1:6179c2d67d19 | 18 | |
| ellisbhastroud | 3:a8960004d261 | 19 | void Menu::init() |
| ellisbhastroud | 3:a8960004d261 | 20 | { |
| ellisbhastroud | 3:a8960004d261 | 21 | _cursor_pos = START; //cursor begins on start |
| ellisbhastroud | 3:a8960004d261 | 22 | |
| ellisbhastroud | 3:a8960004d261 | 23 | } |
| ellisbhastroud | 3:a8960004d261 | 24 | //prints welcome screen |
| ellisbhastroud | 1:6179c2d67d19 | 25 | void Menu::print_welcome(N5110 &lcd) |
| ellisbhastroud | 1:6179c2d67d19 | 26 | { |
| ellisbhastroud | 1:6179c2d67d19 | 27 | lcd.clear(); |
| ellisbhastroud | 1:6179c2d67d19 | 28 | lcd.printString("Welcome to",0,0); |
| ellisbhastroud | 1:6179c2d67d19 | 29 | lcd.printString("GAME",0,1); |
| ellisbhastroud | 1:6179c2d67d19 | 30 | lcd.printString("by",0,2); |
| ellisbhastroud | 1:6179c2d67d19 | 31 | lcd.printString("Ellis Blackford Stroud",0,3); |
| ellisbhastroud | 1:6179c2d67d19 | 32 | lcd.refresh(); |
| ellisbhastroud | 3:a8960004d261 | 33 | wait(1); |
| ellisbhastroud | 3:a8960004d261 | 34 | lcd.clear(); |
| ellisbhastroud | 3:a8960004d261 | 35 | lcd.printString("Use joystick",0,1); |
| ellisbhastroud | 3:a8960004d261 | 36 | lcd.printString("and START/BACK",0,2); |
| ellisbhastroud | 3:a8960004d261 | 37 | lcd.printString("to navigate menu !",0,3); |
| ellisbhastroud | 3:a8960004d261 | 38 | lcd.refresh(); |
| ellisbhastroud | 3:a8960004d261 | 39 | wait(1); |
| ellisbhastroud | 1:6179c2d67d19 | 40 | } |
| ellisbhastroud | 3:a8960004d261 | 41 | //prints main menu screen |
| ellisbhastroud | 1:6179c2d67d19 | 42 | void Menu::print_menu(N5110 &lcd) |
| ellisbhastroud | 1:6179c2d67d19 | 43 | { |
| ellisbhastroud | 1:6179c2d67d19 | 44 | lcd.clear(); |
| ellisbhastroud | 2:81cfa8310f55 | 45 | lcd.printString("Start Game",10,0); |
| ellisbhastroud | 2:81cfa8310f55 | 46 | lcd.printString("Highscores",10,2); |
| ellisbhastroud | 2:81cfa8310f55 | 47 | lcd.printString("Settings",10,4); |
| ellisbhastroud | 2:81cfa8310f55 | 48 | lcd.refresh(); |
| ellisbhastroud | 2:81cfa8310f55 | 49 | } |
| ellisbhastroud | 3:a8960004d261 | 50 | //prints start screen and waits to start game |
| ellisbhastroud | 3:a8960004d261 | 51 | bool Menu::check_start(N5110 &lcd, Gamepad &pad) |
| ellisbhastroud | 3:a8960004d261 | 52 | { |
| ellisbhastroud | 3:a8960004d261 | 53 | lcd.clear(); |
| ellisbhastroud | 3:a8960004d261 | 54 | lcd.printString("Press A ?",8,2); |
| ellisbhastroud | 3:a8960004d261 | 55 | lcd.printString("to start?",8,3); |
| ellisbhastroud | 3:a8960004d261 | 56 | lcd.refresh(); |
| ellisbhastroud | 3:a8960004d261 | 57 | while(pad.check_event(Gamepad::A_PRESSED) == false) { |
| ellisbhastroud | 3:a8960004d261 | 58 | wait(0.2); |
| ellisbhastroud | 3:a8960004d261 | 59 | } |
| ellisbhastroud | 3:a8960004d261 | 60 | lcd.clear(); |
| ellisbhastroud | 3:a8960004d261 | 61 | lcd.refresh(); |
| ellisbhastroud | 3:a8960004d261 | 62 | printf("GAME STARTED"); |
| ellisbhastroud | 3:a8960004d261 | 63 | return true; |
| ellisbhastroud | 3:a8960004d261 | 64 | } |
| ellisbhastroud | 3:a8960004d261 | 65 | //prints highscores screen |
| ellisbhastroud | 2:81cfa8310f55 | 66 | void Menu::print_highscores(N5110 &lcd) |
| ellisbhastroud | 2:81cfa8310f55 | 67 | { |
| ellisbhastroud | 2:81cfa8310f55 | 68 | lcd.clear(); |
| ellisbhastroud | 2:81cfa8310f55 | 69 | lcd.printString("Highscores",0,2); |
| ellisbhastroud | 2:81cfa8310f55 | 70 | lcd.refresh(); |
| ellisbhastroud | 2:81cfa8310f55 | 71 | } |
| ellisbhastroud | 3:a8960004d261 | 72 | //prints settings screen |
| ellisbhastroud | 2:81cfa8310f55 | 73 | void Menu::print_settings(N5110 &lcd) |
| ellisbhastroud | 2:81cfa8310f55 | 74 | { |
| ellisbhastroud | 2:81cfa8310f55 | 75 | lcd.clear(); |
| ellisbhastroud | 2:81cfa8310f55 | 76 | lcd.printString("Settings",0,2); |
| ellisbhastroud | 2:81cfa8310f55 | 77 | lcd.refresh(); |
| ellisbhastroud | 2:81cfa8310f55 | 78 | } |
| ellisbhastroud | 2:81cfa8310f55 | 79 | |
| ellisbhastroud | 3:a8960004d261 | 80 | //changes menu screen using cursor position and returns true if begin start |
| ellisbhastroud | 3:a8960004d261 | 81 | bool Menu::menu_change(N5110 &lcd, Gamepad &pad) |
| ellisbhastroud | 2:81cfa8310f55 | 82 | { |
| ellisbhastroud | 3:a8960004d261 | 83 | int m = _cursor_pos; |
| ellisbhastroud | 3:a8960004d261 | 84 | |
| ellisbhastroud | 3:a8960004d261 | 85 | switch(m) { //uses cursor position to select next screen |
| ellisbhastroud | 3:a8960004d261 | 86 | case 0: |
| ellisbhastroud | 3:a8960004d261 | 87 | //returns true if start game |
| ellisbhastroud | 3:a8960004d261 | 88 | { |
| ellisbhastroud | 3:a8960004d261 | 89 | bool start = check_start(lcd, pad); |
| ellisbhastroud | 3:a8960004d261 | 90 | return start; |
| ellisbhastroud | 3:a8960004d261 | 91 | } |
| ellisbhastroud | 3:a8960004d261 | 92 | case 1: |
| ellisbhastroud | 3:a8960004d261 | 93 | print_highscores(lcd); |
| ellisbhastroud | 3:a8960004d261 | 94 | return false; |
| ellisbhastroud | 3:a8960004d261 | 95 | case 2: |
| ellisbhastroud | 3:a8960004d261 | 96 | print_settings(lcd); |
| ellisbhastroud | 3:a8960004d261 | 97 | return false; |
| ellisbhastroud | 3:a8960004d261 | 98 | default: // default case |
| ellisbhastroud | 3:a8960004d261 | 99 | return false; |
| ellisbhastroud | 3:a8960004d261 | 100 | } |
| ellisbhastroud | 3:a8960004d261 | 101 | } |
| ellisbhastroud | 3:a8960004d261 | 102 | |
| ellisbhastroud | 3:a8960004d261 | 103 | void Menu::menu_return(N5110 &lcd, Gamepad &pad) |
| ellisbhastroud | 3:a8960004d261 | 104 | { |
| ellisbhastroud | 3:a8960004d261 | 105 | while(pad.check_event(Gamepad::BACK_PRESSED) == false) { |
| ellisbhastroud | 3:a8960004d261 | 106 | |
| ellisbhastroud | 3:a8960004d261 | 107 | } |
| ellisbhastroud | 3:a8960004d261 | 108 | print_menu(lcd); |
| ellisbhastroud | 1:6179c2d67d19 | 109 | } |
| ellisbhastroud | 1:6179c2d67d19 | 110 | |
| ellisbhastroud | 2:81cfa8310f55 | 111 | //Uses joystick direction to select menu option |
| ellisbhastroud | 3:a8960004d261 | 112 | void Menu::menu_select(Gamepad &pad, N5110 &lcd) |
| ellisbhastroud | 2:81cfa8310f55 | 113 | { |
| ellisbhastroud | 2:81cfa8310f55 | 114 | |
| ellisbhastroud | 3:a8960004d261 | 115 | while(pad.check_event(Gamepad::START_PRESSED) == false) { //loops until start pressed |
| ellisbhastroud | 3:a8960004d261 | 116 | |
| ellisbhastroud | 3:a8960004d261 | 117 | int d = pad.get_direction(); |
| ellisbhastroud | 3:a8960004d261 | 118 | MenuChoices cursor_pos = _cursor_pos; |
| ellisbhastroud | 3:a8960004d261 | 119 | |
| ellisbhastroud | 3:a8960004d261 | 120 | switch(cursor_pos) { |
| ellisbhastroud | 3:a8960004d261 | 121 | //cursor on start |
| ellisbhastroud | 3:a8960004d261 | 122 | case 0: |
| ellisbhastroud | 3:a8960004d261 | 123 | draw_cursor(lcd); |
| ellisbhastroud | 3:a8960004d261 | 124 | switch(d) { |
| ellisbhastroud | 3:a8960004d261 | 125 | case 0: //joystick centered |
| ellisbhastroud | 3:a8960004d261 | 126 | _cursor_pos = START; |
| ellisbhastroud | 3:a8960004d261 | 127 | break; |
| ellisbhastroud | 3:a8960004d261 | 128 | case 1: //joystick up |
| ellisbhastroud | 3:a8960004d261 | 129 | _cursor_pos = SETTINGS; |
| ellisbhastroud | 3:a8960004d261 | 130 | break; |
| ellisbhastroud | 3:a8960004d261 | 131 | case 5: //joystick down |
| ellisbhastroud | 3:a8960004d261 | 132 | _cursor_pos = HIGHSCORES; |
| ellisbhastroud | 3:a8960004d261 | 133 | break; |
| ellisbhastroud | 3:a8960004d261 | 134 | } |
| ellisbhastroud | 3:a8960004d261 | 135 | break; |
| ellisbhastroud | 3:a8960004d261 | 136 | //cursor on highscores |
| ellisbhastroud | 3:a8960004d261 | 137 | case 1: |
| ellisbhastroud | 3:a8960004d261 | 138 | draw_cursor(lcd); |
| ellisbhastroud | 3:a8960004d261 | 139 | switch(d) { |
| ellisbhastroud | 3:a8960004d261 | 140 | case 0: //joystick centered |
| ellisbhastroud | 3:a8960004d261 | 141 | _cursor_pos = HIGHSCORES; |
| ellisbhastroud | 3:a8960004d261 | 142 | break; |
| ellisbhastroud | 3:a8960004d261 | 143 | case 1: //joystick up |
| ellisbhastroud | 3:a8960004d261 | 144 | _cursor_pos = START; |
| ellisbhastroud | 3:a8960004d261 | 145 | break; |
| ellisbhastroud | 3:a8960004d261 | 146 | case 5: //joystick down |
| ellisbhastroud | 3:a8960004d261 | 147 | _cursor_pos = SETTINGS; |
| ellisbhastroud | 3:a8960004d261 | 148 | break; |
| ellisbhastroud | 3:a8960004d261 | 149 | } |
| ellisbhastroud | 3:a8960004d261 | 150 | break; |
| ellisbhastroud | 3:a8960004d261 | 151 | //cursor on settings |
| ellisbhastroud | 3:a8960004d261 | 152 | case 2: |
| ellisbhastroud | 3:a8960004d261 | 153 | draw_cursor(lcd); |
| ellisbhastroud | 3:a8960004d261 | 154 | switch(d) { |
| ellisbhastroud | 3:a8960004d261 | 155 | case 0: //joystick centered |
| ellisbhastroud | 3:a8960004d261 | 156 | _cursor_pos = SETTINGS; |
| ellisbhastroud | 3:a8960004d261 | 157 | break; |
| ellisbhastroud | 3:a8960004d261 | 158 | case 1: //joystick up |
| ellisbhastroud | 3:a8960004d261 | 159 | _cursor_pos = HIGHSCORES; |
| ellisbhastroud | 3:a8960004d261 | 160 | break; |
| ellisbhastroud | 3:a8960004d261 | 161 | case 5: //joystick down |
| ellisbhastroud | 3:a8960004d261 | 162 | _cursor_pos = START; |
| ellisbhastroud | 3:a8960004d261 | 163 | break; |
| ellisbhastroud | 3:a8960004d261 | 164 | } |
| ellisbhastroud | 3:a8960004d261 | 165 | break; |
| ellisbhastroud | 3:a8960004d261 | 166 | } |
| ellisbhastroud | 3:a8960004d261 | 167 | |
| ellisbhastroud | 3:a8960004d261 | 168 | wait(0.2); |
| ellisbhastroud | 3:a8960004d261 | 169 | } |
| ellisbhastroud | 2:81cfa8310f55 | 170 | |
| ellisbhastroud | 3:a8960004d261 | 171 | } |
| ellisbhastroud | 3:a8960004d261 | 172 | |
| ellisbhastroud | 3:a8960004d261 | 173 | |
| ellisbhastroud | 3:a8960004d261 | 174 | //private methods |
| ellisbhastroud | 3:a8960004d261 | 175 | |
| ellisbhastroud | 3:a8960004d261 | 176 | //draws cursor in necessary position |
| ellisbhastroud | 3:a8960004d261 | 177 | void Menu::draw_cursor(N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 178 | { |
| ellisbhastroud | 3:a8960004d261 | 179 | int cursor_pos = _cursor_pos; |
| ellisbhastroud | 3:a8960004d261 | 180 | switch(cursor_pos) { |
| ellisbhastroud | 3:a8960004d261 | 181 | case 0: //cursor on start |
| ellisbhastroud | 2:81cfa8310f55 | 182 | lcd.drawRect(0,16,6,8,FILL_WHITE); |
| ellisbhastroud | 2:81cfa8310f55 | 183 | lcd.drawRect(0,32,6,8,FILL_WHITE); |
| ellisbhastroud | 3:a8960004d261 | 184 | lcd.drawRect(0,0,6,8,FILL_TRANSPARENT); |
| ellisbhastroud | 2:81cfa8310f55 | 185 | break; |
| ellisbhastroud | 3:a8960004d261 | 186 | case 1: //cursor on highscores |
| ellisbhastroud | 2:81cfa8310f55 | 187 | lcd.drawRect(0,0,6,8,FILL_WHITE); |
| ellisbhastroud | 2:81cfa8310f55 | 188 | lcd.drawRect(0,32,6,8,FILL_WHITE); |
| ellisbhastroud | 2:81cfa8310f55 | 189 | lcd.drawRect(0,16,6,8,FILL_TRANSPARENT); |
| ellisbhastroud | 2:81cfa8310f55 | 190 | break; |
| ellisbhastroud | 3:a8960004d261 | 191 | case 2: //cursor on settings |
| ellisbhastroud | 2:81cfa8310f55 | 192 | lcd.drawRect(0,0,6,8,FILL_WHITE); |
| ellisbhastroud | 2:81cfa8310f55 | 193 | lcd.drawRect(0,16,6,8,FILL_WHITE); |
| ellisbhastroud | 2:81cfa8310f55 | 194 | lcd.drawRect(0,32,6,8,FILL_TRANSPARENT); |
| ellisbhastroud | 2:81cfa8310f55 | 195 | break; |
| ellisbhastroud | 3:a8960004d261 | 196 | } |
| ellisbhastroud | 3:a8960004d261 | 197 | lcd.refresh(); |
| ellisbhastroud | 3:a8960004d261 | 198 | } |