Peng Jingran / Mbed 2 deprecated Snake_copy

Dependencies:   mbed FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Snake.cpp Source File

Snake.cpp

00001 #include "Snake.h"
00002 
00003 #define HEIGHT 48
00004 #define CEILING 8
00005 #define FLOOR 48
00006 #define WIDTH 84
00007 
00008 
00009 snakePart snek;
00010 
00011 
00012 
00013 Snake::Snake()
00014 {
00015 
00016 }
00017 Snake::~Snake()
00018 {
00019   
00020 }
00021 
00022 
00023 void Snake::init(int x, int y, int lenght, int _live){  
00024        
00025     startx=x;
00026     starty=y;
00027     
00028     startl=lenght;
00029     live=_live;
00030     _length = lenght;    
00031     _food.init();
00032     
00033     int lowerBound=_length;
00034     for (int i=0;lowerBound>i;i++)
00035     {   
00036         snek._x[i]=x+i;
00037         snek._y[i]=y;
00038         snek._dir[i]=4;
00039     }
00040     
00041     snek._x[_length]=x+_length;
00042     snek._x[_length+1]=x+_length+1;
00043     snek._y[_length]=y;
00044     snek._dir[_length]=1;
00045     printf("xog is %d  ", snek._x[_length-1]);
00046 
00047 }
00048 
00049 void Snake::draw(N5110 &lcd){//condition of the collasion function
00050         
00051         checkWallCollision(lcd);
00052         checkTailCollision(lcd);
00053             
00054             if (live!=0){
00055                 lcd.clear();
00056                 waitCount=((float)_length/3)+5;
00057                 waitTime=(1/waitCount);
00058                 wait(waitTime);                
00059                 _food.draw(lcd);
00060                 lcd.drawRect(0,7,80,48-8,FILL_TRANSPARENT);
00061                 drawScore(lcd);
00062                 
00063                 int lowerBound=_length;
00064                 for (int i=0; lowerBound>i;i++){
00065                     
00066                     bool ifcondition(snek._x!=0);
00067                     if (ifcondition)
00068                         {
00069                             bool ifcondition(snek._y!=0);
00070                             if (ifcondition)
00071                                 {
00072                                     lcd.setPixel(snek._x[i],snek._y[i]);
00073                                 }
00074                         }
00075                 
00076                 lcd.refresh();
00077                 
00078                 }
00079        }
00080        
00081 }
00082 void Snake::update(Gamepad &pad){
00083      bool ifcondition(live!=0);
00084      if (ifcondition){
00085     
00086             d=pad.get_direction();
00087             printf("x+1 is %d", snek._x[_length+2]);
00088             printf("y+1 is %d", snek._y[_length+2]);
00089             printf("dir+1 is %d", snek._dir[_length+2]);
00090             printf("length is %d", _length);
00091     
00092             bool ifcondition(snek._dir[_length-1]==1);
00093             if (ifcondition)//check thecconditions that snake eat the food
00094                 {
00095                     snek._x[_length]++;                    
00096                 }
00097             
00098             if (snek._dir[_length-1]==2)
00099                 {
00100                     snek._y[_length]--;
00101                 
00102                 }
00103             
00104             if (snek._dir[_length-1]==3)
00105                 {
00106                     snek._x[_length]--;                    
00107                 }
00108                 
00109             if (snek._dir[_length-1]==4)
00110                 {
00111                     snek._y[_length]++;                                        
00112                 }
00113             
00114             for (int i=0 ;_length<i ;i++)
00115                 {                      
00116                     snek._x[i]=snek._x[i+1];
00117                     snek._y[i]=snek._y[i+1];
00118                                        
00119                 }
00120                 
00121             if (d==N){
00122                 
00123                         if (snek._dir[_length-1]!=4)
00124                             {
00125                                 snek._dir[_length-1] = 2;                                
00126                             }
00127 
00128             }
00129             
00130             if (d==E){
00131                 
00132                         if (snek._dir[_length-1]!=3)
00133                             {
00134                                 snek._dir[_length-1] = 1;                                
00135                             }
00136                 
00137                 }
00138                 
00139             if (d==W){
00140                 
00141                         if (snek._dir[_length-1]!=1)
00142                             {
00143                                 snek._dir[_length-1] = 3;
00144                             }
00145                 
00146                 
00147                 }
00148                 
00149             if (d==S){
00150                 
00151                         if (snek._dir[_length-1]!=2)
00152                             {
00153                                 snek._dir[_length-1] = 4;
00154                             }
00155                 
00156                 
00157                 }
00158         
00159             printf("updated ");
00160             
00161         }
00162 }
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 
00193 
00194 void Snake::deadSnake(N5110 &lcd){//check the condition that the life reduces
00195         
00196        
00197         live--; 
00198         while (live==!1){
00199             lcd.clear();
00200             lcd.init();
00201             lcd.printString("Game Over",0,1);
00202             lcd.printString("Press Reset",0,2);
00203             lcd.printString("To restart",0,3);
00204             lcd.refresh(); 
00205           
00206         }
00207         init(startx,starty,startl,live);
00208         
00209 }
00210         
00211 void Snake::checkWallCollision(N5110 &lcd){
00212     
00213     bool ifcondition=(snek._x[_length]==WIDTH||snek._x[_length]==0);
00214     if (ifcondition)
00215         {     
00216             deadSnake(lcd);
00217         }
00218     
00219     if (snek._y[_length]==CEILING||snek._y[_length]==FLOOR)//if snake hits top or bottom walls
00220         {     
00221             deadSnake(lcd);
00222         }
00223     
00224 }
00225 
00226 void Snake::checkTailCollision(N5110 &lcd){
00227     int upperBound=_length;
00228     for (int i=0 ;upperBound<i ;i++){//check the snake crushes its nail
00229                     
00230                     bool ifcondition=(snek._x[_length-1]==snek._x[i] && snek._y[_length-1]==snek._y[i]);                      
00231                     if (ifcondition)
00232                         {
00233                             deadSnake(lcd);
00234                         }
00235                 }
00236     
00237     
00238 }
00239 
00240 void Snake::drawScore(N5110 &lcd){
00241         char buffer1[14];
00242         char buffer2[14];
00243         sprintf(buffer1,"%2d",live);
00244         sprintf(buffer2,"%2d",_length-4);
00245         lcd.printString(buffer1,20,0);
00246         lcd.printString(buffer2,60,0);
00247         lcd.printString("Life:",0,0);
00248         lcd.printString("Score:",42,0);
00249     
00250 }