Snake game snake library

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Snake.cpp Source File

Snake.cpp

00001 #include "Snake.h"
00002 
00003 
00004 #define WIDTH 84
00005 #define HEIGHT 48
00006 #define CEILING 8
00007 #define FLOOR 48
00008 
00009 snakePart snak;
00010 int LIFE[7][7] = {
00011     
00012     {0,1,0,0,0,1,0},
00013     {1,1,1,0,1,1,1},
00014     {1,1,1,1,1,1,1},
00015     {0,1,1,1,1,1,0},
00016     {0,0,1,1,1,0,0},
00017     {0,0,0,1,0,0,0},
00018     };
00019 
00020 
00021 Snake::Snake()
00022 {
00023 
00024 }
00025 Snake::~Snake()
00026 {
00027   
00028 }
00029 
00030 /************************Functions************************/
00031 
00032 void Snake::init(int x, int y, int length, int _live){//initalizing the starting variables     
00033     live=_live;
00034     _length = length;    
00035     _food.init();
00036     initx=x;
00037     inity=y;
00038     initl=length;
00039     snak._x[0] = x;
00040     snak._y[0] = y;
00041     snak._dirc[0] = length;
00042     for (int i=1; i < _length ;i++)
00043     {   
00044         snak._x[i]=snak._x[0]+i;
00045         snak._y[i]=snak._y[0];
00046         snak._dirc[i]=1;
00047     }
00048     snak._x[_length]=x+_length;
00049     snak._y[_length]=y;
00050     snak._dirc[_length]=1;
00051 }
00052 
00053 void Snake::drawsnake(N5110 &lcd, Gamepad &pad){
00054         check_WallCollision(lcd,pad);// if head==wall game over
00055         check_TailCollision(lcd,pad);
00056             if (live!=0){
00057                 lcd.clear();
00058                 waitExpect=((float)_length/3)+5;//set wait so the game is speeding up
00059                 waitTime=(1/(waitExpect+_speed));
00060                 wait(waitTime);                
00061                 _food.drawfood(lcd);//make first food
00062                 lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);//draw arena
00063                 drawscore(lcd);
00064                 for ( int i=0; _length>i;i++){//draw snake
00065                     
00066                                     lcd.setPixel(snak._x[i],snak._y[i]);
00067                         
00068                 
00069                 lcd.refresh();
00070                 }
00071        }
00072        
00073 }
00074 void Snake::snakemov(Gamepad &pad){
00075      
00076      if (live!=0){
00077     
00078             d=pad.get_direction();
00079     
00080             if (snak._dirc[_length-1]==1)//set direction according _dir
00081                 {
00082                     snak._x[_length]++;                    
00083                 }
00084             
00085             if (snak._dirc[_length-1]==2)
00086                 {
00087                     snak._y[_length]--;
00088                 
00089                 }
00090             
00091             if (snak._dirc[_length-1]==3)
00092                 {
00093                     snak._x[_length]--;                    
00094                 }
00095                 
00096             if (snak._dirc[_length-1]==4)
00097                 {
00098                     snak._y[_length]++;                                        
00099                 }
00100             
00101             for (int i=0 ;_length<i ;i++)
00102                 {                      
00103                     snak._x[i]=snak._x[i+1];
00104                     snak._y[i]=snak._y[i+1];
00105                     //printf("done");                    
00106                 }
00107             //check dpad which way it is pointing
00108             //set direction accordingly, 1 is right, up is 2, 3 is left and 4 is down        
00109             if (d==N){// if stick points up, go up
00110                 
00111                         if (snak._dirc[_length-1]!=4)
00112                             {
00113                                 snak._dirc[_length-1] = 2;                                
00114                             }
00115 
00116             }
00117             
00118             if (d==E){// if stick points right, go right
00119                 
00120                         if (snak._dirc[_length-1]!=3)
00121                             {
00122                                 snak._dirc[_length-1] = 1;                                
00123                             }
00124                 
00125                 }
00126                 
00127             if (d==W){// if stick points left, go left
00128                 
00129                         if (snak._dirc[_length-1]!=1)
00130                             {
00131                                 snak._dirc[_length-1] = 3;
00132                             }
00133                 
00134                 
00135                 }
00136                 
00137             if (d==S){// if stick points down, go down 
00138                 
00139                         if (snak._dirc[_length-1]!=2)
00140                             {
00141                                 snak._dirc[_length-1] = 4;
00142                             }
00143                 
00144                 
00145                 }
00146         
00147             printf("updated ");
00148             eat(pad);
00149         }//live loop
00150 }//end of update
00151 
00152 void Snake::eat(Gamepad &pad){
00153     
00154     Foodpos foodPos = _food.returnPos();
00155     if(snak._x[_length-1]==foodPos.x && snak._y[_length-1]== foodPos.y)
00156         {
00157             snak._x[_length+1]=snak._x[_length];//-1
00158             snak._y[_length+1]=snak._y[_length];
00159             snak._dirc[_length+1]=snak._dirc[_length-1];
00160             _length=_length+1;//if head == food, _length++
00161             _food.createfood();//spawn new food
00162             Tone_1(pad);
00163         }
00164 }//end of addPoint
00165 
00166 int Snake::dead(N5110 &lcd, Gamepad &pad){
00167         
00168        
00169         live--; //take a life away
00170         //while (live==0){//dead reset game
00171         while(live==0) {
00172             lcd.clear();
00173             lcd.printString("Game Over",0,1);
00174             lcd.printString("Press Reset",0,2);
00175             lcd.printString("To Back",0,4);
00176             lcd.refresh();
00177             
00178             pad.tone(0.0,0.5); //gap
00179             wait(0.5);
00180             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00181                   return 0;
00182                 } 
00183             pad.tone(523.0,0.25); //1.
00184             wait(0.25);
00185             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00186                   return 0;
00187                 } 
00188             pad.tone(587.0,0.25); //2.
00189             wait(0.25);
00190             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00191                   return 0;
00192                 } 
00193             pad.tone(659.0,0.25); //3.
00194             wait(0.25);
00195             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00196                   return 0;
00197                 } 
00198             pad.tone(523.0,0.25); //1.
00199             wait(0.25);
00200             pad.tone(784.0,0.75); //5-
00201             wait(0.75);
00202             pad.tone(659.0,0.25); //3.
00203             wait(0.25);
00204             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00205                   return 0;
00206                 } 
00207             pad.tone(587.0,0.5); //2
00208             wait(0.5);
00209             pad.tone(784.0,0.5); //5
00210             wait(0.5);
00211             pad.tone(587.0,0.5); //2
00212             wait(0.5);
00213             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00214                   return 0;
00215                 } 
00216             pad.tone(523.0,0.25); //1.
00217             wait(0.25);
00218             pad.tone(440.0,0.25); //l6.
00219             wait(0.25);
00220             pad.tone(659.0,0.75); //3-
00221             wait(0.75);
00222             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00223                   return 0;
00224                 } 
00225             pad.tone(523.0,0.25); //1.
00226             wait(0.25);
00227             pad.tone(494.0,0.5); //l7
00228             wait(0.5);
00229             pad.tone(0.0,0.5); //gap
00230             wait(0.5);
00231             pad.tone(494.0,0.5); //l7
00232             wait(0.5);
00233             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00234                   return 0;
00235                 }  
00236             pad.tone(440.0,0.5); //l6
00237             wait(0.5);
00238             pad.tone(494.0,0.5); //l7
00239             wait(0.5);
00240             pad.tone(523.0,0.25); //1.
00241             wait(0.25);
00242             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00243                   return 0;
00244                 }  
00245             pad.tone(587.0,0.25); //2.
00246             wait(0.25);
00247             pad.tone(392.0,0.5); //l5
00248             wait(0.5);
00249             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00250                   return 0;
00251                 } 
00252             pad.tone(523.0,0.5); //1
00253             wait(0.5);
00254             pad.tone(587.0,0.25); //2.
00255             wait(0.25);
00256             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00257                   return 0;
00258                 }
00259             pad.tone(659.0,0.25); //3.
00260             wait(0.25);
00261             pad.tone(698.0,0.5); //4
00262             wait(0.5);
00263             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00264                   return 0;
00265                 }
00266             pad.tone(698.0,0.25); //4.
00267             wait(0.25);
00268             pad.tone(659.0,0.25); //3.
00269             wait(0.25);
00270             pad.tone(587.0,0.25); //2.
00271             wait(0.25);
00272             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00273                   return 0;
00274                 }
00275             pad.tone(523.0,0.25); //1.
00276             wait(0.25);
00277             pad.tone(587.0,0.5); //2
00278             wait(0.5);
00279             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00280                   return 0;
00281                 }
00282             pad.tone(0.0,5); //gap
00283             wait(0.5);
00284             pad.tone(523.0,0.25); //1.
00285             wait(0.25);
00286             pad.tone(587.0,0.25); //2.
00287             wait(0.25);
00288             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00289                   return 0;
00290                 }
00291             pad.tone(659.0,0.25); //3.
00292             wait(0.25);
00293             pad.tone(523.0,0.25); //1.
00294             wait(0.25);
00295             pad.tone(784.0,0.75); //5-
00296             wait(0.75);
00297             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00298                   return 0;
00299                 }
00300             pad.tone(659.0,0.25); //3.
00301             wait(0.25);
00302             pad.tone(587.0,0.5); //2
00303             wait(0.5);
00304             pad.tone(784.0,0.5); //5
00305             wait(0.5);
00306             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00307                   return 0;
00308                 }
00309             pad.tone(587.0,0.5); //2
00310             wait(0.5);
00311             pad.tone(523.0,0.25); //1.
00312             wait(0.25);
00313             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00314                   return 0;
00315                 }
00316             pad.tone(440.0,0.25); //l6.
00317             wait(0.25);
00318             pad.tone(440.0,0.5); //l6
00319             wait(0.5);
00320             pad.tone(494.0,0.25); //l7.
00321             wait(0.25);
00322             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00323                   return 0;
00324                 }  
00325             pad.tone(523.0,0.25); //1.
00326             wait(0.25);
00327             pad.tone(392.0,0.5); //l5
00328             wait(0.5);
00329             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00330                   return 0;
00331                 }  
00332             pad.tone(0.0,0.5); //gap
00333             wait(0.5);
00334             pad.tone(0.0,0.25); //gap.
00335             wait(0.25);
00336             pad.tone(494.0,0.5); //l7
00337             wait(0.5);
00338             pad.tone(523.0,0.25); //1.
00339             wait(0.25);
00340             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00341                   return 0;
00342                 }  
00343             pad.tone(587.0,0.25); //2.
00344             wait(0.25);
00345             pad.tone(392.0,0.5); //l5
00346             wait(0.5);
00347             pad.tone(523.0,0.5); //1
00348             wait(0.5);
00349             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00350                   return 0;
00351                 }  
00352             pad.tone(587.0,0.25); //2.
00353             wait(0.25);
00354             pad.tone(659.0,0.25); //3.
00355             wait(0.25);
00356             pad.tone(698.0,0.5); //4
00357             wait(0.5);
00358             pad.tone(698.0,0.25); //4.
00359             wait(0.25);
00360             pad.tone(659.0,0.25); //3.
00361             wait(0.25);
00362             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00363                   return 0;
00364                 }  
00365             pad.tone(587.0,0.25); //2.
00366             wait(0.25);
00367             pad.tone(523.0,0.25); //1.
00368             wait(0.25);
00369             pad.tone(523.0,0.5); //1
00370             wait(0.5);
00371             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00372                   return 0;
00373                 }  
00374             pad.tone(0.0,0.5); //gap
00375             wait(0.5);
00376             pad.tone(0.0,0.5); //gap
00377             wait(0.5);
00378             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00379                   return 0;
00380                 }  
00381             pad.tone(659.0,0.25); //3.
00382             wait(0.25);
00383             pad.tone(698.0,0.25); //4.
00384             wait(0.25);
00385             pad.tone(784.0,0.5); //5
00386             wait(0.5);
00387             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00388                   return 0;
00389                 }  
00390             pad.tone(784.0,0.5); //5
00391             wait(0.5);
00392             pad.tone(784.0,0.5); //5
00393             wait(0.5);
00394             pad.tone(784.0,0.5); //5
00395             wait(0.5);
00396             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00397                   return 0;
00398                 }  
00399             pad.tone(784.0,0.25); //5.
00400             wait(0.25);
00401             pad.tone(880.0,0.25); //6.
00402             wait(0.25);
00403             pad.tone(784.0,0.25); //5.
00404             wait(0.25);
00405             pad.tone(698.0,0.25); //4.
00406             wait(0.25);
00407             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00408                   return 0;
00409                 }  
00410             pad.tone(659.0,0.5); //3
00411             wait(0.5);
00412             pad.tone(659.0,0.5); //3
00413             wait(0.5);
00414             pad.tone(659.0,0.5); //3
00415             wait(0.5);
00416             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00417                   return 0;
00418                 }  
00419             pad.tone(659.0,0.5); //3
00420             wait(0.5);
00421             pad.tone(659.0,0.25); //3.
00422             wait(0.25);
00423             pad.tone(698.0,0.25); //4.
00424             wait(0.25);
00425             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00426                   return 0;
00427                 }  
00428             pad.tone(659.0,0.25); //3.
00429             wait(0.25);
00430             pad.tone(587.0,0.25); //2.
00431             wait(0.25);
00432             pad.tone(523.0,0.5); //1
00433             wait(0.5);
00434             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00435                   return 0;
00436                 }  
00437             pad.tone(523.0,0.5); //1
00438             wait(0.5);
00439             pad.tone(523.0,0.5); //1.
00440             wait(0.5);
00441             pad.tone(494.0,0.25); //l7.
00442             wait(0.25);
00443             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00444                   return 0;
00445                 }  
00446             pad.tone(440.0,0.5); //l6
00447             wait(0.5);
00448             pad.tone(494.0,0.5); //l7
00449             wait(0.5);
00450             pad.tone(494.0,0.25); //l7.
00451             wait(0.25);
00452             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00453                   return 0;
00454                 }  
00455             pad.tone(523.0,0.25); //1.
00456             wait(0.25);
00457             pad.tone(587.0,0.5); //2
00458             wait(0.5);
00459             pad.tone(587.0,0.25); //2.
00460             wait(0.25);
00461             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00462                   return 0;
00463                 }  
00464             pad.tone(659.0,0.25); //3.
00465             wait(0.25);
00466             pad.tone(587.0,0.25); //2.
00467             wait(0.25);
00468             pad.tone(659.0,0.25); //3.
00469             wait(0.25);
00470             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00471                   return 0;
00472                 }  
00473             pad.tone(587.0,0.5); //2
00474             wait(0.5);
00475             pad.tone(0.0,0.5); //gap
00476             wait(0.5);
00477             pad.tone(659.0,0.25); //3.
00478             wait(0.25);
00479             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00480                   return 0;
00481                 }  
00482             pad.tone(698.0,0.25); //4.
00483             wait(0.25);
00484             pad.tone(784.0,0.5); //5
00485             wait(0.5);
00486             pad.tone(784.0,0.5); //5
00487             wait(0.5);
00488             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00489                   return 0;
00490                 }  
00491             pad.tone(784.0,0.5); //5
00492             wait(0.5);
00493             pad.tone(784.0,0.5); //5
00494             wait(0.5);
00495             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00496                   return 0;
00497                 }  
00498             pad.tone(784.0,0.25); //5.
00499             wait(0.25);
00500             pad.tone(880.0,0.25); //6.
00501             wait(0.25);
00502             pad.tone(784.0,0.25); //5.
00503             wait(0.25);
00504             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00505                   return 0;
00506                 }  
00507             pad.tone(698.0,0.25); //4.
00508             wait(0.25);
00509             pad.tone(659.0,0.5); //3
00510             wait(0.5);
00511             pad.tone(659.0,0.5); //3
00512             wait(0.5);
00513             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00514                   return 0;
00515                 }  
00516             pad.tone(659.0,0.5); //3
00517             wait(0.5);
00518             pad.tone(659.0,0.25); //3.
00519             wait(0.25);
00520             pad.tone(698.0,0.25); //4.
00521             wait(0.25);
00522             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00523                   return 0;
00524                 }  
00525             pad.tone(659.0,0.25); //3.
00526             wait(0.25);
00527             pad.tone(587.0,0.25); //2.
00528             wait(0.25);
00529             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00530                   return 0;
00531                 }  
00532             pad.tone(523.0,0.25); //1.
00533             wait(0.25);
00534             
00535             pad.tone(494.0,0.25); //l7.
00536             wait(0.25);
00537             pad.tone(440.0,0.5); //l6
00538             wait(0.5);
00539             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00540                   return 0;
00541                 }  
00542             pad.tone(440.0,0.25); //l6.
00543             wait(0.25);
00544             pad.tone(494.0,0.25); //l7.
00545             wait(0.25);
00546             pad.tone(523.0,0.25); //1.
00547             wait(0.25);
00548             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00549                   return 0;
00550                 }  
00551             pad.tone(587.0,0.25); //2.
00552             wait(0.25);
00553             pad.tone(392.0,0.5); //l5
00554             wait(0.5);
00555             pad.tone(523.0,0.5); //1
00556             wait(0.5);
00557             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00558                   return 0;
00559                 }  
00560             pad.tone(587.0,0.25); //2.
00561             wait(0.25);
00562             pad.tone(659.0,0.25); //3.
00563             wait(0.25);
00564             pad.tone(587.0,0.75); //2-
00565             wait(0.75);
00566             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00567                   return 0;
00568                 }  
00569             pad.tone(587.0,0.25); //2.
00570             wait(0.25);
00571             pad.tone(587.0,0.25); //2.
00572             wait(0.25);
00573             pad.tone(523.0,0.25); //1.
00574             wait(0.25);
00575             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00576                   return 0;
00577                 }  
00578             pad.tone(523.0,0.5); //1
00579             wait(0.5);
00580             pad.tone(0.0,0.5); //gap
00581             wait(0.5);
00582             pad.tone(0.0,0.5); //gap
00583             wait(0.5);
00584             if(pad.check_event(Gamepad::BACK_PRESSED) == true){
00585                   return 0;
00586                 }  
00587 
00588         }
00589         if(Highest_score < (_length - 5)){ 
00590         Highest_score = _length - 5;
00591         }
00592         init(initx,inity,initl,live);
00593 //end of deadSnake
00594                   return 0;
00595 }
00596 
00597 void Snake::Tone_1(Gamepad &pad){
00598          pad.tone(750.0,0.1);
00599          }
00600          
00601 void Snake::check_WallCollision(N5110 &lcd, Gamepad &pad){
00602     
00603     if (snak._x[_length] == 0 || snak._x[_length] == WIDTH)//if snake head hits side walls
00604         {   
00605             game_music(pad);
00606             dead(lcd,pad);
00607         }
00608     if (snak._y[_length]== FLOOR ||snak._y[_length]== CEILING )//if snake hits top or bottom walls
00609         {   
00610             game_music(pad);
00611             dead(lcd,pad);
00612         }
00613     
00614 }//end of checkWallCollision
00615 
00616 void Snake::check_TailCollision(N5110 &lcd, Gamepad &pad){//if snake eats itself
00617     for (int i=0 ;_length<i ;i++){
00618                                           
00619                     if (snak._x[_length-1]==snak._x[i] && snak._y[_length-1]==snak._y[i])
00620                         {   
00621                             game_music(pad);
00622                             dead(lcd,pad);
00623                         }
00624                 }
00625     
00626     
00627 }//end of checkTailCollision
00628 
00629 void Snake::game_music(Gamepad &pad){
00630     
00631     pad.tone(1046.0,0.125); 
00632     wait(0.125);
00633     pad.tone(1075.0,0.125); 
00634     wait(0.125);
00635     pad.tone(1318.0,0.125); 
00636     wait(0.125);
00637     }
00638 
00639 void Snake::drawscore(N5110 &lcd){
00640         char buffer1[14];
00641         char buffer2[14];
00642         sprintf(buffer1,"%2d",live);
00643         sprintf(buffer2,"%2d",_length-5);
00644         lcd.printString(buffer1,20,0);
00645         lcd.printString(buffer2,70,0);
00646         lcd.drawSprite(6,1,7,7,(int *)LIFE);
00647         lcd.printString(":",17,0);
00648         lcd.printString("Score:",40,0);//display life and points
00649     
00650 }//end of drawScore