Snake game snake library
Diff: Snake.cpp
- Revision:
- 10:f63560a7e7ed
- Parent:
- 9:e023c11f7737
diff -r e023c11f7737 -r f63560a7e7ed Snake.cpp --- a/Snake.cpp Sat Apr 27 06:18:17 2019 +0000 +++ b/Snake.cpp Sun Apr 28 05:49:11 2019 +0000 @@ -50,9 +50,9 @@ snak._dirc[_length]=1; } -void Snake::drawsnake(N5110 &lcd){ - check_WallCollision(lcd);// if head==wall game over - check_TailCollision(lcd); +void Snake::drawsnake(N5110 &lcd, Gamepad &pad){ + check_WallCollision(lcd,pad);// if head==wall game over + check_TailCollision(lcd,pad); if (live!=0){ lcd.clear(); waitExpect=((float)_length/3)+5;//set wait so the game is speeding up @@ -163,7 +163,7 @@ } }//end of addPoint -void Snake::dead(N5110 &lcd){ +int Snake::dead(N5110 &lcd, Gamepad &pad){ live--; //take a life away @@ -172,42 +172,470 @@ lcd.clear(); lcd.printString("Game Over",0,1); lcd.printString("Press Reset",0,2); - lcd.printString("To restart",0,3); - lcd.refresh(); + lcd.printString("To Back",0,4); + lcd.refresh(); + + pad.tone(0.0,0.5); //gap + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.25); //1. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.25); //2. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.25); //3. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.25); //1. + wait(0.25); + pad.tone(784.0,0.75); //5- + wait(0.75); + pad.tone(659.0,0.25); //3. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.5); //2 + wait(0.5); + pad.tone(784.0,0.5); //5 + wait(0.5); + pad.tone(587.0,0.5); //2 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.25); //1. + wait(0.25); + pad.tone(440.0,0.25); //l6. + wait(0.25); + pad.tone(659.0,0.75); //3- + wait(0.75); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.25); //1. + wait(0.25); + pad.tone(494.0,0.5); //l7 + wait(0.5); + pad.tone(0.0,0.5); //gap + wait(0.5); + pad.tone(494.0,0.5); //l7 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(440.0,0.5); //l6 + wait(0.5); + pad.tone(494.0,0.5); //l7 + wait(0.5); + pad.tone(523.0,0.25); //1. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(392.0,0.5); //l5 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.5); //1 + wait(0.5); + pad.tone(587.0,0.25); //2. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(698.0,0.5); //4 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(698.0,0.25); //4. + wait(0.25); + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(587.0,0.25); //2. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.25); //1. + wait(0.25); + pad.tone(587.0,0.5); //2 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(0.0,5); //gap + wait(0.5); + pad.tone(523.0,0.25); //1. + wait(0.25); + pad.tone(587.0,0.25); //2. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(523.0,0.25); //1. + wait(0.25); + pad.tone(784.0,0.75); //5- + wait(0.75); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(587.0,0.5); //2 + wait(0.5); + pad.tone(784.0,0.5); //5 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.5); //2 + wait(0.5); + pad.tone(523.0,0.25); //1. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(440.0,0.25); //l6. + wait(0.25); + pad.tone(440.0,0.5); //l6 + wait(0.5); + pad.tone(494.0,0.25); //l7. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.25); //1. + wait(0.25); + pad.tone(392.0,0.5); //l5 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(0.0,0.5); //gap + wait(0.5); + pad.tone(0.0,0.25); //gap. + wait(0.25); + pad.tone(494.0,0.5); //l7 + wait(0.5); + pad.tone(523.0,0.25); //1. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(392.0,0.5); //l5 + wait(0.5); + pad.tone(523.0,0.5); //1 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(698.0,0.5); //4 + wait(0.5); + pad.tone(698.0,0.25); //4. + wait(0.25); + pad.tone(659.0,0.25); //3. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(523.0,0.25); //1. + wait(0.25); + pad.tone(523.0,0.5); //1 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(0.0,0.5); //gap + wait(0.5); + pad.tone(0.0,0.5); //gap + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(698.0,0.25); //4. + wait(0.25); + pad.tone(784.0,0.5); //5 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(784.0,0.5); //5 + wait(0.5); + pad.tone(784.0,0.5); //5 + wait(0.5); + pad.tone(784.0,0.5); //5 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(784.0,0.25); //5. + wait(0.25); + pad.tone(880.0,0.25); //6. + wait(0.25); + pad.tone(784.0,0.25); //5. + wait(0.25); + pad.tone(698.0,0.25); //4. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.5); //3 + wait(0.5); + pad.tone(659.0,0.5); //3 + wait(0.5); + pad.tone(659.0,0.5); //3 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.5); //3 + wait(0.5); + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(698.0,0.25); //4. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(523.0,0.5); //1 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.5); //1 + wait(0.5); + pad.tone(523.0,0.5); //1. + wait(0.5); + pad.tone(494.0,0.25); //l7. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(440.0,0.5); //l6 + wait(0.5); + pad.tone(494.0,0.5); //l7 + wait(0.5); + pad.tone(494.0,0.25); //l7. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.25); //1. + wait(0.25); + pad.tone(587.0,0.5); //2 + wait(0.5); + pad.tone(587.0,0.25); //2. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(659.0,0.25); //3. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.5); //2 + wait(0.5); + pad.tone(0.0,0.5); //gap + wait(0.5); + pad.tone(659.0,0.25); //3. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(698.0,0.25); //4. + wait(0.25); + pad.tone(784.0,0.5); //5 + wait(0.5); + pad.tone(784.0,0.5); //5 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(784.0,0.5); //5 + wait(0.5); + pad.tone(784.0,0.5); //5 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(784.0,0.25); //5. + wait(0.25); + pad.tone(880.0,0.25); //6. + wait(0.25); + pad.tone(784.0,0.25); //5. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(698.0,0.25); //4. + wait(0.25); + pad.tone(659.0,0.5); //3 + wait(0.5); + pad.tone(659.0,0.5); //3 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.5); //3 + wait(0.5); + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(698.0,0.25); //4. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(587.0,0.25); //2. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.25); //1. + wait(0.25); + + pad.tone(494.0,0.25); //l7. + wait(0.25); + pad.tone(440.0,0.5); //l6 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(440.0,0.25); //l6. + wait(0.25); + pad.tone(494.0,0.25); //l7. + wait(0.25); + pad.tone(523.0,0.25); //1. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(392.0,0.5); //l5 + wait(0.5); + pad.tone(523.0,0.5); //1 + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(659.0,0.25); //3. + wait(0.25); + pad.tone(587.0,0.75); //2- + wait(0.75); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(587.0,0.25); //2. + wait(0.25); + pad.tone(523.0,0.25); //1. + wait(0.25); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + pad.tone(523.0,0.5); //1 + wait(0.5); + pad.tone(0.0,0.5); //gap + wait(0.5); + pad.tone(0.0,0.5); //gap + wait(0.5); + if(pad.check_event(Gamepad::BACK_PRESSED) == true){ + return 0; + } + + } + if(Highest_score < (_length - 5)){ + Highest_score = _length - 5; } init(initx,inity,initl,live); //end of deadSnake + return 0; } void Snake::Tone_1(Gamepad &pad){ pad.tone(750.0,0.1); } -void Snake::check_WallCollision(N5110 &lcd){ +void Snake::check_WallCollision(N5110 &lcd, Gamepad &pad){ if (snak._x[_length] == 0 || snak._x[_length] == WIDTH)//if snake head hits side walls - { - dead(lcd); + { + game_music(pad); + dead(lcd,pad); } if (snak._y[_length]== FLOOR ||snak._y[_length]== CEILING )//if snake hits top or bottom walls - { - dead(lcd); + { + game_music(pad); + dead(lcd,pad); } }//end of checkWallCollision -void Snake::check_TailCollision(N5110 &lcd){//if snake eats itself +void Snake::check_TailCollision(N5110 &lcd, Gamepad &pad){//if snake eats itself for (int i=0 ;_length<i ;i++){ if (snak._x[_length-1]==snak._x[i] && snak._y[_length-1]==snak._y[i]) { - dead(lcd); + game_music(pad); + dead(lcd,pad); } } }//end of checkTailCollision +void Snake::game_music(Gamepad &pad){ + + pad.tone(1046.0,0.125); + wait(0.125); + pad.tone(1075.0,0.125); + wait(0.125); + pad.tone(1318.0,0.125); + wait(0.125); + } + void Snake::drawscore(N5110 &lcd){ char buffer1[14]; char buffer2[14];