Balint Bogdan 2645 project, 200966741
Diff: Snake.cpp
- Revision:
- 6:cc8d2088f490
- Parent:
- 5:449858a54971
- Child:
- 7:a2f426a37e60
diff -r 449858a54971 -r cc8d2088f490 Snake.cpp --- a/Snake.cpp Fri May 05 14:51:27 2017 +0000 +++ b/Snake.cpp Fri May 05 15:06:54 2017 +0000 @@ -19,24 +19,20 @@ } +/************************Functions************************/ + void Snake::init(int x, int y, int lenght, int _live){ - //initalizing the starting variables - //snek._x[0]=x; - //snek._y[0]=y; - startx=x; + //initalizing the starting variables + startx=x;//saving these variables for later init starty=y; startl=lenght; live=_live; - /*if (live==0){ - live=3; - }*/ - _length = lenght; - - _food.init(); - for (int i=0;_length>i;i++) + _length = lenght; + _food.init();//init food + for (int i=0;_length>i;i++)//create start snake { snek._x[i]=x+i; snek._y[i]=y; @@ -45,27 +41,20 @@ snek._x[_length]=x+_length; snek._y[_length]=y; - /* snek._y[0]=y; - snek._y[1]=y+1; - snek._y[2]=y+2; - snek._y[3]=y+2; - snek._y[4]=y+3; - snek._y[5]=y+3; - snek._y[6]=y+4;*/ _direction = 1;//1 is East, 2 is South, 3 is West, 4 is North printf("xog is %d ", snek._x[_length-1]); //printf("initalized"); - } +}//end of init void Snake::draw(N5110 &lcd){ - checkWallCollision(lcd); + checkWallCollision(lcd);// if head==wall game over checkTailCollision(lcd); if (live!=0){ lcd.clear(); - waitCount=((float)_length/3)+5; + waitCount=((float)_length/3)+5;//set wait so the game is speeding up waitTime=(1/waitCount); wait(waitTime); char buffer1[14]; @@ -74,10 +63,11 @@ sprintf(buffer2,"%2d",_length); lcd.printString(buffer1,25,0); lcd.printString(buffer2,70,0); - _food.draw(lcd); - lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT); lcd.printString("Life:",0,0); - lcd.printString("Pts:",40,0); + lcd.printString("Pts:",40,0);//display life and points + _food.draw(lcd);//make first food + lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);// + for ( int i=0; _length>i;i++){ if (snek._x!=0) @@ -88,28 +78,24 @@ } } - - lcd.refresh(); //printf("drawn"); } }//live loop -} +}//end of draw void Snake::update(Gamepad &pad){ if (live!=0){ d=pad.get_direction(); - startx=snek._x[0]; - printf("x+1 is %d", snek._x[_length+1]); printf("y+1 is %d", snek._y[_length+1]); printf("dir+1 is %d", snek._dir[_length+1]); printf("length is %d", _length); - /*if ( pad.check_event(Gamepad::A_PRESSED) == true)//testing the add_point manually + /*if ( pad.check_event(Gamepad::A_PRESSED) == true)//testing the addPoint manually { snek._x[_length+1]=snek._x[_length];//-1 snek._y[_length+1]=snek._y[_length]; @@ -119,9 +105,7 @@ } */ - - - //printf("x is %d ", snek._x[_length-1]); + //printf("x is %d ", snek._x[_length-1]); if (snek._dir[_length-1]==1) { snek._x[_length]++; @@ -149,15 +133,9 @@ snek._y[i]=snek._y[i+1]; //printf("done"); } - - - //check dpad which way it is pointing - //set direction accordingly, 1 is right, up is 2, 3 is left and 4 is down - //set a breakpoint at head by snakeX/Y[i] - //switch case to make sure direction is good - //if head == food, _length++, and -direction we add 1 length - // if head==wall game over - if (d==N){ + //check dpad which way it is pointing + //set direction accordingly, 1 is right, up is 2, 3 is left and 4 is down + if (d==N){// if stick points up, go up if (snek._dir[_length-1]!=4) { @@ -166,7 +144,7 @@ } - if (d==E){ + if (d==E){// if stick points right, go right if (snek._dir[_length-1]!=3) { @@ -174,7 +152,8 @@ } } - if (d==W){ + + if (d==W){// if stick points left, go left if (snek._dir[_length-1]!=1) { @@ -183,7 +162,8 @@ } - if (d==S){ + + if (d==S){// if stick points down, go down if (snek._dir[_length-1]!=2) { @@ -194,11 +174,11 @@ } printf("updated "); - add_point(); + addPoint(); }//live loop -} +}//end of update -void Snake::add_point(){ +void Snake::addPoint(){ posXY foodPos = _food.returnPos(); if(snek._x[_length-1]==foodPos.x && snek._y[_length-1]== foodPos.y) @@ -206,10 +186,10 @@ snek._x[_length+1]=snek._x[_length];//-1 snek._y[_length+1]=snek._y[_length]; snek._dir[_length+1]=snek._dir[_length-1]; - _length=_length+1; - _food.respawn(); + _length=_length+1;//if head == food, _length++ + _food.respawn();//spawn new food } -}//end of add_point +}//end of addPoint void Snake::deadSnake(N5110 &lcd){ @@ -227,7 +207,7 @@ //lcd.printString("Game Over",0,1); //lcd.refresh(); - init(25,starty,startl,live); + init(startx,starty,startl,live); }//end of deadSnake @@ -254,4 +234,4 @@ } -} \ No newline at end of file +}//end of checkTailCollision \ No newline at end of file