Balint Bogdan 2645 project, 200966741

Dependents:   2645Game_el15bb

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 snek;
00010 
00011 
00012 
00013 Snake::Snake()
00014 {
00015 
00016 }
00017 Snake::~Snake()
00018 {
00019   
00020 }
00021 
00022 /************************Functions************************/
00023 
00024 void Snake::init(int x, int y, int lenght, int _live){//initalizing the starting variables     
00025        
00026     startx=x;//saving these variables for later init
00027     starty=y;
00028     startl=lenght;
00029     live=_live;
00030     _length = lenght;    
00031     _food.init();//init food
00032     for (int i=0;_length>i;i++)//create start snake
00033     {   
00034         snek._x[i]=x+i;
00035         snek._y[i]=y;
00036         snek._dir[i]=1;
00037     }
00038     snek._x[_length]=x+_length;
00039     snek._x[_length+1]=x+_length+1;
00040     snek._y[_length]=y;
00041     snek._dir[_length]=1;
00042     printf("xog is %d  ", snek._x[_length-1]);
00043     //printf("initalized");
00044 }//end of init
00045 
00046 void Snake::draw(N5110 &lcd){
00047         checkWallCollision(lcd);// if head==wall game over
00048         checkTailCollision(lcd);
00049             if (live!=0){
00050                 lcd.clear();
00051                 waitCount=((float)_length/3)+5;//set wait so the game is speeding up
00052                 waitTime=(1/waitCount);
00053                 wait(waitTime);                
00054                 _food.draw(lcd);//make first food
00055                 lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);//draw arena
00056                 drawScore(lcd);
00057                 for ( int i=0; _length>i;i++){//draw snake
00058                     if (snek._x!=0)
00059                         {
00060                             if (snek._y!=0)
00061                                 {
00062                                     lcd.setPixel(snek._x[i],snek._y[i]);
00063                                 }
00064                         }
00065                 
00066                 lcd.refresh();
00067                 
00068                 //printf("drawn");
00069                 }
00070        }//live loop 
00071        
00072 }//end of draw
00073 void Snake::update(Gamepad &pad){
00074      
00075      if (live!=0){
00076     
00077             d=pad.get_direction();
00078             printf("x+1 is %d", snek._x[_length+1]);
00079             printf("y+1 is %d", snek._y[_length+1]);
00080             printf("dir+1 is %d", snek._dir[_length+1]);
00081             printf("length is %d", _length);
00082     
00083              /*if ( pad.check_event(Gamepad::A_PRESSED) == true)//testing the addPoint manually
00084             {
00085                 snek._x[_length+1]=snek._x[_length];//-1
00086                 snek._y[_length+1]=snek._y[_length];
00087                 snek._dir[_length+1]=snek._dir[_length-1];
00088                 _length=_length+1;
00089                 printf("length+1 is %d", _length);
00090                 
00091             }
00092             */
00093             //printf("x is %d  ", snek._x[_length-1]);
00094             if (snek._dir[_length-1]==1)//set direction according _dir
00095                 {
00096                     snek._x[_length]++;                    
00097                 }
00098             
00099             if (snek._dir[_length-1]==2)
00100                 {
00101                     snek._y[_length]--;
00102                 
00103                 }
00104             
00105             if (snek._dir[_length-1]==3)
00106                 {
00107                     snek._x[_length]--;                    
00108                 }
00109                 
00110             if (snek._dir[_length-1]==4)
00111                 {
00112                     snek._y[_length]++;                                        
00113                 }
00114             
00115             for (int i=0 ;_length<i ;i++)
00116                 {                      
00117                     snek._x[i]=snek._x[i+1];
00118                     snek._y[i]=snek._y[i+1];
00119                     //printf("done");                    
00120                 }
00121             //check dpad which way it is pointing
00122             //set direction accordingly, 1 is right, up is 2, 3 is left and 4 is down        
00123             if (d==N){// if stick points up, go up
00124                 
00125                         if (snek._dir[_length-1]!=4)
00126                             {
00127                                 snek._dir[_length-1] = 2;                                
00128                             }
00129 
00130             }
00131             
00132             if (d==E){// if stick points right, go right
00133                 
00134                         if (snek._dir[_length-1]!=3)
00135                             {
00136                                 snek._dir[_length-1] = 1;                                
00137                             }
00138                 
00139                 }
00140                 
00141             if (d==W){// if stick points left, go left
00142                 
00143                         if (snek._dir[_length-1]!=1)
00144                             {
00145                                 snek._dir[_length-1] = 3;
00146                             }
00147                 
00148                 
00149                 }
00150                 
00151             if (d==S){// if stick points down, go down 
00152                 
00153                         if (snek._dir[_length-1]!=2)
00154                             {
00155                                 snek._dir[_length-1] = 4;
00156                             }
00157                 
00158                 
00159                 }
00160         
00161             printf("updated ");
00162             addPoint();
00163         }//live loop
00164 }//end of update
00165 
00166 void Snake::addPoint(){
00167     
00168     posXY foodPos = _food.returnPos();
00169     if(snek._x[_length-1]==foodPos.x && snek._y[_length-1]== foodPos.y)
00170         {
00171             snek._x[_length+1]=snek._x[_length];//-1
00172             snek._y[_length+1]=snek._y[_length];
00173             snek._dir[_length+1]=snek._dir[_length-1];
00174             _length=_length+1;//if head == food, _length++
00175             _food.respawn();//spawn new food
00176         }
00177 }//end of addPoint
00178 
00179 void Snake::deadSnake(N5110 &lcd){
00180         
00181        
00182         live--; //take a life away
00183         while (live==0){//dead reset game
00184             lcd.clear();
00185             lcd.printString("Game Over",0,1);
00186             lcd.printString("Press Reset",0,2);
00187             lcd.printString("To restart",0,3);
00188             lcd.refresh(); 
00189           
00190         }
00191                 //lcd.clear();
00192         //lcd.printString("Game Over",0,1);
00193         //lcd.refresh();    
00194         
00195         init(startx,starty,startl,live);
00196         
00197 }//end of deadSnake
00198         
00199 void Snake::checkWallCollision(N5110 &lcd){
00200     
00201     if (snek._x[_length]==WIDTH||snek._x[_length]==0)//if snake head hits side walls
00202         {     
00203             deadSnake(lcd);
00204         }
00205     if (snek._y[_length]==CEILING||snek._y[_length]==FLOOR)//if snake hits top or bottom walls
00206         {     
00207             deadSnake(lcd);
00208         }
00209     
00210 }//end of checkWallCollision
00211 
00212 void Snake::checkTailCollision(N5110 &lcd){//if snake eats itself
00213     for (int i=0 ;_length<i ;i++){
00214                                           
00215                     if (snek._x[_length-1]==snek._x[i] && snek._y[_length-1]==snek._y[i])
00216                         {
00217                             deadSnake(lcd);
00218                         }
00219                 }
00220     
00221     
00222 }//end of checkTailCollision
00223 
00224 void Snake::drawScore(N5110 &lcd){
00225         char buffer1[14];
00226         char buffer2[14];
00227         sprintf(buffer1,"%2d",live);
00228         sprintf(buffer2,"%2d",_length-5);
00229         lcd.printString(buffer1,25,0);
00230         lcd.printString(buffer2,70,0);
00231         lcd.printString("Life:",0,0);
00232         lcd.printString("Pts:",40,0);//display life and points
00233     
00234 }//end of drawScore