Snake

Dependencies:   mbed

Fork of el17x2l by LI Xiang

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Menu.cpp Source File

Menu.cpp

00001 #include "Menu.h"
00002 
00003 Menu::Menu()
00004 {
00005 }
00006 
00007 Menu::~Menu()
00008 {
00009 }
00010 
00011 void Menu::init(Gamepad &pad,N5110 &lcd)
00012 {
00013     lcd.clear();
00014     lcd.printString("     SNAKE    ",0,1);
00015     lcd.printString("  Press Start ",0,4);
00016     lcd.refresh();
00017     // wait flashing LEDs until start button is pressed
00018     //start 键不等于false 的时候跳出去  也就是按下就跳出循环
00019     while (1) {
00020         pad.leds_on();
00021         wait(0.1);
00022         pad.leds_off();
00023         wait(0.1);
00024         if(pad.check_event(Gamepad::START_PRESSED) == true)
00025          break;
00026     }
00027     pad.tone(1000.0,0.1);
00028     changespeed1(pad,lcd);
00029     wait(0.5);
00030 }
00031 
00032 void Menu::changemap(Gamepad &apd,N5110 &lcd)
00033 {
00034     lcd.clear();
00035     lcd.printString("   Rookie   ",0,1);
00036     lcd.printString("   
00037 
00038 void Menu::changespeed1(Gamepad &pad,N5110 &lcd)
00039 {
00040     lcd.clear();
00041     lcd.printString(" Change Speed ",0,1);
00042     lcd.printString("  < Normal >  ",0,3);
00043     lcd.refresh();
00044     while (1) {
00045          wait(0.2);
00046          if(pad.check_event(Gamepad::START_PRESSED) == true)
00047          break;
00048          if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00049              pad.tone(1500.0,0.1);
00050              init(pad,lcd);
00051              break;
00052              }
00053          d = pad.get_direction();
00054          wait(0.1);
00055          if (d == W) {
00056          changespeed2(pad,lcd);
00057              }
00058          else if (d == E) {
00059              changespeed3(pad,lcd);
00060              }
00061              }
00062     pad.tone(1000.0,0.1);
00063     speed = 3;
00064     sv = 15;
00065     game(pad,lcd);
00066 }
00067 
00068 void Menu::changespeed2(Gamepad &pad,N5110 &lcd)
00069 {
00070     lcd.clear();
00071     lcd.printString(" Change Speed ",0,1);
00072     lcd.printString("  <  Easy  >  ",0,3);
00073     lcd.refresh();
00074     while (1) {
00075          wait(0.2);
00076          if(pad.check_event(Gamepad::START_PRESSED) == true)
00077          break;
00078          if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00079              pad.tone(1500.0,0.1);
00080              init(pad,lcd);
00081              break;
00082              }
00083          d = pad.get_direction();
00084          wait(0.1);
00085          if(d == E) {
00086              changespeed1(pad,lcd);
00087            }
00088            }
00089     pad.tone(1000.0,0.1);
00090     speed = 2;
00091     sv = 10;
00092     game(pad,lcd);
00093 }
00094 
00095 void Menu::changespeed3(Gamepad &pad,N5110 &lcd)
00096 {
00097     lcd.clear();
00098     lcd.printString(" Change Speed ",0,1);
00099     lcd.printString("  <  Hard  >  ",0,3);
00100     lcd.refresh();
00101     while (1) {
00102          wait(0.2);
00103          if(pad.check_event(Gamepad::START_PRESSED) == true)
00104          break;
00105          if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00106              pad.tone(1500.0,0.1);
00107              init(pad,lcd);
00108              break;
00109              }
00110          d = pad.get_direction();
00111          wait(0.1);
00112          if(d == W) {
00113          changespeed1(pad,lcd);
00114          }
00115     }
00116     pad.tone(1000.0,0.1);
00117     speed = 5;
00118     sv = 20;
00119     game(pad,lcd);
00120 }
00121 
00122 // this function draws each frame on the LCD
00123 void Menu::render(Gamepad &pad,N5110 &lcd)
00124 {
00125     // clear screen, re-draw and refresh
00126     eatfood(pad,lcd);
00127     printscore(lcd);
00128     lcd.refresh();
00129 }
00130 
00131 void Menu::eatfood(Gamepad &pad,N5110 &lcd)
00132 {
00133     Vector2D food_pos = _food.get_pos();
00134     _snake.backcurr();
00135     _snake.update(pad);
00136     lcd.drawRect(3,8,78,38,FILL_TRANSPARENT);
00137     _snake.move(lcd);
00138     //int i=0;
00139     //while (1){
00140     //if (i==3)
00141     //break;
00142     if ((_snake.gethead()->x == food_pos.x-1) && (_snake.gethead()->y == food_pos.y-1)){
00143         pad.tone(750.0,0.1);
00144         lcd.setPixel(food_pos.x,food_pos.y,false);
00145         lcd.setPixel(food_pos.x+1,food_pos.y,false);
00146         lcd.setPixel(food_pos.x,food_pos.y+1,false);
00147         lcd.setPixel(food_pos.x+1,food_pos.y+1,false);
00148         _snake.grow();
00149         _food.creat(lcd);
00150         _snake.backcurr();
00151         add_score();  
00152        // i++;
00153         }
00154     }
00155     /*
00156     lcd.setPixel(food_pos.x,food_pos.y-1,true);
00157     lcd.setPixel(food_pos.x-1,food_pos.y,true);
00158     lcd.setPixel(food_pos.x+1,food_pos.y-1,true);
00159     lcd.setPixel(food_pos.x-2,food_pos.y,true);
00160     if ((_snake.gethead()->x == food_pos.x-1) && (_snake.gethead()->y == food_pos.y-1)){
00161         pad.tone(750.0,0.1);
00162         lcd.setPixel(food_pos.x,food_pos.y,false);
00163         lcd.setPixel(food_pos.x+1,food_pos.y,false);
00164         lcd.setPixel(food_pos.x,food_pos.y+1,false);
00165         lcd.setPixel(food_pos.x+1,food_pos.y+1,false);
00166         lcd.setPixel(food_pos.x,food_pos.y-1,false);
00167         lcd.setPixel(food_pos.x-1,food_pos.y,false);
00168         lcd.setPixel(food_pos.x+1,food_pos.y-1,false);
00169         lcd.setPixel(food_pos.x-2,food_pos.y,false);
00170         _food.creat(lcd);
00171         _snake.backcurr();
00172         add_score();  
00173         i=0;
00174         }
00175 }
00176 */
00177     
00178 void Menu::printscore(N5110 &lcd)
00179 {
00180     int score = get_score();
00181     
00182     char buffer[14];
00183     sprintf(buffer,"Score :  %2d  ",score);
00184     lcd.printString(buffer,4,0);   
00185     }
00186 
00187 void Menu::game(Gamepad &pad,N5110 &lcd)
00188 {
00189     int fps = speed;
00190     wait(0.1);
00191     lcd.clear();
00192     wait(0.1);
00193     _food.creat(lcd);
00194    while (1) {
00195         gameover(lcd);
00196         render(pad,lcd);
00197         wait(1.0f/fps);
00198         if (pad.check_event(Gamepad::START_PRESSED) == true)
00199         break;
00200         }
00201     pad.tone(1000.0,0.1);
00202     pause(pad,lcd);
00203 }
00204 
00205 void Menu::pause(Gamepad &pad,N5110 &lcd)
00206 {
00207     while (1) {
00208         lcd.clear();
00209         printscore(lcd);
00210         lcd.printString("     Pause   ",0,3);
00211         lcd.drawRect(3,8,78,38,FILL_TRANSPARENT);
00212         lcd.refresh();
00213         if (pad.check_event(Gamepad::BACK_PRESSED) == true)
00214         break;
00215         }
00216     pad.tone(1500.0,0.1);
00217     game(pad,lcd);
00218 }
00219   
00220 void Menu::gameover(N5110 &lcd)
00221 {
00222      while(
00223         (_snake.gethead()->x < 4)|| 
00224         (_snake.gethead()->x > 77)||//78+3-4
00225         (_snake.gethead()->y < 9)||
00226         (_snake.gethead()->y > 41)//38+7-4
00227         ){
00228             lcd.clear();
00229             lcd.printString("  GAME  OVER   ",0,3);
00230             lcd.refresh();
00231             lcd.inverseMode();
00232             wait(0.5);
00233             lcd.normalMode();
00234             wait(0.5);
00235          }         
00236 }
00237 
00238 void Menu::add_score()
00239 {
00240     _score += sv;
00241     }
00242     
00243 int Menu::get_score()
00244 {
00245     return _score;
00246     }