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
Menu.cpp
00001 #include "Menu.h" 00002 00003 // Define sprite arrays. 00004 int menu_sprite[23][80] = { 00005 { 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0, 00006 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 00007 0,0,1 }, 00008 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 00009 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 00010 1,1,1 }, 00011 { 0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0, 00012 0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1, 00013 1,1,0 }, 00014 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 00015 0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0, 00016 0,0,0 }, 00017 { 0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0, 00018 0,0,0,0,0,1,0,1,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1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,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1, 00072 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 00073 0,0,0 } 00074 }; 00075 00076 // Define the finite state machine for three different states: running game, 00077 // display 00078 // menu and display controls, {output,{a,b,c,d}} where a,b,c,d are inputs. 00079 // The outputs have been assigned to 0,1,2. 00080 // There are four different relevant input combinations that have been assigned 00081 // to: 0,1,2,3. 00082 State _fsm[3] = { 00083 {0,{0,1,0,0}}, 00084 {1,{1,1,0,2}}, 00085 {2,{2,1,2,0}} 00086 }; 00087 00088 // Constructor and destructor. 00089 Menu::Menu() {} 00090 00091 Menu::~Menu() {} 00092 00093 void Menu::init() { 00094 // Starting setup of menu. 00095 _state = 1; // Starting state is the menu. 00096 _controller.init(); 00097 // Initialise counters for musical tune. 00098 _tone_flag = false; 00099 _tone_counter = 0; 00100 _chord_counter = 0; 00101 } 00102 00103 void Menu::run(N5110 &lcd, Gamepad &gamepad) { 00104 set_input(gamepad.check_event(Gamepad::START_PRESSED), 00105 gamepad.check_event(Gamepad::BACK_PRESSED), 00106 gamepad.check_event(Gamepad::X_PRESSED)); 00107 // Select the current output, execute that output via the output function and 00108 // update to the next state depending on the input. 00109 _output = _fsm[_state].output; 00110 output(lcd, gamepad); 00111 _state = _fsm[_state].next_state[_menu_input]; 00112 } 00113 00114 void Menu::output(N5110 &lcd, Gamepad &gamepad) { 00115 // 0, 1 and 2 have been assigned to the output of each state respectively. 00116 if (_output == 0) { 00117 run_game(lcd, gamepad); // Runs the main game loop. 00118 } else if (_output == 1) { 00119 display_menu(lcd, gamepad); // Displays the main menu. 00120 } else { 00121 display_controls(lcd, gamepad); // Displayes the control page. 00122 } 00123 } 00124 00125 void Menu::set_input(bool start, bool back, bool x) { 00126 // 0, 1, 2, 3 have been assigned to each relevant input combination. 00127 if (!start && !back && !x) { // Nothing is pressed. 00128 _menu_input = 0; 00129 } else if (!start && back) { // Only back is pressed. 00130 _menu_input = 1; 00131 } else if (start && !back) { // Only start is pressed. 00132 _menu_input = 2; 00133 } else if (x) { // Only X is pressed. 00134 _menu_input = 3; 00135 } 00136 } 00137 00138 void Menu::run_game(N5110 &lcd, Gamepad &gamepad) { 00139 _controller.run_game_engine(lcd, gamepad); 00140 } 00141 00142 void Menu::display_controls(N5110 &lcd, Gamepad &gamepad) { 00143 // Print lines for aesthetics, and print controls text. 00144 lcd.drawLine(0,0,84,0,FILL_BLACK); 00145 lcd.drawLine(0,6,84,6,FILL_BLACK); 00146 lcd.drawLine(0,24,84,24,FILL_BLACK); 00147 lcd.drawLine(0,30,84,30,FILL_BLACK); 00148 lcd.printString("JOYSTICK:",0,0); 00149 lcd.printString("-Go Left/Right",0,1); 00150 lcd.printString("-Down to Duck",0,2); 00151 lcd.printString("Collect Coins!",0,5); 00152 lcd.printString("A:",0,3); 00153 lcd.printString("-Jump",0,4); 00154 play_tone(gamepad); // Play the tune. 00155 } 00156 00157 void Menu::display_menu(N5110 &lcd, Gamepad &gamepad) { 00158 // Print lines and sprite for aesthetics, and print menu text. 00159 lcd.drawLine(0,0,84,0,FILL_BLACK); 00160 lcd.drawLine(0,6,84,6,FILL_BLACK); 00161 lcd.drawLine(0,24,84,24,FILL_BLACK); 00162 lcd.printString("SKATER",24,0); 00163 lcd.printString("START- Play!",0,1); 00164 lcd.printString("X- Controls",0,2); 00165 lcd.drawSprite(1,25,23,80,(int*)menu_sprite); 00166 play_tone(gamepad); // Play the tune. 00167 } 00168 00169 void Menu::play_tone(Gamepad &gamepad) { 00170 // Plays a musical tune in 3/4 based on two chords, A and F in Aminor. 00171 // It switches between each chord every 12 game loops via chord counter. 00172 _tone_flag = !_tone_flag; 00173 _tone_counter++; 00174 _chord_counter++; 00175 if (_chord_counter < 12) { 00176 play_chord_a(gamepad); 00177 } else { 00178 if (_chord_counter == 24) _chord_counter = 0; 00179 play_chord_b(gamepad); 00180 } 00181 wait(0.14); // Determines how fast the musical tune is played. 00182 } 00183 00184 void Menu::play_chord_a(Gamepad &gamepad) { 00185 // For each chord, the first set of two notes is played for 5 game loops and 00186 // the second set for 2 game loops via tone counter. 00187 // Each note is alternated every game loop via tone flag. 00188 if (_tone_counter < 5) { 00189 if (_tone_flag) { 00190 gamepad.tone(220, 0.5); 00191 } else { 00192 gamepad.tone(440, 0.1); 00193 } 00194 } else { 00195 if (_tone_counter == 6) _tone_counter = 0; 00196 if (_tone_flag) { 00197 gamepad.tone(174.61, 0.1); 00198 } else { 00199 gamepad.tone(246.94, 0.1); 00200 } 00201 } 00202 } 00203 00204 void Menu::play_chord_b(Gamepad &gamepad) { 00205 // Same as play_chord_a, just different notes. 00206 if (_tone_counter < 5) { 00207 if (_tone_flag) { 00208 gamepad.tone(174.61, 0.5); 00209 } else { 00210 gamepad.tone(349.23, 0.1); 00211 } 00212 } else { 00213 if (_tone_counter == 6) _tone_counter = 0; 00214 if (_tone_flag) { 00215 gamepad.tone(659.25, 0.1); 00216 } else { 00217 gamepad.tone(220, 0.1); 00218 } 00219 } 00220 }
Generated on Mon Jul 18 2022 14:06:02 by
1.7.2