Arturs Kozlovskis 201253737

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Menu.cpp Source File

Menu.cpp

00001 #include "mbed.h"
00002 #include "Menu.h"
00003 #include "N5110.h"
00004 #include "Gamepad.h"
00005 #include "Objects.h"
00006 
00007 //the big cannon sprite for the initial screen animation
00008 int _big_cannon[14][17] = {
00009     { 0,0,0,0,0,1,1,1,1,0,0,0,0,0 },
00010     { 0,0,0,0,0,1,1,1,1,0,0,0,0,0 },
00011     { 0,0,0,0,0,1,1,1,1,0,0,0,0,0 },
00012     { 0,0,0,0,0,1,1,1,1,0,0,0,0,0 },
00013     { 0,0,0,1,1,1,1,1,1,1,1,0,0,0 },
00014     { 0,0,0,1,1,1,1,1,1,1,1,0,0,0 },
00015     { 0,0,0,1,1,1,1,1,1,1,1,0,0,0 },
00016     { 0,0,0,1,1,1,1,1,1,1,1,0,0,0 },
00017     { 0,0,0,1,1,1,1,1,1,1,1,0,0,0 },
00018     { 1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
00019     { 1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
00020     { 1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
00021     { 1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
00022     { 1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
00023 
00024 };
00025 //cannon sprite for menu screen animation
00026 int _small_cannon[6][6] = {
00027     { 0,0,1,1,0,0 },
00028     { 0,0,1,1,0,0 },
00029     { 0,1,1,1,1,0 },
00030     { 0,1,1,1,1,0 },
00031     { 1,1,1,1,1,1 },
00032     { 1,1,1,1,1,1 },
00033 };
00034 
00035 //the arrow sprite for the menu
00036 int arrow[7][6] = {
00037     {1,0,1,0,0,0},
00038     {0,1,0,1,0,0},
00039     {0,0,1,0,1,0},
00040     {0,0,0,1,0,1},
00041     {0,0,1,0,1,0},
00042     {0,1,0,1,0,0},
00043     {1,0,1,0,0,0},
00044 
00045 };
00046 
00047 //initialising the gamepad and the screen as well as some variables
00048 Menu::Menu(N5110 &lcd,Gamepad &pad)
00049 {
00050     _pad = pad;
00051     _lcd = lcd;
00052     _arrow_pos = 16;
00053     _arrow_pos_settings = 8;
00054     _game_speed = "normal";
00055     strcpy(print_speed, _game_speed.c_str());
00056 }
00057 
00058 void Menu::first_screen()
00059 {
00060     //first number:x ; second nmber:y
00061     int circle_two_pos[2] = {35,20};
00062     int circle_three_pos[2] = {23,26};
00063     int circle_four_pos[2] = {17,35};
00064     int circle_five_pos[2] = {70,25};
00065 
00066     int LEDS = 0;
00067     //toggling all the leds
00068     for(int i = 0; i < 29; i++) {
00069         if(LEDS == 0) {
00070             LEDS = 1;
00071             _pad.leds(LEDS);
00072         } else {
00073             LEDS = 0;
00074             _pad.leds(LEDS);
00075         }
00076         //clears the screen
00077         _lcd.clear();
00078         _lcd.printString("CANNON SMASH!",5,1);
00079         _lcd.drawSprite(35,34,14,17,(int *)_big_cannon);
00080         _lcd.drawRect(0,46,84,2,FILL_BLACK);
00081         //makes the ball animation
00082         if (i > 14) {
00083 
00084             _lcd.drawCircle(circle_two_pos[0],circle_two_pos[1],2,FILL_BLACK);
00085             _lcd.drawCircle(circle_three_pos[0],circle_three_pos[1],3,FILL_BLACK);
00086             _lcd.drawCircle(circle_four_pos[0],circle_four_pos[1],2,FILL_BLACK);
00087             _lcd.drawCircle(circle_five_pos[0],circle_five_pos[1],9,FILL_BLACK);
00088 
00089             //makes the second circle's animation
00090             circle_two_pos[1] = circle_two_pos[1] + 1;
00091             if(circle_two_pos[1] == 21 || circle_two_pos[1] == 23 || circle_two_pos[1] == 25 || circle_two_pos[1] == 27 || circle_two_pos[1] == 29 ) {
00092                 circle_two_pos[0] += 1;
00093             }
00094             //makes the third cirlce's animation
00095             circle_three_pos[0] += 1;
00096             circle_three_pos[1] += 1;
00097             //makes the fourth circle's animation
00098             circle_four_pos[0] += 1;
00099             if(circle_four_pos[0] == 19 || circle_four_pos[0] == 21 || circle_four_pos[0] == 23 || circle_four_pos[0] == 25 || circle_four_pos[0] == 26 || circle_four_pos[0] == 27 || circle_four_pos[0] == 29 ) {
00100                 circle_four_pos[1] += 1;
00101             }
00102             //makes the fifth circle's animation
00103             circle_five_pos[0] += -1;
00104             circle_five_pos[1] += 1;
00105         }
00106         //refreshes the screen
00107         _lcd.refresh();
00108         wait(0.1);
00109     }
00110     //sets the LEDs on
00111     _pad.leds(1);
00112 }
00113 
00114 void Menu::menu_screen()
00115 {
00116     //runs the menu
00117     while(1) {
00118         //if button A pressed exit the loop and go to the chosen option
00119         while(!(_pad.A_held())) {
00120             //prints the options for player to choose
00121             _lcd.clear();
00122             _lcd.drawSprite(2,_arrow_pos,7,6,(int*) arrow);
00123             _lcd.printString("CANNON SMASH!",5,0);
00124             _lcd.printString("Play", 10,2);
00125             _lcd.printString("Settings",10,3);
00126             _lcd.printString("Instructions",10,4);
00127             _lcd.drawRect(0,47,84,1,FILL_BLACK);
00128             _lcd.drawSprite(0,41,6,6,(int*)_small_cannon);
00129             _lcd.drawSprite(19,41,6,6,(int*)_small_cannon);
00130             _lcd.drawSprite(38,41,6,6,(int*)_small_cannon);
00131             _lcd.drawSprite(58,41,6,6,(int*)_small_cannon);
00132             _lcd.drawSprite(78,41,6,6,(int*)_small_cannon);
00133             _lcd.refresh();
00134 
00135             //pressing x: goes up the menu
00136             if(_pad.X_held() && _arrow_pos > 16) {
00137                 _arrow_pos += -8;
00138             }
00139             //pressing b: goes down the menu
00140             if(_pad.B_held() && _arrow_pos < 32) {
00141                 _arrow_pos += 8;
00142             }
00143             //makes two button presses not overlap
00144             wait(0.12);
00145         }
00146         //the chosen option execution. Decided on where the arrow indicator was
00147         //arrow_pos 16 goes to setting's option
00148         if(_arrow_pos == 24) {
00149             //clears the lcd screen
00150             _lcd.clear();
00151             char buf_contrast[5];
00152             sprintf(buf_contrast,"%f",_pad.read_pot1());
00153 
00154             //while Y button is not pressed keep doing the loop
00155             while(!(_pad.Y_held())) {
00156                 //draws the seeting's options
00157                 _lcd.clear();
00158                 _lcd.drawSprite(2, _arrow_pos_settings, 7, 6, (int*)arrow);
00159                 _lcd.printString("Game speed:", 10, 1);
00160                 _lcd.printString(print_speed,10,2);
00161                 _lcd.printString("Contrast:", 10, 3);
00162                 _lcd.printString(buf_contrast,10,4);
00163                 _lcd.refresh();
00164                 //pressing x makes the arrow go up
00165                 if(_pad.X_held() && _arrow_pos_settings > 8) {
00166                     _arrow_pos_settings += -16;
00167                 }
00168                 //pressing b makes the arrow go down
00169                 if(_pad.B_held() && _arrow_pos_settings < 24) {
00170                     _arrow_pos_settings += 16;
00171                 }
00172                 // if setting arrow position 8 change the game speed depending on the pot 2 value
00173                 if(_arrow_pos_settings == 8) {
00174 
00175                     if(_pad.read_pot2() >= 0.5f) {
00176                         _game_speed = "fast";
00177                         strcpy(print_speed, _game_speed.c_str());
00178                     } else if(_pad.read_pot2() < 0.5f) {
00179                         _game_speed = "normal";
00180                         strcpy(print_speed, _game_speed.c_str());
00181                     }
00182                     // if settings arrow position 24 change the screen contrast
00183                 } else if(_arrow_pos_settings == 24) {
00184                     _lcd.setContrast(_pad.read_pot1());
00185                     sprintf(buf_contrast,"%f",_pad.read_pot1());
00186                 }
00187                 wait(0.05);
00188             }
00189         }
00190         // if arrow position is 24 display the instuctions
00191         if (_arrow_pos == 32) {
00192             while(!(_pad.Y_held())) {
00193                 _lcd.clear();
00194                 _lcd.printString("Shoot: X",0,1);
00195                 _lcd.printString("Move: Joystick",0,2);
00196                 _lcd.printString("Pause: A",0,3);
00197                 _lcd.printString("Go back: Y",0,4);
00198                 _lcd.refresh();
00199                 wait(0.1);
00200             }
00201         }
00202         //if arrow position is 8 goes into the game
00203         if (_arrow_pos == 16) {
00204             break;
00205         }
00206     }
00207 }
00208 
00209 string Menu:: get_game_speed()
00210 {
00211     return _game_speed;
00212 }
00213 
00214 int Menu::pause_screen(int score)
00215 {
00216     //when playing if pressed A go in to pause mode
00217     if(_pad.A_held()) {
00218 
00219         while(1) {
00220             //prints pause screen
00221             _lcd.clear();
00222             sprintf(buf_score,"%d",score);
00223             _lcd.printString("Score:",0,0);
00224             _lcd.printString(buf_score,36,0);
00225             _lcd.printString("PAUSED:",10,2);
00226             _lcd.printString("Continue:A",15,3);
00227             _lcd.printString("Menu:Y",15,4);
00228             _lcd.refresh();
00229             //keeps two presses not overlap
00230             wait(0.2);
00231             //if A pressed continue with the game
00232             if(_pad.A_held()) {
00233                 return 0;
00234             }
00235             //if Y pressed go back to menu
00236             if(_pad.Y_held()) {
00237                 return 1;
00238             }
00239         }
00240     }
00241      return 0;
00242 }
00243 
00244 int Menu::game_over_screen(int score)
00245 {
00246     while(1) {
00247         //prints the "game over" and displays the score
00248         _lcd.clear();
00249         _lcd.printString("Game over!", 15,2);
00250         _lcd.printString("Play:A",5,4);
00251         _lcd.printString("Go back:Y",5,5);
00252         sprintf(buf_score,"%d",score);
00253         _lcd.printString("Score:",0,0);
00254         _lcd.printString(buf_score,36,0);
00255         _lcd.refresh();
00256         //if A pressed start a new game
00257         if(_pad.A_held()) {
00258             return 2;
00259         }
00260         //if Y pressed go back to menu
00261         if(_pad.Y_held()) {
00262             return 3;
00263         }
00264         wait(0.1);
00265     }
00266 }