Marty Laverick / main

Dependents:   DCGame el14m2l_Dungeon_Crawler_Game

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002 @file main.cpp
00003 
00004 @brief Member functions implementations
00005 
00006 */
00007 
00008 #include "main.h"
00009 
00010 int main()
00011 {
00012     init_K64F();
00013     init_N5110();
00014     init_obj();
00015     calibrate_joystick();  // get centred values of joystick
00016     refresh_rate  = 10.0f;
00017     g_dt  = 1.0f/refresh_rate ;
00018     update_flag.attach(&update_flag_func,g_dt );  // read joystick n times per second
00019     
00020     //used to initialise the highscore files
00021     /*fp = fopen("/sd/firstname.txt", "w");
00022     fprintf(fp, "AAA");
00023     fclose(fp);
00024     fp = fopen("/sd/secondname.txt", "w");
00025     fprintf(fp, "AAA");
00026     fclose(fp);
00027     fp = fopen("/sd/thirdname.txt", "w");
00028     fprintf(fp, "AAA");
00029     fclose(fp);
00030     fp = fopen("/sd/fourthname.txt", "w");
00031     fprintf(fp, "AAA");
00032     fclose(fp);
00033     fp = fopen("/sd/fifthname.txt", "w");
00034     fprintf(fp, "AAA");
00035     fclose(fp);
00036     fp = fopen("/sd/firstscore.txt", "w");
00037     fprintf(fp, "%d",selector);
00038     fclose(fp);
00039     fp = fopen("/sd/secondscore.txt", "w");
00040     fprintf(fp, "%d",selector);
00041     fclose(fp);
00042     fp = fopen("/sd/thirdscore.txt", "w");
00043     fprintf(fp, "%d",selector);
00044     fclose(fp);
00045     fp = fopen("/sd/fourthscore.txt", "w");
00046     fprintf(fp, "%d",selector);
00047     fclose(fp);
00048     fp = fopen("/sd/fifthscore.txt", "w");
00049     fprintf(fp, "%d",selector);
00050     fclose(fp);*/
00051     
00052     menu_viewed =0;
00053     js_button_previous =0;
00054     b_button_previous =0;
00055     up_flag =0;
00056     menu();
00057 }
00058 
00059 // read default positions of the joystick to calibrate later readings
00060 void calibrate_joystick()
00061 {
00062     js_button.mode(PullDown);
00063     b_button.mode(PullDown);
00064     // must not move during calibration
00065     joystick.x0 = js_pot_x;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
00066     joystick.y0 = js_pot_y;
00067 }
00068 
00069 void update_flag_func()
00070 {
00071     up_flag =1;
00072 }
00073 
00074 void update_joystick()
00075 {
00076     // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
00077     joystick.x = js_pot_x - joystick.x0;
00078     joystick.y = js_pot_y - joystick.y0;
00079     // read button selector
00080     joystick.button = js_button_check();
00081     joystick.bbutton = b_button_check();
00082 
00083     // calculate direction depending on x,y values
00084     // tolerance allows a little lee-way in case joystick not exactly in the selectord direction
00085     if(fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00086         joystick.direction = CENTRE;
00087     } else if(joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00088         joystick.direction = UP;
00089     } else if(joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00090         joystick.direction = DOWN;
00091     } else if(joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00092         joystick.direction = RIGHT;
00093     } else if(joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00094         joystick.direction = LEFT;
00095     } else if(joystick.y < DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) {
00096         joystick.direction = UPRIGHT;
00097     } else if(joystick.y < DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) {
00098         joystick.direction = UPLEFT;
00099     } else if(joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) {
00100         joystick.direction = DOWNRIGHT;
00101     } else if(joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) {
00102         joystick.direction = DOWNLEFT;
00103     } else {
00104         joystick.direction = UNKNOWN;
00105     }
00106 }
00107 
00108 bool js_button_check()
00109 {
00110     if((js_button_previous ==1) && (js_button==1)) {
00111         return 0;
00112     } else if((js_button_previous ==0) && (js_button==1)) {
00113         js_button_previous =1;
00114         return 1;
00115     } else {
00116         js_button_previous =0;
00117         return 0;
00118     }
00119 }
00120 
00121 bool b_button_check()
00122 {
00123     if((b_button_previous ==1) && (b_button==1)) {
00124         return 0;
00125     } else if((b_button_previous ==0) && (b_button==1)) {
00126         b_button_previous =1;
00127         return 1;
00128     } else {
00129         b_button_previous =0;
00130         return 0;
00131     }
00132 }
00133 
00134 void init_K64F()
00135 {
00136     // on-board LEDs are active-low, so set pin high to turn them off.
00137     r_led=1;
00138     g_led=0;
00139     b_led=1;
00140 
00141     // since the on-board switches have external pull-ups, we should disable the internal pull-down
00142     // resistors that are enabled by default using InterruptIn
00143     sw2.mode(PullNone);
00144     sw3.mode(PullNone);
00145 
00146 }
00147 
00148 void init_N5110()
00149 {
00150     lcd.init();
00151     lcd.normalMode();
00152     lcd.setBrightness(0.5);
00153 }
00154 
00155 void init_obj()
00156 {
00157     player_still_obj.fill(player_still);
00158     player_moving1_obj.fill(player_moving1);
00159     player_moving2_obj.fill(player_moving2);
00160     player_jumping_obj.fill(player_jumping);
00161     pointer_obj.fill(pointer);
00162     spike_small_obj1.fill(spike_small);
00163     spike_small_obj2.fill(spike_small);
00164     spike_small_obj3.fill(spike_small);
00165     spike_large_obj1.fill(spike_large);
00166     spike_large_obj2.fill(spike_large);
00167 }
00168 
00169 void menu()
00170 {
00171     while(!joystick.button && !joystick.bbutton && !menu_viewed ) {
00172         if(up_flag ) {
00173             up_flag =0;
00174             update_joystick();
00175         }
00176         lcd.clear();
00177         lcd.printString("Cave Runner",10,0);
00178         player.draw(34,31);
00179         lcd.printString("Press A or B",6,5);
00180         selector =0;
00181         option =1;
00182         lcd.refresh();
00183         sleep();
00184     }
00185     if(joystick.button && !menu_viewed ) {
00186         buzzer.tone(NOTE_C4,SEMIQUAVER);
00187     } else if(joystick.bbutton && !menu_viewed ) {
00188         buzzer.tone(NOTE_D3,SEMIQUAVER);
00189     }
00190     update_joystick();
00191     lcd.clear();
00192     menu_viewed =1;
00193     while(!joystick.button) {
00194         if(up_flag ) {
00195             up_flag =0;
00196             update_joystick();
00197         }
00198         //read highscores
00199         fp = fopen("/sd/firstscore.txt", "r");
00200         fscanf(fp, "%d",&highscore1 );
00201         fclose(fp);
00202         fp = fopen("/sd/secondscore.txt", "r");
00203         fscanf(fp, "%d",&highscore2 );
00204         fclose(fp);
00205         fp = fopen("/sd/thirdscore.txt", "r");
00206         fscanf(fp, "%d",&highscore3 );
00207         fclose(fp);
00208         fp = fopen("/sd/fourthscore.txt", "r");
00209         fscanf(fp, "%d",&highscore4 );
00210         fclose(fp);
00211         fp = fopen("/sd/fifthscore.txt", "r");
00212         fscanf(fp, "%d",&highscore5 );
00213         fclose(fp);
00214         //read highscore names
00215         fp = fopen("/sd/firstname.txt", "r");
00216         fscanf(fp, "%c",&name1 [0]);
00217         fscanf(fp, "%c",&name1 [1]);
00218         fscanf(fp, "%c",&name1 [2]);
00219         fscanf(fp, "%c",&name1 [3]);
00220         fclose(fp);
00221         fp = fopen("/sd/secondname.txt", "r");
00222         fscanf(fp, "%c",&name2 [0]);
00223         fscanf(fp, "%c",&name2 [1]);
00224         fscanf(fp, "%c",&name2 [2]);
00225         fscanf(fp, "%c",&name2 [3]);
00226         fclose(fp);
00227         fp = fopen("/sd/thirdname.txt", "r");
00228         fscanf(fp, "%c",&name3 [0]);
00229         fscanf(fp, "%c",&name3 [1]);
00230         fscanf(fp, "%c",&name3 [2]);
00231         fscanf(fp, "%c",&name3 [3]);
00232         fclose(fp);
00233         fp = fopen("/sd/fourthname.txt", "r");
00234         fscanf(fp, "%c",&name4 [0]);
00235         fscanf(fp, "%c",&name4 [1]);
00236         fscanf(fp, "%c",&name4 [2]);
00237         fscanf(fp, "%c",&name4 [3]);
00238         fclose(fp);
00239         fp = fopen("/sd/fifthname.txt", "r");
00240         fscanf(fp, "%c",&name5 [0]);
00241         fscanf(fp, "%c",&name5 [1]);
00242         fscanf(fp, "%c",&name5 [2]);
00243         fscanf(fp, "%c",&name5 [3]);
00244         fclose(fp);
00245         switch(selector ) {
00246             case 0:
00247                 lcd.clear();
00248                 lcd.printString("Cave Runner",10,0);
00249                 lcd.printString("Start Game",16,2);
00250                 lcd.printString("Highscores",16,3);
00251                 lcd.printString("Rules",16,4);
00252                 lcd.printString("Credits",16,5);
00253                 pointer_obj.draw(10,21);
00254                 option =1;
00255                 lcd.refresh();
00256                 if(joystick.direction==DOWN) {
00257                     selector =1;
00258                     buzzer.tone(NOTE_A3,SEMIQUAVER);
00259                     break;
00260                 } else {
00261                     selector =0;
00262                     wait(0.1);
00263                     break;
00264                 }
00265             case 1:
00266                 lcd.clear();
00267                 lcd.printString("Cave Runner",10,0);
00268                 lcd.printString("Start Game",16,2);
00269                 lcd.printString("Highscores",16,3);
00270                 lcd.printString("Rules",16,4);
00271                 lcd.printString("Credits",16,5);
00272                 pointer_obj.draw(10,29);
00273                 option =2;
00274                 lcd.refresh();
00275                 if(joystick.direction==UP) {
00276                     selector =0;
00277                     buzzer.tone(NOTE_A3,SEMIQUAVER);
00278                     break;
00279                 } else if(joystick.direction==DOWN) {
00280                     selector =2;
00281                     buzzer.tone(NOTE_A3,SEMIQUAVER);
00282                     break;
00283                 } else {
00284                     selector =1;
00285                     wait(0.1);
00286                     break;
00287                 }
00288             case 2:
00289                 lcd.clear();
00290                 lcd.printString("Cave Runner",10,0);
00291                 lcd.printString("Start Game",16,2);
00292                 lcd.printString("Highscores",16,3);
00293                 lcd.printString("Rules",16,4);
00294                 lcd.printString("Credits",16,5);
00295                 pointer_obj.draw(10,37);
00296                 option =3;
00297                 lcd.refresh();
00298                 if(joystick.direction==UP) {
00299                     selector =1;
00300                     buzzer.tone(NOTE_A3,SEMIQUAVER);
00301                     break;
00302                 } else if(joystick.direction==DOWN) {
00303                     selector =3;
00304                     buzzer.tone(NOTE_A3,SEMIQUAVER);
00305                     break;
00306                 } else {
00307                     selector =2;
00308                     wait(0.1);
00309                     break;
00310                 }
00311             case 3:
00312                 lcd.clear();
00313                 lcd.printString("Cave Runner",10,0);
00314                 lcd.printString("Start Game",16,2);
00315                 lcd.printString("Highscores",16,3);
00316                 lcd.printString("Rules",16,4);
00317                 lcd.printString("Credits",16,5);
00318                 pointer_obj.draw(10,45);
00319                 option =4;
00320                 lcd.refresh();
00321                 if(joystick.direction==UP) {
00322                     selector =2;
00323                     buzzer.tone(NOTE_A3,SEMIQUAVER);
00324                     break;
00325                 } else {
00326                     selector =3;
00327                     wait(0.1);
00328                     break;
00329                 }
00330         }
00331         sleep();
00332     }
00333     menu_select();
00334 }
00335 
00336 void menu_select()
00337 {
00338     buzzer.tone(NOTE_C4,SEMIQUAVER);
00339     update_joystick();
00340     if(option ==1) {
00341         start_game();
00342     } else if(option ==2) {
00343         highscores();
00344     } else if(option ==3) {
00345         rules();
00346     } else if(option ==4) {
00347         credits();
00348     }
00349 }
00350 
00351 void start_game()
00352 {
00353     wait(g_dt );
00354     update_joystick();
00355     lcd.clear();
00356     screen.init();
00357     init_game();
00358     while(1) {
00359         if(up_flag ) {
00360             up_flag =0;
00361             update_joystick();
00362             check_collisions();
00363             update_physics_engine();
00364             redraw();
00365         }
00366         score_counter ++;
00367         if(joystick.direction==CENTRE) {
00368             vel.x=0.0;
00369             vel.y=0.0;
00370         } else if(joystick.direction==RIGHT) {
00371             vel.x=4.0;
00372             vel.y=0.0;
00373         } else if(joystick.direction==LEFT) {
00374             vel.x=-4.0;
00375             vel.y=0.0;
00376         } else if((joystick.direction==UP) && (jump_counter <3)) {
00377             jump_counter ++;
00378             vel.x=0.0;
00379             vel.y=-8.0;
00380         } else if((joystick.direction==UPRIGHT) && (jump_counter <3)) {
00381             jump_counter ++;
00382             vel.x=4.0;
00383             vel.y=-8.0;
00384         } else if((joystick.direction==UPLEFT) && (jump_counter <3)) {
00385             jump_counter ++;
00386             vel.x=-4.0;
00387             vel.y=-8.0;
00388         }
00389         //check is there will be a spike spawn
00390         if(!spike1_there ) {
00391             if((rand()%30)==3) {
00392                 spike1_there =1;
00393                 spike_x1 =79;
00394             }
00395         }
00396         if((!spike2_there ) && (score_counter >100)) {
00397             if((rand()%30)==9) {
00398                 spike2_there =1;
00399                 spike_x2 =79;
00400             }
00401         }
00402         if((!spike3_there ) && (score_counter >200)) {
00403             if((rand()%30)==18) {
00404                 spike3_there =1;
00405                 spike_x3 =79;
00406             }
00407         }
00408         if((!spike4_there ) && (score_counter >500)) {
00409             if((rand()%50)==24) {
00410                 spike4_there =1;
00411                 spike_x4 =78;
00412             }
00413         }
00414         if((!spike5_there ) && (score_counter >1000)) {
00415             if((rand()%50)==42) {
00416                 spike5_there =1;
00417                 spike_x5 =78;
00418             }
00419         }
00420         //check for spike actions if they are on screen
00421         if(spike1_there ) {
00422             if(spike_small_obj1.check_collision(spike_x1 ,41)) {
00423                 health --;
00424                 spike1_there =0;
00425                 buzzer.tone(NOTE_CS1,SEMIQUAVER);
00426             }
00427             if(spike_x1 <0) {
00428                 spike1_there =0;
00429             }
00430             spike_x1 --;
00431         }
00432         if(spike2_there ) {
00433             if(spike_small_obj2.check_collision(spike_x2 ,41)) {
00434                 health --;
00435                 spike2_there =0;
00436                 buzzer.tone(NOTE_CS1,SEMIQUAVER);
00437             }
00438             if(spike_x2 <0) {
00439                 spike2_there =0;
00440             }
00441             spike_x2 --;
00442         }
00443         if(spike3_there ) {
00444             if(spike_small_obj3.check_collision(spike_x3 ,41)) {
00445                 health --;
00446                 spike3_there =0;
00447                 buzzer.tone(NOTE_CS1,SEMIQUAVER);
00448             }
00449             if(spike_x3 <0) {
00450                 spike3_there =0;
00451             }
00452             spike_x3 --;
00453         }
00454         if(spike4_there ) {
00455             if(spike_large_obj1.check_collision(spike_x4 ,41)) {
00456                 health --;
00457                 spike4_there =0;
00458                 buzzer.tone(NOTE_CS1,SEMIQUAVER);
00459             }
00460             if(spike_x4 <0) {
00461                 spike4_there =0;
00462             }
00463             spike_x4 --;
00464         }
00465         if(spike5_there ) {
00466             if(spike_large_obj2.check_collision(spike_x5 ,41)) {
00467                 health --;
00468                 spike5_there =0;
00469                 buzzer.tone(NOTE_CS1,SEMIQUAVER);
00470             }
00471             if(spike_x5 <0) {
00472                 spike5_there =0;
00473             }
00474             spike_x5 --;
00475         }
00476         if(dead ) {
00477             //bwom bwom bwom to be added
00478             death();
00479         }
00480         sleep();
00481     }
00482 }
00483 
00484 void init_game()
00485 {
00486     health =5;
00487     dead =0;
00488     jump_counter =0;
00489     score_counter =0;
00490     spike1_there =0;
00491     spike2_there =0;
00492     spike3_there =0;
00493     spike4_there =0;
00494     spike5_there =0;
00495     spike1_counter =0;
00496     spike2_counter =0;
00497     spike3_counter =0;
00498     spike4_counter =0;
00499     spike5_counter =0;
00500     bat1.counter=0;
00501     bat2.counter=0;
00502     bat3.counter=0;
00503     bat4.counter=0;
00504     bat5.counter=0;
00505     bat1.state=0;
00506     bat2.state=0;
00507     bat3.state=0;
00508     bat4.state=0;
00509     bat5.state=0;
00510     bat1.next=0;
00511     bat2.next=0;
00512     bat3.next=0;
00513     bat4.next=0;
00514     bat5.next=0;
00515     background.counter=0;
00516     for(int i=43; i<48; i++) {
00517         for(int j=0; j<84; j++) {
00518             screen.set_pixel(j,i,GAME);
00519         }
00520     }
00521     background.init();
00522     background.fill(screen.layers);
00523     init_player();
00524     background.draw_screen(BG);
00525     draw_health();
00526     screen.draw();
00527     lcd.printString("READY",28,2);
00528     wait(1.0);
00529     lcd.printString("GO!",35,3);
00530     wait(1.0);
00531     lcd.refresh();
00532 }
00533 
00534 void init_player()
00535 {
00536     pos.x = 0;
00537     pos.y = 42;
00538     player.draw_screen(pos.x,pos.y,GAME);
00539     vel.x = 0.0;
00540     vel.y = 0.0;
00541     acc.x = 0.0;
00542     acc.y = 0.0;
00543 }
00544 
00545 void draw_health()
00546 {
00547     if(health ==5) {
00548         bat1.draw_screen(0,6,HEALTH);
00549         bat2.draw_screen(13,6,HEALTH);
00550         bat3.draw_screen(26,6,HEALTH);
00551         bat4.draw_screen(39,6,HEALTH);
00552         bat5.draw_screen(52,6,HEALTH);
00553     } else if (health ==4) {
00554         bat1.draw_screen(0,6,HEALTH);
00555         bat2.draw_screen(13,6,HEALTH);
00556         bat3.draw_screen(26,6,HEALTH);
00557         bat4.draw_screen(39,6,HEALTH);
00558     } else if (health ==3) {
00559         bat1.draw_screen(0,6,HEALTH);
00560         bat2.draw_screen(13,6,HEALTH);
00561         bat3.draw_screen(26,6,HEALTH);
00562     } else if (health ==2) {
00563         bat1.draw_screen(0,6,HEALTH);
00564         bat2.draw_screen(13,6,HEALTH);
00565     } else if (health ==1) {
00566         bat1.draw_screen(0,6,HEALTH);
00567     } else if (health ==0) {
00568         dead =1;
00569     }
00570 }
00571 
00572 void check_collisions()
00573 {
00574     //groud collision
00575     for(int i=0; i<13; i++) {
00576         if(screen.get_pixel((pos.x + i),(pos.y + 1),GAME)==0) {
00577             acc.y = 4.0;
00578             jumping  = true;
00579         } else {
00580             acc.y = 0.0;
00581             jump_counter =0;
00582             jumping  = false;
00583             break;
00584         }
00585     }
00586     //ceilling pixel collision
00587     for(int i=0; i<13; i++) {
00588         if(screen.get_pixel((pos.x + i),(pos.y - 15),GAME)==0) {
00589             acc.y = acc.y;
00590         } else {
00591             vel.y = - vel.y;
00592             acc.y = 4.0;
00593             break;
00594         }
00595     }
00596     //ceilling screen collision
00597     if((pos.y - 14) == 0) {
00598         vel.y = - vel.y;
00599         acc.y = 4.0;
00600     }
00601     //right pixel collision
00602     for(int j=0; j<15; j++) {
00603         if((screen.get_pixel((pos.x + 13),(pos.y - j),GAME)==1) && (pos.x + 13 < 84)) {
00604             vel.x = -vel.x;
00605             break;
00606         }
00607     }
00608     //left pixel collision
00609     for(int j=0; j<15; j++) {
00610         if((screen.get_pixel((pos.x - 1),(pos.y - j),GAME)==1) && (pos.x - 1 > 0)) {
00611             vel.x = -vel.x;
00612             break;
00613         }
00614     }
00615 }
00616 
00617 void update_physics_engine()
00618 {
00619     // from Newton's Laws
00620 
00621     // calc new velocity (assume 'unit' time)
00622     vel.x = vel.x + acc.x; // * g_gt;
00623     vel.y = vel.y + acc.y; // * g_gt;
00624 
00625     // calc new position (assume 'unit' time)
00626     pos.x = pos.x + vel.x;// * g_gt;
00627     pos.y = pos.y + vel.y;// * g_dt;
00628     
00629     //stops the character from falling off the side of the screen
00630     if(pos.x<0) {
00631         pos.x=0;
00632     }
00633     if(pos.x>71) {
00634         pos.x=71;
00635     }
00636     //stops the character from falling through the floor
00637     if((pos.y>42) && ((screen.get_pixel(pos.x+3,pos.y-10,GAME)) || (screen.get_pixel(pos.x+9,pos.y-6,GAME)))) {
00638         pos.y=42;
00639     }
00640     //stops the character from falling through platforms
00641     if((screen.get_pixel(pos.x,pos.y+1,GAME)) && !(screen.get_pixel(pos.x,pos.y+2,GAME))) {
00642         pos.y=pos.y-3;
00643     }
00644     
00645     // should really multiply the above by the time-step,
00646     // but since the pixel can only be a integer value,
00647     // it makes the motion a little 'jumpy'.
00648 
00649 }
00650 
00651 void redraw()
00652 {
00653     lcd.clear();
00654     screen.clear();
00655     if(jumping ) {
00656         player_jumping_obj.draw_screen(pos.x,pos.y,GAME);
00657     } else {
00658         player.draw_screen(pos.x,pos.y,GAME);
00659     }
00660     if(spike1_there ) {
00661         spike_small_obj1.draw_screen(spike_x1 ,42,GAME);
00662     }
00663     if(spike2_there ) {
00664         spike_small_obj2.draw_screen(spike_x2 ,42,GAME);
00665     }
00666     if(spike3_there ) {
00667         spike_small_obj3.draw_screen(spike_x3 ,42,GAME);
00668     }
00669     if(spike4_there ) {
00670         spike_large_obj1.draw_screen(spike_x4 ,42,GAME);
00671     }
00672     if(spike5_there ) {
00673         spike_large_obj2.draw_screen(spike_x5 ,42,GAME);
00674     }
00675     for(int i=43; i<48; i++) {
00676         for(int j=0; j<84; j++) {
00677             screen.set_pixel(j,i,GAME);
00678         }
00679     }
00680     background.draw_screen(BG);
00681     draw_health();
00682     screen.draw();
00683     lcd.refresh();
00684     
00685 }
00686 
00687 void death()
00688 {
00689     wait(g_dt );
00690     lcd.clear();
00691     while(!joystick.bbutton) {
00692         if(up_flag ) {
00693             up_flag =0;
00694             update_joystick();
00695         }
00696         lcd.printString("Better Luck",10,1);
00697         lcd.printString("Next Time",15,2);
00698         lcd.printString("Your Score",13,4);
00699         sprintf(score ,"%d",score_counter );
00700         lcd.printString(score ,32,5);
00701         sleep();
00702     }
00703     buzzer.tone(NOTE_D3,SEMIQUAVER);
00704     new_highscore();
00705     menu();
00706 }
00707 
00708 void highscores()
00709 {
00710     wait(g_dt );
00711     lcd.clear();
00712     while(!joystick.bbutton) {
00713         if(up_flag ) {
00714             up_flag =0;
00715             update_joystick();
00716         }
00717         lcd.printString("Highscores",12,0);
00718         sprintf(score1 ,"%d",highscore1 );
00719         lcd.printString(score1 ,42,1);
00720         lcd.printString(name1 ,20,1);
00721         sprintf(score2 ,"%d",highscore2 );
00722         lcd.printString(score2 ,42,2);
00723         lcd.printString(name2 ,20,2);
00724         sprintf(score3 ,"%d",highscore3 );
00725         lcd.printString(score3 ,42,3);
00726         lcd.printString(name3 ,20,3);
00727         sprintf(score4 ,"%d",highscore4 );
00728         lcd.printString(score4 ,42,4);
00729         lcd.printString(name4 ,20,4);
00730         sprintf(score5 ,"%d",highscore5 );
00731         lcd.printString(score5 ,42,5);
00732         lcd.printString(name5 ,20,5);
00733         sleep();
00734     }
00735     buzzer.tone(NOTE_D3,SEMIQUAVER);
00736     menu();
00737 }
00738 
00739 void new_highscore()
00740 {
00741     if(score_counter >=highscore1 ) {
00742         highscore5 =highscore4 ;
00743         highscore4 =highscore3 ;
00744         highscore3 =highscore2 ;
00745         highscore2 =highscore1 ;
00746         highscore1 =score_counter ;
00747         lcd.clear();
00748         lcd.printString("New Highscore!",0,2);
00749         wait(2.0);
00750         register_name();
00751         fp = fopen("/sd/firstscore.txt", "w");
00752         fprintf(fp, "%d",highscore1 );
00753         fclose(fp);
00754         fp = fopen("/sd/secondscore.txt", "w");
00755         fprintf(fp, "%d",highscore2 );
00756         fclose(fp);
00757         fp = fopen("/sd/thirdscore.txt", "w");
00758         fprintf(fp, "%d",highscore3 );
00759         fclose(fp);
00760         fp = fopen("/sd/fourthscore.txt", "w");
00761         fprintf(fp, "%d",highscore4 );
00762         fclose(fp);
00763         fp = fopen("/sd/fifthscore.txt", "w");
00764         fprintf(fp, "%d",highscore5 );
00765         fclose(fp);
00766         sprintf(name5 ,name4 );
00767         sprintf(name4 ,name3 );
00768         sprintf(name3 ,name2 );
00769         sprintf(name2 ,name1 );
00770         sprintf(name1 ,name );
00771         fp = fopen("/sd/firstname.txt", "w");
00772         fprintf(fp, name1 );
00773         fclose(fp);
00774         fp = fopen("/sd/secondname.txt", "w");
00775         fprintf(fp, name2 );
00776         fclose(fp);
00777         fp = fopen("/sd/thirdname.txt", "w");
00778         fprintf(fp, name3 );
00779         fclose(fp);
00780         fp = fopen("/sd/fourthname.txt", "w");
00781         fprintf(fp, name4 );
00782         fclose(fp);
00783         fp = fopen("/sd/fifthname.txt", "w");
00784         fprintf(fp, name5 );
00785         fclose(fp);
00786     } else if(score_counter >=highscore2 ) {
00787         highscore5 =highscore4 ;
00788         highscore4 =highscore3 ;
00789         highscore3 =highscore2 ;
00790         highscore2 =score_counter ;
00791         lcd.clear();
00792         lcd.printString("New Highscore!",0,2);
00793         wait(2.0);
00794         register_name();
00795         fp = fopen("/sd/secondscore.txt", "w");
00796         fprintf(fp, "%d",highscore2 );
00797         fclose(fp);
00798         fp = fopen("/sd/thirdscore.txt", "w");
00799         fprintf(fp, "%d",highscore3 );
00800         fclose(fp);
00801         fp = fopen("/sd/fourthscore.txt", "w");
00802         fprintf(fp, "%d",highscore4 );
00803         fclose(fp);
00804         fp = fopen("/sd/fifthscore.txt", "w");
00805         fprintf(fp, "%d",highscore5 );
00806         fclose(fp);
00807         sprintf(name5 ,name4 );
00808         sprintf(name4 ,name3 );
00809         sprintf(name3 ,name2 );
00810         sprintf(name2 ,name );
00811         fp = fopen("/sd/secondname.txt", "w");
00812         fprintf(fp, name2 );
00813         fclose(fp);
00814         fp = fopen("/sd/thirdname.txt", "w");
00815         fprintf(fp, name3 );
00816         fclose(fp);
00817         fp = fopen("/sd/fourthname.txt", "w");
00818         fprintf(fp, name4 );
00819         fclose(fp);
00820         fp = fopen("/sd/fifthname.txt", "w");
00821         fprintf(fp, name5 );
00822         fclose(fp);
00823     } else if(score_counter >=highscore3 ) {
00824         highscore5 =highscore4 ;
00825         highscore4 =highscore3 ;
00826         highscore3 =score_counter ;
00827         lcd.clear();
00828         lcd.printString("New Highscore!",0,2);
00829         wait(2.0);
00830         register_name();
00831         fp = fopen("/sd/thirdscore.txt", "w");
00832         fprintf(fp, "%d",highscore3 );
00833         fclose(fp);
00834         fp = fopen("/sd/fourthscore.txt", "w");
00835         fprintf(fp, "%d",highscore4 );
00836         fclose(fp);
00837         fp = fopen("/sd/fifthscore.txt", "w");
00838         fprintf(fp, "%d",highscore5 );
00839         fclose(fp);
00840         sprintf(name5 ,name4 );
00841         sprintf(name4 ,name3 );
00842         sprintf(name3 ,name );
00843         fp = fopen("/sd/thirdname.txt", "w");
00844         fprintf(fp, name3 );
00845         fclose(fp);
00846         fp = fopen("/sd/fourthname.txt", "w");
00847         fprintf(fp, name4 );
00848         fclose(fp);
00849         fp = fopen("/sd/fifthname.txt", "w");
00850         fprintf(fp, name5 );
00851         fclose(fp);
00852     } else if(score_counter >=highscore4 ) {
00853         highscore5 =highscore4 ;
00854         highscore4 =score_counter ;
00855         lcd.clear();
00856         lcd.printString("New Highscore!",0,2);
00857         wait(2.0);
00858         register_name();
00859         fp = fopen("/sd/fourthscore.txt", "w");
00860         fprintf(fp, "%d",highscore4 );
00861         fclose(fp);
00862         fp = fopen("/sd/fifthscore.txt", "w");
00863         fprintf(fp, "%d",highscore5 );
00864         fclose(fp);
00865         sprintf(name5 ,name4 );
00866         sprintf(name4 ,name );
00867         fp = fopen("/sd/fourthname.txt", "w");
00868         fprintf(fp, name4 );
00869         fclose(fp);
00870         fp = fopen("/sd/fifthname.txt", "w");
00871         fprintf(fp, name5 );
00872         fclose(fp);
00873     } else if(score_counter >=highscore5 ) {
00874         highscore5 =score_counter ;
00875         lcd.clear();
00876         lcd.printString("New Highscore!",0,2);
00877         wait(2.0);
00878         register_name();
00879         fp = fopen("/sd/fifthscore.txt", "w");
00880         fprintf(fp, "%d",highscore5 );
00881         fclose(fp);
00882         sprintf(name5 ,name );
00883         fp = fopen("/sd/fifthname.txt", "w");
00884         fprintf(fp, name5 );
00885         fclose(fp);
00886     }
00887 }
00888 
00889 void register_name()
00890 {
00891     name [0]='A';
00892     name [1]='A';
00893     name [2]='A';
00894     letter_counter =0;
00895     letter =0;
00896     letter1 =0;
00897     letter2 =0;
00898     letter3 =0;
00899     while(!joystick.button) {
00900         if(up_flag ) {
00901             up_flag =0;
00902             update_joystick();
00903         }
00904         lcd.clear();
00905         switch(letter ) {
00906             case 0:
00907                 if(letter_counter >=2) {
00908                     lcd.setPixel(32,24);
00909                     lcd.setPixel(33,24);
00910                     lcd.setPixel(34,24);
00911                     lcd.setPixel(35,24);
00912                     lcd.setPixel(36,24);
00913                     if(letter_counter ==4) {
00914                         letter_counter =0;
00915                     } else {
00916                         letter_counter ++;
00917                     }
00918                 } else {
00919                     letter_counter ++;
00920                 }
00921                 switch(letter1 ) {
00922                     case 0:
00923                         name [0]='A';
00924                         lcd.printChar(name [0],32,2);
00925                         lcd.printChar(name [1],38,2);
00926                         lcd.printChar(name [2],44,2);
00927                         wait(g_dt );
00928                         if(joystick.direction==DOWN) {
00929                             letter1 ++;
00930                         } else if(joystick.direction==UP) {
00931                             letter1 =25;
00932                         }
00933                         if(joystick.direction==RIGHT) {
00934                             letter ++;
00935                         }
00936                         break;
00937                     case 1:
00938                         name [0]='B';
00939                         lcd.printChar(name [0],32,2);
00940                         lcd.printChar(name [1],38,2);
00941                         lcd.printChar(name [2],44,2);
00942                         wait(g_dt );
00943                         if(joystick.direction==DOWN) {
00944                             letter1 ++;
00945                         } else if(joystick.direction==UP) {
00946                             letter1 --;
00947                         }
00948                         if(joystick.direction==RIGHT) {
00949                             letter ++;
00950                         }
00951                         break;
00952                     case 2:
00953                         name [0]='C';
00954                         lcd.printChar(name [0],32,2);
00955                         lcd.printChar(name [1],38,2);
00956                         lcd.printChar(name [2],44,2);
00957                         wait(g_dt );
00958                         if(joystick.direction==DOWN) {
00959                             letter1 ++;
00960                         } else if(joystick.direction==UP) {
00961                             letter1 --;
00962                         }
00963                         if(joystick.direction==RIGHT) {
00964                             letter ++;
00965                         }
00966                         break;
00967                     case 3:
00968                         name [0]='D';
00969                         lcd.printChar(name [0],32,2);
00970                         lcd.printChar(name [1],38,2);
00971                         lcd.printChar(name [2],44,2);
00972                         wait(g_dt );
00973                         if(joystick.direction==DOWN) {
00974                             letter1 ++;
00975                         } else if(joystick.direction==UP) {
00976                             letter1 --;
00977                         }
00978                         if(joystick.direction==RIGHT) {
00979                             letter ++;
00980                         }
00981                         break;
00982                     case 4:
00983                         name [0]='E';
00984                         lcd.printChar(name [0],32,2);
00985                         lcd.printChar(name [1],38,2);
00986                         lcd.printChar(name [2],44,2);
00987                         wait(g_dt );
00988                         if(joystick.direction==DOWN) {
00989                             letter1 ++;
00990                         } else if(joystick.direction==UP) {
00991                             letter1 --;
00992                         }
00993                         if(joystick.direction==RIGHT) {
00994                             letter ++;
00995                         }
00996                         break;
00997                     case 5:
00998                         name [0]='F';
00999                         lcd.printChar(name [0],32,2);
01000                         lcd.printChar(name [1],38,2);
01001                         lcd.printChar(name [2],44,2);
01002                         wait(g_dt );
01003                         if(joystick.direction==DOWN) {
01004                             letter1 ++;
01005                         } else if(joystick.direction==UP) {
01006                             letter1 --;
01007                         }
01008                         if(joystick.direction==RIGHT) {
01009                             letter ++;
01010                         }
01011                         break;
01012                     case 6:
01013                         name [0]='G';
01014                         lcd.printChar(name [0],32,2);
01015                         lcd.printChar(name [1],38,2);
01016                         lcd.printChar(name [2],44,2);
01017                         wait(g_dt );
01018                         if(joystick.direction==DOWN) {
01019                             letter1 ++;
01020                         } else if(joystick.direction==UP) {
01021                             letter1 --;
01022                         }
01023                         if(joystick.direction==RIGHT) {
01024                             letter ++;
01025                         }
01026                         break;
01027                     case 7:
01028                         name [0]='H';
01029                         lcd.printChar(name [0],32,2);
01030                         lcd.printChar(name [1],38,2);
01031                         lcd.printChar(name [2],44,2);
01032                         wait(g_dt );
01033                         if(joystick.direction==DOWN) {
01034                             letter1 ++;
01035                         } else if(joystick.direction==UP) {
01036                             letter1 --;
01037                         }
01038                         if(joystick.direction==RIGHT) {
01039                             letter ++;
01040                         }
01041                         break;
01042                     case 8:
01043                         name [0]='I';
01044                         lcd.printChar(name [0],32,2);
01045                         lcd.printChar(name [1],38,2);
01046                         lcd.printChar(name [2],44,2);
01047                         wait(g_dt );
01048                         if(joystick.direction==DOWN) {
01049                             letter1 ++;
01050                         } else if(joystick.direction==UP) {
01051                             letter1 --;
01052                         }
01053                         if(joystick.direction==RIGHT) {
01054                             letter ++;
01055                         }
01056                         break;
01057                     case 9:
01058                         name [0]='J';
01059                         lcd.printChar(name [0],32,2);
01060                         lcd.printChar(name [1],38,2);
01061                         lcd.printChar(name [2],44,2);
01062                         wait(g_dt );
01063                         if(joystick.direction==DOWN) {
01064                             letter1 ++;
01065                         } else if(joystick.direction==UP) {
01066                             letter1 --;
01067                         }
01068                         if(joystick.direction==RIGHT) {
01069                             letter ++;
01070                         }
01071                         break;
01072                     case 10:
01073                         name [0]='K';
01074                         lcd.printChar(name [0],32,2);
01075                         lcd.printChar(name [1],38,2);
01076                         lcd.printChar(name [2],44,2);
01077                         wait(g_dt );
01078                         if(joystick.direction==DOWN) {
01079                             letter1 ++;
01080                         } else if(joystick.direction==UP) {
01081                             letter1 --;
01082                         }
01083                         if(joystick.direction==RIGHT) {
01084                             letter ++;
01085                         }
01086                         break;
01087                     case 11:
01088                         name [0]='L';
01089                         lcd.printChar(name [0],32,2);
01090                         lcd.printChar(name [1],38,2);
01091                         lcd.printChar(name [2],44,2);
01092                         wait(g_dt );
01093                         if(joystick.direction==DOWN) {
01094                             letter1 ++;
01095                         } else if(joystick.direction==UP) {
01096                             letter1 --;
01097                         }
01098                         if(joystick.direction==RIGHT) {
01099                             letter ++;
01100                         }
01101                         break;
01102                     case 12:
01103                         name [0]='M';
01104                         lcd.printChar(name [0],32,2);
01105                         lcd.printChar(name [1],38,2);
01106                         lcd.printChar(name [2],44,2);
01107                         wait(g_dt );
01108                         if(joystick.direction==DOWN) {
01109                             letter1 ++;
01110                         } else if(joystick.direction==UP) {
01111                             letter1 --;
01112                         }
01113                         if(joystick.direction==RIGHT) {
01114                             letter ++;
01115                         }
01116                         break;
01117                     case 13:
01118                         name [0]='N';
01119                         lcd.printChar(name [0],32,2);
01120                         lcd.printChar(name [1],38,2);
01121                         lcd.printChar(name [2],44,2);
01122                         wait(g_dt );
01123                         if(joystick.direction==DOWN) {
01124                             letter1 ++;
01125                         } else if(joystick.direction==UP) {
01126                             letter1 --;
01127                         }
01128                         if(joystick.direction==RIGHT) {
01129                             letter ++;
01130                         }
01131                         break;
01132                     case 14:
01133                         name [0]='O';
01134                         lcd.printChar(name [0],32,2);
01135                         lcd.printChar(name [1],38,2);
01136                         lcd.printChar(name [2],44,2);
01137                         wait(g_dt );
01138                         if(joystick.direction==DOWN) {
01139                             letter1 ++;
01140                         } else if(joystick.direction==UP) {
01141                             letter1 --;
01142                         }
01143                         if(joystick.direction==RIGHT) {
01144                             letter ++;
01145                         }
01146                         break;
01147                     case 15:
01148                         name [0]='P';
01149                         lcd.printChar(name [0],32,2);
01150                         lcd.printChar(name [1],38,2);
01151                         lcd.printChar(name [2],44,2);
01152                         wait(g_dt );
01153                         if(joystick.direction==DOWN) {
01154                             letter1 ++;
01155                         } else if(joystick.direction==UP) {
01156                             letter1 --;
01157                         }
01158                         if(joystick.direction==RIGHT) {
01159                             letter ++;
01160                         }
01161                         break;
01162                     case 16:
01163                         name [0]='Q';
01164                         lcd.printChar(name [0],32,2);
01165                         lcd.printChar(name [1],38,2);
01166                         lcd.printChar(name [2],44,2);
01167                         wait(g_dt );
01168                         if(joystick.direction==DOWN) {
01169                             letter1 ++;
01170                         } else if(joystick.direction==UP) {
01171                             letter1 --;
01172                         }
01173                         if(joystick.direction==RIGHT) {
01174                             letter ++;
01175                         }
01176                         break;
01177                     case 17:
01178                         name [0]='R';
01179                         lcd.printChar(name [0],32,2);
01180                         lcd.printChar(name [1],38,2);
01181                         lcd.printChar(name [2],44,2);
01182                         wait(g_dt );
01183                         if(joystick.direction==DOWN) {
01184                             letter1 ++;
01185                         } else if(joystick.direction==UP) {
01186                             letter1 --;
01187                         }
01188                         if(joystick.direction==RIGHT) {
01189                             letter ++;
01190                         }
01191                         break;
01192                     case 18:
01193                         name [0]='S';
01194                         lcd.printChar(name [0],32,2);
01195                         lcd.printChar(name [1],38,2);
01196                         lcd.printChar(name [2],44,2);
01197                         wait(g_dt );
01198                         if(joystick.direction==DOWN) {
01199                             letter1 ++;
01200                         } else if(joystick.direction==UP) {
01201                             letter1 --;
01202                         }
01203                         if(joystick.direction==RIGHT) {
01204                             letter ++;
01205                         }
01206                         break;
01207                     case 19:
01208                         name [0]='T';
01209                         lcd.printChar(name [0],32,2);
01210                         lcd.printChar(name [1],38,2);
01211                         lcd.printChar(name [2],44,2);
01212                         wait(g_dt );
01213                         if(joystick.direction==DOWN) {
01214                             letter1 ++;
01215                         } else if(joystick.direction==UP) {
01216                             letter1 --;
01217                         }
01218                         if(joystick.direction==RIGHT) {
01219                             letter ++;
01220                         }
01221                         break;
01222                     case 20:
01223                         name [0]='U';
01224                         lcd.printChar(name [0],32,2);
01225                         lcd.printChar(name [1],38,2);
01226                         lcd.printChar(name [2],44,2);
01227                         wait(g_dt );
01228                         if(joystick.direction==DOWN) {
01229                             letter1 ++;
01230                         } else if(joystick.direction==UP) {
01231                             letter1 --;
01232                         }
01233                         if(joystick.direction==RIGHT) {
01234                             letter ++;
01235                         }
01236                         break;
01237                     case 21:
01238                         name [0]='V';
01239                         lcd.printChar(name [0],32,2);
01240                         lcd.printChar(name [1],38,2);
01241                         lcd.printChar(name [2],44,2);
01242                         wait(g_dt );
01243                         if(joystick.direction==DOWN) {
01244                             letter1 ++;
01245                         } else if(joystick.direction==UP) {
01246                             letter1 --;
01247                         }
01248                         if(joystick.direction==RIGHT) {
01249                             letter ++;
01250                         }
01251                         break;
01252                     case 22:
01253                         name [0]='W';
01254                         lcd.printChar(name [0],32,2);
01255                         lcd.printChar(name [1],38,2);
01256                         lcd.printChar(name [2],44,2);
01257                         wait(g_dt );
01258                         if(joystick.direction==DOWN) {
01259                             letter1 ++;
01260                         } else if(joystick.direction==UP) {
01261                             letter1 --;
01262                         }
01263                         if(joystick.direction==RIGHT) {
01264                             letter ++;
01265                         }
01266                         break;
01267                     case 23:
01268                         name [0]='X';
01269                         lcd.printChar(name [0],32,2);
01270                         lcd.printChar(name [1],38,2);
01271                         lcd.printChar(name [2],44,2);
01272                         wait(g_dt );
01273                         if(joystick.direction==DOWN) {
01274                             letter1 ++;
01275                         } else if(joystick.direction==UP) {
01276                             letter1 --;
01277                         }
01278                         if(joystick.direction==RIGHT) {
01279                             letter ++;
01280                         }
01281                         break;
01282                     case 24:
01283                         name [0]='Y';
01284                         lcd.printChar(name [0],32,2);
01285                         lcd.printChar(name [1],38,2);
01286                         lcd.printChar(name [2],44,2);
01287                         wait(g_dt );
01288                         if(joystick.direction==DOWN) {
01289                             letter1 ++;
01290                         } else if(joystick.direction==UP) {
01291                             letter1 --;
01292                         }
01293                         if(joystick.direction==RIGHT) {
01294                             letter ++;
01295                         }
01296                         break;
01297                     case 25:
01298                         name [0]='Z';
01299                         lcd.printChar(name [0],32,2);
01300                         lcd.printChar(name [1],38,2);
01301                         lcd.printChar(name [2],44,2);
01302                         wait(g_dt );
01303                         if(joystick.direction==DOWN) {
01304                             letter1 =0;
01305                         } else if(joystick.direction==UP) {
01306                             letter1 --;
01307                         }
01308                         if(joystick.direction==RIGHT) {
01309                             letter ++;
01310                         }
01311                         break;
01312                 }
01313                 break;
01314             case 1:
01315                 if(letter_counter >=2) {
01316                     lcd.setPixel(38,24);
01317                     lcd.setPixel(39,24);
01318                     lcd.setPixel(40,24);
01319                     lcd.setPixel(41,24);
01320                     lcd.setPixel(42,24);
01321                     if(letter_counter ==4) {
01322                         letter_counter =0;
01323                     } else {
01324                         letter_counter ++;
01325                     }
01326                 } else {
01327                     letter_counter ++;
01328                 }
01329                 switch(letter2 ) {
01330                     case 0:
01331                         name [1]='A';
01332                         lcd.printChar(name [0],32,2);
01333                         lcd.printChar(name [1],38,2);
01334                         lcd.printChar(name [2],44,2);
01335                         wait(g_dt );
01336                         if(joystick.direction==DOWN) {
01337                             letter2 ++;
01338                         } else if(joystick.direction==UP) {
01339                             letter2 =25;
01340                         }
01341                         if(joystick.direction==RIGHT) {
01342                             letter ++;
01343                         } else if(joystick.direction==LEFT) {
01344                             letter --;
01345                         }
01346                         break;
01347                     case 1:
01348                         name [1]='B';
01349                         lcd.printChar(name [0],32,2);
01350                         lcd.printChar(name [1],38,2);
01351                         lcd.printChar(name [2],44,2);
01352                         wait(g_dt );
01353                         if(joystick.direction==DOWN) {
01354                             letter2 ++;
01355                         } else if(joystick.direction==UP) {
01356                             letter2 --;
01357                         }
01358                         if(joystick.direction==RIGHT) {
01359                             letter ++;
01360                         } else if(joystick.direction==LEFT) {
01361                             letter --;
01362                         }
01363                         break;
01364                     case 2:
01365                         name [1]='C';
01366                         lcd.printChar(name [0],32,2);
01367                         lcd.printChar(name [1],38,2);
01368                         lcd.printChar(name [2],44,2);
01369                         wait(g_dt );
01370                         if(joystick.direction==DOWN) {
01371                             letter2 ++;
01372                         } else if(joystick.direction==UP) {
01373                             letter2 --;
01374                         }
01375                         if(joystick.direction==RIGHT) {
01376                             letter ++;
01377                         } else if(joystick.direction==LEFT) {
01378                             letter --;
01379                         }
01380                         break;
01381                     case 3:
01382                         name [1]='D';
01383                         lcd.printChar(name [0],32,2);
01384                         lcd.printChar(name [1],38,2);
01385                         lcd.printChar(name [2],44,2);
01386                         wait(g_dt );
01387                         if(joystick.direction==DOWN) {
01388                             letter2 ++;
01389                         } else if(joystick.direction==UP) {
01390                             letter2 --;
01391                         }
01392                         if(joystick.direction==RIGHT) {
01393                             letter ++;
01394                         } else if(joystick.direction==LEFT) {
01395                             letter --;
01396                         }
01397                         break;
01398                     case 4:
01399                         name [1]='E';
01400                         lcd.printChar(name [0],32,2);
01401                         lcd.printChar(name [1],38,2);
01402                         lcd.printChar(name [2],44,2);
01403                         wait(g_dt );
01404                         if(joystick.direction==DOWN) {
01405                             letter2 ++;
01406                         } else if(joystick.direction==UP) {
01407                             letter2 --;
01408                         }
01409                         if(joystick.direction==RIGHT) {
01410                             letter ++;
01411                         } else if(joystick.direction==LEFT) {
01412                             letter --;
01413                         }
01414                         break;
01415                     case 5:
01416                         name [1]='F';
01417                         lcd.printChar(name [0],32,2);
01418                         lcd.printChar(name [1],38,2);
01419                         lcd.printChar(name [2],44,2);
01420                         wait(g_dt );
01421                         if(joystick.direction==DOWN) {
01422                             letter2 ++;
01423                         } else if(joystick.direction==UP) {
01424                             letter2 --;
01425                         }
01426                         if(joystick.direction==RIGHT) {
01427                             letter ++;
01428                         } else if(joystick.direction==LEFT) {
01429                             letter --;
01430                         }
01431                         break;
01432                     case 6:
01433                         name [1]='G';
01434                         lcd.printChar(name [0],32,2);
01435                         lcd.printChar(name [1],38,2);
01436                         lcd.printChar(name [2],44,2);
01437                         wait(g_dt );
01438                         if(joystick.direction==DOWN) {
01439                             letter2 ++;
01440                         } else if(joystick.direction==UP) {
01441                             letter2 --;
01442                         }
01443                         if(joystick.direction==RIGHT) {
01444                             letter ++;
01445                         } else if(joystick.direction==LEFT) {
01446                             letter --;
01447                         }
01448                         break;
01449                     case 7:
01450                         name [1]='H';
01451                         lcd.printChar(name [0],32,2);
01452                         lcd.printChar(name [1],38,2);
01453                         lcd.printChar(name [2],44,2);
01454                         wait(g_dt );
01455                         if(joystick.direction==DOWN) {
01456                             letter2 ++;
01457                         } else if(joystick.direction==UP) {
01458                             letter2 --;
01459                         }
01460                         if(joystick.direction==RIGHT) {
01461                             letter ++;
01462                         } else if(joystick.direction==LEFT) {
01463                             letter --;
01464                         }
01465                         break;
01466                     case 8:
01467                         name [1]='I';
01468                         lcd.printChar(name [0],32,2);
01469                         lcd.printChar(name [1],38,2);
01470                         lcd.printChar(name [2],44,2);
01471                         wait(g_dt );
01472                         if(joystick.direction==DOWN) {
01473                             letter2 ++;
01474                         } else if(joystick.direction==UP) {
01475                             letter2 --;
01476                         }
01477                         if(joystick.direction==RIGHT) {
01478                             letter ++;
01479                         } else if(joystick.direction==LEFT) {
01480                             letter --;
01481                         }
01482                         break;
01483                     case 9:
01484                         name [1]='J';
01485                         lcd.printChar(name [0],32,2);
01486                         lcd.printChar(name [1],38,2);
01487                         lcd.printChar(name [2],44,2);
01488                         wait(g_dt );
01489                         if(joystick.direction==DOWN) {
01490                             letter2 ++;
01491                         } else if(joystick.direction==UP) {
01492                             letter2 --;
01493                         }
01494                         if(joystick.direction==RIGHT) {
01495                             letter ++;
01496                         } else if(joystick.direction==LEFT) {
01497                             letter --;
01498                         }
01499                         break;
01500                     case 10:
01501                         name [1]='K';
01502                         lcd.printChar(name [0],32,2);
01503                         lcd.printChar(name [1],38,2);
01504                         lcd.printChar(name [2],44,2);
01505                         wait(g_dt );
01506                         if(joystick.direction==DOWN) {
01507                             letter2 ++;
01508                         } else if(joystick.direction==UP) {
01509                             letter2 --;
01510                         }
01511                         if(joystick.direction==RIGHT) {
01512                             letter ++;
01513                         } else if(joystick.direction==LEFT) {
01514                             letter --;
01515                         }
01516                         break;
01517                     case 11:
01518                         name [1]='L';
01519                         lcd.printChar(name [0],32,2);
01520                         lcd.printChar(name [1],38,2);
01521                         lcd.printChar(name [2],44,2);
01522                         wait(g_dt );
01523                         if(joystick.direction==DOWN) {
01524                             letter2 ++;
01525                         } else if(joystick.direction==UP) {
01526                             letter2 --;
01527                         }
01528                         if(joystick.direction==RIGHT) {
01529                             letter ++;
01530                         } else if(joystick.direction==LEFT) {
01531                             letter --;
01532                         }
01533                         break;
01534                     case 12:
01535                         name [1]='M';
01536                         lcd.printChar(name [0],32,2);
01537                         lcd.printChar(name [1],38,2);
01538                         lcd.printChar(name [2],44,2);
01539                         wait(g_dt );
01540                         if(joystick.direction==DOWN) {
01541                             letter2 ++;
01542                         } else if(joystick.direction==UP) {
01543                             letter2 --;
01544                         }
01545                         if(joystick.direction==RIGHT) {
01546                             letter ++;
01547                         } else if(joystick.direction==LEFT) {
01548                             letter --;
01549                         }
01550                         break;
01551                     case 13:
01552                         name [1]='N';
01553                         lcd.printChar(name [0],32,2);
01554                         lcd.printChar(name [1],38,2);
01555                         lcd.printChar(name [2],44,2);
01556                         wait(g_dt );
01557                         if(joystick.direction==DOWN) {
01558                             letter2 ++;
01559                         } else if(joystick.direction==UP) {
01560                             letter2 --;
01561                         }
01562                         if(joystick.direction==RIGHT) {
01563                             letter ++;
01564                         } else if(joystick.direction==LEFT) {
01565                             letter --;
01566                         }
01567                         break;
01568                     case 14:
01569                         name [1]='O';
01570                         lcd.printChar(name [0],32,2);
01571                         lcd.printChar(name [1],38,2);
01572                         lcd.printChar(name [2],44,2);
01573                         wait(g_dt );
01574                         if(joystick.direction==DOWN) {
01575                             letter2 ++;
01576                         } else if(joystick.direction==UP) {
01577                             letter2 --;
01578                         }
01579                         if(joystick.direction==RIGHT) {
01580                             letter ++;
01581                         } else if(joystick.direction==LEFT) {
01582                             letter --;
01583                         }
01584                         break;
01585                     case 15:
01586                         name [1]='P';
01587                         lcd.printChar(name [0],32,2);
01588                         lcd.printChar(name [1],38,2);
01589                         lcd.printChar(name [2],44,2);
01590                         wait(g_dt );
01591                         if(joystick.direction==DOWN) {
01592                             letter2 ++;
01593                         } else if(joystick.direction==UP) {
01594                             letter2 --;
01595                         }
01596                         if(joystick.direction==RIGHT) {
01597                             letter ++;
01598                         } else if(joystick.direction==LEFT) {
01599                             letter --;
01600                         }
01601                         break;
01602                     case 16:
01603                         name [1]='Q';
01604                         lcd.printChar(name [0],32,2);
01605                         lcd.printChar(name [1],38,2);
01606                         lcd.printChar(name [2],44,2);
01607                         wait(g_dt );
01608                         if(joystick.direction==DOWN) {
01609                             letter2 ++;
01610                         } else if(joystick.direction==UP) {
01611                             letter2 --;
01612                         }
01613                         if(joystick.direction==RIGHT) {
01614                             letter ++;
01615                         } else if(joystick.direction==LEFT) {
01616                             letter --;
01617                         }
01618                         break;
01619                     case 17:
01620                         name [1]='R';
01621                         lcd.printChar(name [0],32,2);
01622                         lcd.printChar(name [1],38,2);
01623                         lcd.printChar(name [2],44,2);
01624                         wait(g_dt );
01625                         if(joystick.direction==DOWN) {
01626                             letter2 ++;
01627                         } else if(joystick.direction==UP) {
01628                             letter2 --;
01629                         }
01630                         if(joystick.direction==RIGHT) {
01631                             letter ++;
01632                         } else if(joystick.direction==LEFT) {
01633                             letter --;
01634                         }
01635                         break;
01636                     case 18:
01637                         name [1]='S';
01638                         lcd.printChar(name [0],32,2);
01639                         lcd.printChar(name [1],38,2);
01640                         lcd.printChar(name [2],44,2);
01641                         wait(g_dt );
01642                         if(joystick.direction==DOWN) {
01643                             letter2 ++;
01644                         } else if(joystick.direction==UP) {
01645                             letter2 --;
01646                         }
01647                         if(joystick.direction==RIGHT) {
01648                             letter ++;
01649                         } else if(joystick.direction==LEFT) {
01650                             letter --;
01651                         }
01652                         break;
01653                     case 19:
01654                         name [1]='T';
01655                         lcd.printChar(name [0],32,2);
01656                         lcd.printChar(name [1],38,2);
01657                         lcd.printChar(name [2],44,2);
01658                         wait(g_dt );
01659                         if(joystick.direction==DOWN) {
01660                             letter2 ++;
01661                         } else if(joystick.direction==UP) {
01662                             letter2 --;
01663                         }
01664                         if(joystick.direction==RIGHT) {
01665                             letter ++;
01666                         } else if(joystick.direction==LEFT) {
01667                             letter --;
01668                         }
01669                         break;
01670                     case 20:
01671                         name [1]='U';
01672                         lcd.printChar(name [0],32,2);
01673                         lcd.printChar(name [1],38,2);
01674                         lcd.printChar(name [2],44,2);
01675                         wait(g_dt );
01676                         if(joystick.direction==DOWN) {
01677                             letter2 ++;
01678                         } else if(joystick.direction==UP) {
01679                             letter2 --;
01680                         }
01681                         if(joystick.direction==RIGHT) {
01682                             letter ++;
01683                         } else if(joystick.direction==LEFT) {
01684                             letter --;
01685                         }
01686                         break;
01687                     case 21:
01688                         name [1]='V';
01689                         lcd.printChar(name [0],32,2);
01690                         lcd.printChar(name [1],38,2);
01691                         lcd.printChar(name [2],44,2);
01692                         wait(g_dt );
01693                         if(joystick.direction==DOWN) {
01694                             letter2 ++;
01695                         } else if(joystick.direction==UP) {
01696                             letter2 --;
01697                         }
01698                         if(joystick.direction==RIGHT) {
01699                             letter ++;
01700                         } else if(joystick.direction==LEFT) {
01701                             letter --;
01702                         }
01703                         break;
01704                     case 22:
01705                         name [1]='W';
01706                         lcd.printChar(name [0],32,2);
01707                         lcd.printChar(name [1],38,2);
01708                         lcd.printChar(name [2],44,2);
01709                         wait(g_dt );
01710                         if(joystick.direction==DOWN) {
01711                             letter2 ++;
01712                         } else if(joystick.direction==UP) {
01713                             letter2 --;
01714                         }
01715                         if(joystick.direction==RIGHT) {
01716                             letter ++;
01717                         } else if(joystick.direction==LEFT) {
01718                             letter --;
01719                         }
01720                         break;
01721                     case 23:
01722                         name [1]='X';
01723                         lcd.printChar(name [0],32,2);
01724                         lcd.printChar(name [1],38,2);
01725                         lcd.printChar(name [2],44,2);
01726                         wait(g_dt );
01727                         if(joystick.direction==DOWN) {
01728                             letter2 ++;
01729                         } else if(joystick.direction==UP) {
01730                             letter2 --;
01731                         }
01732                         if(joystick.direction==RIGHT) {
01733                             letter ++;
01734                         } else if(joystick.direction==LEFT) {
01735                             letter --;
01736                         }
01737                         break;
01738                     case 24:
01739                         name [1]='Y';
01740                         lcd.printChar(name [0],32,2);
01741                         lcd.printChar(name [1],38,2);
01742                         lcd.printChar(name [2],44,2);
01743                         wait(g_dt );
01744                         if(joystick.direction==DOWN) {
01745                             letter2 ++;
01746                         } else if(joystick.direction==UP) {
01747                             letter2 --;
01748                         }
01749                         if(joystick.direction==RIGHT) {
01750                             letter ++;
01751                         } else if(joystick.direction==LEFT) {
01752                             letter --;
01753                         }
01754                         break;
01755                     case 25:
01756                         name [1]='Z';
01757                         lcd.printChar(name [0],32,2);
01758                         lcd.printChar(name [1],38,2);
01759                         lcd.printChar(name [2],44,2);
01760                         wait(g_dt );
01761                         if(joystick.direction==DOWN) {
01762                             letter2 =0;
01763                         } else if(joystick.direction==UP) {
01764                             letter2 --;
01765                         }
01766                         if(joystick.direction==RIGHT) {
01767                             letter ++;
01768                         } else if(joystick.direction==LEFT) {
01769                             letter --;
01770                         }
01771                         break;
01772                 }
01773                 break;
01774             case 2:
01775                 if(letter_counter >=2) {
01776                     lcd.setPixel(44,24);
01777                     lcd.setPixel(45,24);
01778                     lcd.setPixel(46,24);
01779                     lcd.setPixel(47,24);
01780                     lcd.setPixel(48,24);
01781                     if(letter_counter ==4) {
01782                         letter_counter =0;
01783                     } else {
01784                         letter_counter ++;
01785                     }
01786                 } else {
01787                     letter_counter ++;
01788                 }
01789                 switch(letter3 ) {
01790                     case 0:
01791                         name [2]='A';
01792                         lcd.printChar(name [0],32,2);
01793                         lcd.printChar(name [1],38,2);
01794                         lcd.printChar(name [2],44,2);
01795                         wait(g_dt );
01796                         if(joystick.direction==DOWN) {
01797                             letter3 ++;
01798                         } else if(joystick.direction==UP) {
01799                             letter3 =25;
01800                         }
01801                         if(joystick.direction==LEFT) {
01802                             letter --;
01803                         }
01804                         break;
01805                     case 1:
01806                         name [2]='B';
01807                         lcd.printChar(name [0],32,2);
01808                         lcd.printChar(name [1],38,2);
01809                         lcd.printChar(name [2],44,2);
01810                         wait(g_dt );
01811                         if(joystick.direction==DOWN) {
01812                             letter3 ++;
01813                         } else if(joystick.direction==UP) {
01814                             letter3 --;
01815                         }
01816                         if(joystick.direction==LEFT) {
01817                             letter --;
01818                         }
01819                         break;
01820                     case 2:
01821                         name [2]='C';
01822                         lcd.printChar(name [0],32,2);
01823                         lcd.printChar(name [1],38,2);
01824                         lcd.printChar(name [2],44,2);
01825                         wait(g_dt );
01826                         if(joystick.direction==DOWN) {
01827                             letter3 ++;
01828                         } else if(joystick.direction==UP) {
01829                             letter3 --;
01830                         }
01831                         if(joystick.direction==LEFT) {
01832                             letter --;
01833                         }
01834                         break;
01835                     case 3:
01836                         name [2]='D';
01837                         lcd.printChar(name [0],32,2);
01838                         lcd.printChar(name [1],38,2);
01839                         lcd.printChar(name [2],44,2);
01840                         wait(g_dt );
01841                         if(joystick.direction==DOWN) {
01842                             letter3 ++;
01843                         } else if(joystick.direction==UP) {
01844                             letter3 --;
01845                         }
01846                         if(joystick.direction==LEFT) {
01847                             letter --;
01848                         }
01849                         break;
01850                     case 4:
01851                         name [2]='E';
01852                         lcd.printChar(name [0],32,2);
01853                         lcd.printChar(name [1],38,2);
01854                         lcd.printChar(name [2],44,2);
01855                         wait(g_dt );
01856                         if(joystick.direction==DOWN) {
01857                             letter3 ++;
01858                         } else if(joystick.direction==UP) {
01859                             letter3 --;
01860                         }
01861                         if(joystick.direction==LEFT) {
01862                             letter --;
01863                         }
01864                         break;
01865                     case 5:
01866                         name [2]='F';
01867                         lcd.printChar(name [0],32,2);
01868                         lcd.printChar(name [1],38,2);
01869                         lcd.printChar(name [2],44,2);
01870                         wait(g_dt );
01871                         if(joystick.direction==DOWN) {
01872                             letter3 ++;
01873                         } else if(joystick.direction==UP) {
01874                             letter3 --;
01875                         }
01876                         if(joystick.direction==LEFT) {
01877                             letter --;
01878                         }
01879                         break;
01880                     case 6:
01881                         name [2]='G';
01882                         lcd.printChar(name [0],32,2);
01883                         lcd.printChar(name [1],38,2);
01884                         lcd.printChar(name [2],44,2);
01885                         wait(g_dt );
01886                         if(joystick.direction==DOWN) {
01887                             letter3 ++;
01888                         } else if(joystick.direction==UP) {
01889                             letter3 --;
01890                         }
01891                         if(joystick.direction==LEFT) {
01892                             letter --;
01893                         }
01894                         break;
01895                     case 7:
01896                         name [2]='H';
01897                         lcd.printChar(name [0],32,2);
01898                         lcd.printChar(name [1],38,2);
01899                         lcd.printChar(name [2],44,2);
01900                         wait(g_dt );
01901                         if(joystick.direction==DOWN) {
01902                             letter3 ++;
01903                         } else if(joystick.direction==UP) {
01904                             letter3 --;
01905                         }
01906                         if(joystick.direction==LEFT) {
01907                             letter --;
01908                         }
01909                         break;
01910                     case 8:
01911                         name [2]='I';
01912                         lcd.printChar(name [0],32,2);
01913                         lcd.printChar(name [1],38,2);
01914                         lcd.printChar(name [2],44,2);
01915                         wait(g_dt );
01916                         if(joystick.direction==DOWN) {
01917                             letter3 ++;
01918                         } else if(joystick.direction==UP) {
01919                             letter3 --;
01920                         }
01921                         if(joystick.direction==LEFT) {
01922                             letter --;
01923                         }
01924                         break;
01925                     case 9:
01926                         name [2]='J';
01927                         lcd.printChar(name [0],32,2);
01928                         lcd.printChar(name [1],38,2);
01929                         lcd.printChar(name [2],44,2);
01930                         wait(g_dt );
01931                         if(joystick.direction==DOWN) {
01932                             letter3 ++;
01933                         } else if(joystick.direction==UP) {
01934                             letter3 --;
01935                         }
01936                         if(joystick.direction==LEFT) {
01937                             letter --;
01938                         }
01939                         break;
01940                     case 10:
01941                         name [2]='K';
01942                         lcd.printChar(name [0],32,2);
01943                         lcd.printChar(name [1],38,2);
01944                         lcd.printChar(name [2],44,2);
01945                         wait(g_dt );
01946                         if(joystick.direction==DOWN) {
01947                             letter3 ++;
01948                         } else if(joystick.direction==UP) {
01949                             letter3 --;
01950                         }
01951                         if(joystick.direction==LEFT) {
01952                             letter --;
01953                         }
01954                         break;
01955                     case 11:
01956                         name [2]='L';
01957                         lcd.printChar(name [0],32,2);
01958                         lcd.printChar(name [1],38,2);
01959                         lcd.printChar(name [2],44,2);
01960                         wait(g_dt );
01961                         if(joystick.direction==DOWN) {
01962                             letter3 ++;
01963                         } else if(joystick.direction==UP) {
01964                             letter3 --;
01965                         }
01966                         if(joystick.direction==LEFT) {
01967                             letter --;
01968                         }
01969                         break;
01970                     case 12:
01971                         name [2]='M';
01972                         lcd.printChar(name [0],32,2);
01973                         lcd.printChar(name [1],38,2);
01974                         lcd.printChar(name [2],44,2);
01975                         wait(g_dt );
01976                         if(joystick.direction==DOWN) {
01977                             letter3 ++;
01978                         } else if(joystick.direction==UP) {
01979                             letter3 --;
01980                         }
01981                         if(joystick.direction==LEFT) {
01982                             letter --;
01983                         }
01984                         break;
01985                     case 13:
01986                         name [2]='N';
01987                         lcd.printChar(name [0],32,2);
01988                         lcd.printChar(name [1],38,2);
01989                         lcd.printChar(name [2],44,2);
01990                         wait(g_dt );
01991                         if(joystick.direction==DOWN) {
01992                             letter3 ++;
01993                         } else if(joystick.direction==UP) {
01994                             letter3 --;
01995                         }
01996                         if(joystick.direction==LEFT) {
01997                             letter --;
01998                         }
01999                         break;
02000                     case 14:
02001                         name [2]='O';
02002                         lcd.printChar(name [0],32,2);
02003                         lcd.printChar(name [1],38,2);
02004                         lcd.printChar(name [2],44,2);
02005                         wait(g_dt );
02006                         if(joystick.direction==DOWN) {
02007                             letter3 ++;
02008                         } else if(joystick.direction==UP) {
02009                             letter3 --;
02010                         }
02011                         if(joystick.direction==LEFT) {
02012                             letter --;
02013                         }
02014                         break;
02015                     case 15:
02016                         name [2]='P';
02017                         lcd.printChar(name [0],32,2);
02018                         lcd.printChar(name [1],38,2);
02019                         lcd.printChar(name [2],44,2);
02020                         wait(g_dt );
02021                         if(joystick.direction==DOWN) {
02022                             letter3 ++;
02023                         } else if(joystick.direction==UP) {
02024                             letter3 --;
02025                         }
02026                         if(joystick.direction==LEFT) {
02027                             letter --;
02028                         }
02029                         break;
02030                     case 16:
02031                         name [2]='Q';
02032                         lcd.printChar(name [0],32,2);
02033                         lcd.printChar(name [1],38,2);
02034                         lcd.printChar(name [2],44,2);
02035                         wait(g_dt );
02036                         if(joystick.direction==DOWN) {
02037                             letter3 ++;
02038                         } else if(joystick.direction==UP) {
02039                             letter3 --;
02040                         }
02041                         if(joystick.direction==LEFT) {
02042                             letter --;
02043                         }
02044                         break;
02045                     case 17:
02046                         name [2]='R';
02047                         lcd.printChar(name [0],32,2);
02048                         lcd.printChar(name [1],38,2);
02049                         lcd.printChar(name [2],44,2);
02050                         wait(g_dt );
02051                         if(joystick.direction==DOWN) {
02052                             letter3 ++;
02053                         } else if(joystick.direction==UP) {
02054                             letter3 --;
02055                         }
02056                         if(joystick.direction==LEFT) {
02057                             letter --;
02058                         }
02059                         break;
02060                     case 18:
02061                         name [2]='S';
02062                         lcd.printChar(name [0],32,2);
02063                         lcd.printChar(name [1],38,2);
02064                         lcd.printChar(name [2],44,2);
02065                         wait(g_dt );
02066                         if(joystick.direction==DOWN) {
02067                             letter3 ++;
02068                         } else if(joystick.direction==UP) {
02069                             letter3 --;
02070                         }
02071                         if(joystick.direction==LEFT) {
02072                             letter --;
02073                         }
02074                         break;
02075                     case 19:
02076                         name [2]='T';
02077                         lcd.printChar(name [0],32,2);
02078                         lcd.printChar(name [1],38,2);
02079                         lcd.printChar(name [2],44,2);
02080                         wait(g_dt );
02081                         if(joystick.direction==DOWN) {
02082                             letter3 ++;
02083                         } else if(joystick.direction==UP) {
02084                             letter3 --;
02085                         }
02086                         if(joystick.direction==LEFT) {
02087                             letter --;
02088                         }
02089                         break;
02090                     case 20:
02091                         name [2]='U';
02092                         lcd.printChar(name [0],32,2);
02093                         lcd.printChar(name [1],38,2);
02094                         lcd.printChar(name [2],44,2);
02095                         wait(g_dt );
02096                         if(joystick.direction==DOWN) {
02097                             letter3 ++;
02098                         } else if(joystick.direction==UP) {
02099                             letter3 --;
02100                         }
02101                         if(joystick.direction==LEFT) {
02102                             letter --;
02103                         }
02104                         break;
02105                     case 21:
02106                         name [2]='V';
02107                         lcd.printChar(name [0],32,2);
02108                         lcd.printChar(name [1],38,2);
02109                         lcd.printChar(name [2],44,2);
02110                         wait(g_dt );
02111                         if(joystick.direction==DOWN) {
02112                             letter3 ++;
02113                         } else if(joystick.direction==UP) {
02114                             letter3 --;
02115                         }
02116                         if(joystick.direction==LEFT) {
02117                             letter --;
02118                         }
02119                         break;
02120                     case 22:
02121                         name [2]='W';
02122                         lcd.printChar(name [0],32,2);
02123                         lcd.printChar(name [1],38,2);
02124                         lcd.printChar(name [2],44,2);
02125                         wait(g_dt );
02126                         if(joystick.direction==DOWN) {
02127                             letter3 ++;
02128                         } else if(joystick.direction==UP) {
02129                             letter3 --;
02130                         }
02131                         if(joystick.direction==LEFT) {
02132                             letter --;
02133                         }
02134                         break;
02135                     case 23:
02136                         name [2]='X';
02137                         lcd.printChar(name [0],32,2);
02138                         lcd.printChar(name [1],38,2);
02139                         lcd.printChar(name [2],44,2);
02140                         wait(g_dt );
02141                         if(joystick.direction==DOWN) {
02142                             letter3 ++;
02143                         } else if(joystick.direction==UP) {
02144                             letter3 --;
02145                         }
02146                         if(joystick.direction==LEFT) {
02147                             letter --;
02148                         }
02149                         break;
02150                     case 24:
02151                         name [2]='Y';
02152                         lcd.printChar(name [0],32,2);
02153                         lcd.printChar(name [1],38,2);
02154                         lcd.printChar(name [2],44,2);
02155                         wait(g_dt );
02156                         if(joystick.direction==DOWN) {
02157                             letter3 ++;
02158                         } else if(joystick.direction==UP) {
02159                             letter3 --;
02160                         }
02161                         if(joystick.direction==LEFT) {
02162                             letter --;
02163                         }
02164                         break;
02165                     case 25:
02166                         name [2]='Z';
02167                         lcd.printChar(name [0],32,2);
02168                         lcd.printChar(name [1],38,2);
02169                         lcd.printChar(name [2],44,2);
02170                         wait(g_dt );
02171                         if(joystick.direction==DOWN) {
02172                             letter3 =0;
02173                         } else if(joystick.direction==UP) {
02174                             letter3 --;
02175                         }
02176                         if(joystick.direction==LEFT) {
02177                             letter --;
02178                         }
02179                         break;
02180                 }
02181                 break;
02182         }
02183         sleep();
02184     }
02185     buzzer.tone(NOTE_C4,SEMIQUAVER);
02186 }
02187 
02188 void rules()
02189 {
02190     wait(g_dt );
02191     lcd.clear();
02192     while(!joystick.bbutton) {
02193         if(up_flag ) {
02194             up_flag =0;
02195             update_joystick();
02196         }
02197         lcd.clear();
02198         bat1.draw(0,7);
02199         spike_small_obj1.draw(0,23);
02200         spike_large_obj1.draw(6,23);
02201         lcd.printString("These are",14,0);
02202         lcd.printString("your health.",14,1);
02203         lcd.printString("Avoid these",14,2);
02204         lcd.printString("or you'll",14,3);
02205         lcd.printString("die!",14,4);
02206         lcd.printString("GOOD LUCK!",14,5);
02207         lcd.refresh();
02208         sleep();
02209     }
02210     buzzer.tone(NOTE_D3,SEMIQUAVER);
02211     menu();
02212 }
02213 
02214 void credits()
02215 {
02216     wait(g_dt );
02217     lcd.clear();
02218     lcd.printString("Cave Runner",10,0);
02219     lcd.printString("A Game By",13,2);
02220     lcd.printString("Marty Laverick",0,3);
02221     lcd.printString("Created For",9,4);
02222     lcd.printString("ELEC2645",14,5);
02223     while(!joystick.bbutton) {
02224         if(up_flag ) {
02225             up_flag =0;
02226             update_joystick();
02227         }
02228         sleep();
02229     }
02230     buzzer.tone(NOTE_D3,SEMIQUAVER);
02231     menu();
02232 }