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
Interface.cpp
00001 #include "Interface.h" 00002 00003 State fsm_5[5] = { 00004 {0,{4,1}}, 00005 {1,{0,2}}, 00006 {2,{1,3}}, 00007 {3,{2,4}}, 00008 {4,{3,0}} 00009 }; 00010 // point to option 00011 const int pointer[3][2] = { 00012 {1,0}, 00013 {1,1}, 00014 {1,0} 00015 }; 00016 00017 Interface::Interface() 00018 { 00019 00020 } 00021 00022 Interface::~Interface() 00023 { 00024 00025 } 00026 00027 void Interface::init() 00028 { 00029 _menu_flag = 0; 00030 _defined_rate_flag = 0; 00031 _defined_max_flag = 0; 00032 _gameover_flag = 0; 00033 _count_down = 18; 00034 _victory_flag = 0; 00035 _sim_flag = 0;//simple game flag 00036 } 00037 00038 00039 void Interface::Welcome(N5110 &lcd,Gamepad &pad) 00040 { 00041 // initialise record 00042 _record.x = 0; 00043 _record.y = 0; 00044 // start game after pressing the start botton 00045 while (!pad.start_pressed()) 00046 { 00047 lcd.clear(); 00048 lcd.printString(" Welcome! ",0,1); 00049 lcd.printString(" Game by ",0,2); 00050 lcd.printString(" Z. FENG ",0,3); 00051 lcd.setContrast( pad.read_pot1());// control the contrast of screen 00052 lcd.drawRect(0,44,int(pad.read_pot1()*WIDTH),2,FILL_BLACK);// shows the contrast of gamepad by a black rectangle 00053 //printf("WIDTH of contrast = %d, contrast = %f\n",(int)pad.read_pot1()*WIDTH,pad.read_pot1()); 00054 lcd.refresh(); 00055 pad.leds_on(); 00056 wait(0.1); 00057 pad.leds_off(); 00058 wait(0.1); 00059 } 00060 _sound.menu_sound(pad); 00061 } 00062 00063 void Interface::menu(N5110 &lcd,Gamepad &pad) 00064 { 00065 while (!pad.A_pressed()) 00066 { 00067 lcd.clear(); 00068 lcd.printString(" MENU",0,0); 00069 lcd.printString(" Start",0,1); 00070 lcd.printString(" Start(Hell)",0,2); 00071 lcd.printString(" user-defined",0,3); 00072 lcd.printString(" Instruction",0,4); 00073 lcd.printString(" Record",0,5); 00074 _d = pad.get_direction(); 00075 // in order to easily control Joystick, pointer move upward in a large angle 00076 if(_d == N||_d ==NE||_d==NW){ 00077 _menu_flag = fsm_5[_menu_flag].next_state[0]; 00078 } else if(_d == S||_d==SE||_d==SW){ 00079 _menu_flag = fsm_5[_menu_flag].next_state[1]; 00080 } 00081 lcd.drawSprite(0,8*_menu_flag+10,3,2,(int*)pointer); 00082 lcd.refresh(); 00083 pad.leds_on(); 00084 wait(0.3); 00085 //printf("A_pressed\n"); 00086 } 00087 _sound.begin_sound(pad); 00088 } 00089 00090 void Interface::undefined_rate(N5110 &lcd,Gamepad &pad) 00091 { 00092 pad.tone(10,0.05); 00093 pad.set_bpm(0); 00094 pad.reset_buttons(); 00095 while (!pad.A_pressed()) 00096 { 00097 lcd.clear(); 00098 lcd.printString(" shots/frames",0,0); 00099 lcd.printString(" 0.3",0,1); 00100 lcd.printString(" 0.4",0,2); 00101 lcd.printString(" 0.5",0,3); 00102 lcd.printString(" 0.6",0,4); 00103 lcd.printString(" 0.7",0,5); 00104 _d = pad.get_direction(); 00105 if(_d == N||_d ==NE||_d==NW){ 00106 _defined_rate_flag = fsm_5[_defined_rate_flag].next_state[0]; 00107 } else if(_d == S||_d==SE||_d==SW){ 00108 _defined_rate_flag = fsm_5[_defined_rate_flag].next_state[1]; 00109 } 00110 lcd.drawSprite(20,8*_defined_rate_flag+10,3,2,(int*)pointer); 00111 lcd.refresh(); 00112 pad.leds_on(); 00113 wait(0.3); 00114 } 00115 } 00116 00117 00118 void Interface::undefined_max(N5110 &lcd,Gamepad &pad) 00119 { 00120 pad.reset_buttons(); 00121 while (!pad.A_pressed()) 00122 { 00123 lcd.clear(); 00124 lcd.printString(" Max.shots",0,0); 00125 lcd.printString(" 15",0,1); 00126 lcd.printString(" 20",0,2); 00127 lcd.printString(" 25",0,3); 00128 lcd.printString(" 30",0,4); 00129 lcd.printString(" 35",0,5); 00130 _d = pad.get_direction(); 00131 if(_d == N||_d ==NE||_d==NW){ 00132 _defined_max_flag = fsm_5[_defined_max_flag].next_state[0]; 00133 } else if(_d == S||_d==SE||_d==SW){ 00134 _defined_max_flag = fsm_5[_defined_max_flag].next_state[1]; 00135 } 00136 lcd.drawSprite(20,8*_defined_max_flag+10,3,2,(int*)pointer); 00137 lcd.refresh(); 00138 pad.leds_on(); 00139 wait(0.3); 00140 } 00141 _sound.begin_sound(pad); 00142 } 00143 00144 00145 void Interface::game_over(N5110 &lcd,Gamepad &pad) 00146 { 00147 _sound.over_sound(pad); 00148 lcd.clear(); 00149 pad.init(); 00150 while(!pad.B_pressed()){ 00151 lcd.printString(" Game over ",0,1); 00152 lcd.printString(" Press B to ",0,2); 00153 lcd.printString(" try again? ",0,3); 00154 lcd.refresh(); 00155 wait(0.1); 00156 //printf("Not press B\n"); 00157 } 00158 _gameover_flag = 1; 00159 } 00160 00161 int Interface::check_pause(N5110 &lcd,Gamepad &pad,int paused_flag, float increment, int max) 00162 { 00163 if(pad.start_pressed()){ 00164 paused_flag = 1; 00165 sprintf(_buffer1," %.1f shots/s",increment*6); 00166 sprintf(_buffer2," with Max.%d ",max); 00167 } 00168 //printf("start press\n"); 00169 while(paused_flag == 1){ 00170 lcd.clear(); 00171 _timer.stop(); 00172 lcd.printString("Paused",23,0); 00173 lcd.printString(_buffer1,0,2); 00174 lcd.printString(_buffer2,0,3); 00175 lcd.refresh(); 00176 if(pad.start_pressed()) 00177 paused_flag = 0; 00178 } 00179 _timer.start(); 00180 return paused_flag; 00181 } 00182 00183 void Interface::count_down(N5110 &lcd) 00184 { 00185 char buffer[6]; 00186 sprintf(buffer,"%d",int(_count_down/6)); 00187 lcd.printString(buffer,40,2); 00188 //printf("count: %d\n",_count_flag); 00189 _count_down--; 00190 } 00191 00192 void Interface::update_record(float increment,int max) 00193 { 00194 if(increment > _record.x||(int(increment)==int(_record.x)&& max > _record.y)){ 00195 _record.x = increment; 00196 _record.y = max; 00197 } 00198 } 00199 00200 void Interface::record(N5110 &lcd,Gamepad &pad) 00201 { 00202 lcd.clear(); 00203 pad.init(); 00204 pad.tone(10,0.05); 00205 pad.set_bpm(0); 00206 sprintf(_buffer1," %.1f shots/s",_record.x*6); 00207 sprintf(_buffer2," with Max.%.0f ",_record.y); 00208 while(!pad.B_pressed()){ 00209 lcd.printString("Record",23,0); 00210 lcd.printString(_buffer1,0,1); 00211 lcd.printString(_buffer2,0,2); 00212 lcd.printString(" Press B back ",0,4); 00213 lcd.printString(" to menu ",0,5); 00214 lcd.refresh(); 00215 wait(0.1); 00216 } 00217 } 00218 00219 void Interface::victory(N5110 &lcd,Gamepad &pad) 00220 { 00221 _sound.vict_sound(pad); 00222 lcd.clear(); 00223 pad.init(); 00224 while(!pad.B_pressed()){ 00225 lcd.printString(" Victory! ",0,1); 00226 lcd.printString(" Press B back ",0,2); 00227 lcd.printString(" to menu ",0,3); 00228 lcd.refresh(); 00229 wait(0.1); 00230 } 00231 _victory_flag = 1; 00232 } 00233 00234 void Interface::simple_game(N5110 &lcd,Gamepad &pad) 00235 { 00236 lcd.clear(); 00237 pad.init(); 00238 while(!pad.A_pressed()){ 00239 lcd.printString(" DO YOU WANT ",0,1); 00240 lcd.printString(" MAKE IT EASY? ",0,2); 00241 lcd.printString(" PRESS A to",0,3); 00242 lcd.printString(" try again! ",0,4); 00243 lcd.refresh(); 00244 wait(0.1); 00245 } 00246 _sim_flag = 1; 00247 _sound.count_sound(pad); 00248 } 00249 00250 void Interface::set_count_down(int count_down) 00251 { 00252 _count_down = count_down; 00253 } 00254 00255 int Interface::get_menu_flag() 00256 { 00257 return _menu_flag; 00258 } 00259 00260 int Interface::get_rate_flag() 00261 { 00262 return _defined_rate_flag; 00263 } 00264 00265 int Interface::get_max_flag() 00266 { 00267 return _defined_max_flag; 00268 } 00269 00270 int Interface::get_sim_flag() 00271 { 00272 //printf("Get simple_flag %d\n",_sim_flag); 00273 return _sim_flag; 00274 } 00275 00276 int Interface::get_gameover_flag() 00277 { 00278 return _gameover_flag; 00279 } 00280 00281 int Interface::get_victory_flag() 00282 { 00283 return _victory_flag; 00284 }
Generated on Thu Jul 14 2022 12:45:25 by
1.7.2