Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Diff: GameObjects/Snake/Snake.cpp
- Revision:
- 70:7caab8069b9b
- Parent:
- 68:b9cfd27987ac
- Child:
- 71:4bd2b27693f3
--- a/GameObjects/Snake/Snake.cpp Sat May 04 17:18:44 2019 +0000 +++ b/GameObjects/Snake/Snake.cpp Sat May 04 20:48:10 2019 +0000 @@ -29,30 +29,16 @@ { _length = length; if(m == 0) { + Vector2D p = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen. Snake::set_pos(p); - for(int i=0; i<=13; i++) { - b[i] = 1; - } - b[14] = 1; m = m+1; } //printf("SPRITE %d %d \n", _x[0], _y[0]); - for(int a=1; a<=10; a++) { - if(_length == a) { - for(int i=0; i<=a-1; i++) { - lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. - } - } - } - if(_length >= 10) { - for(int a=1; a<=10; a++) { - for(int i=0; i<=a-1; i++) { - lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. - } - } + for(int i=0; i<=_length-1; i++) { + lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. } } @@ -68,176 +54,27 @@ } } -Vector2D Snake::get_pos() +Vector2D Snake::get_pos(int snakeIndex) { - for(int i=1; i<=10; i++) { + Vector2D snakepos; //Stores the snake position. + for(int i = (snakeIndex + 1); i<=10; i++) { if(_length == i) { - Vector2D snakepos = {_x[i-1],_y[i-1]}; //Obtains the snake position. + snakepos.x = _x[i - (snakeIndex + 1)]; + snakepos.y = _y[i - (snakeIndex + 1)]; return snakepos; } } if(_length >= 10) { - Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position. + snakepos.x = _x[9 - snakeIndex]; + snakepos.y = _y[9 - snakeIndex]; return snakepos; } - Vector2D snakepos = {-20,-20}; //Obtains the snake position. + snakepos.x = NULL; + snakepos.y = NULL; return snakepos; //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); } -Vector2D Snake::get_pos_before1() //this sends data of following sprites. -{ - for(int i=2; i<=10; i++) { - if(_length == i) { - Vector2D snakepos_b1 = {_x[i-2],_y[i-2]}; //Obtains the snake position. - return snakepos_b1; - } - } - if(_length >= 10) { - Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position. - return snakepos_b1; - } - Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position. - return snakepos_b1; - //printf("snakepos_b1 from player = %f %f \n", snakepos_b1.x, snakepos_b1.y); -} - -Vector2D Snake::get_pos_before2() //this sends data of following sprites. -{ - for(int i=3; i<=10; i++) { - if(_length == i) { - Vector2D snakepos_b2 = {_x[i-3],_y[i-3]}; //Obtains the snake position. - return snakepos_b2; - } - } - if(_length >= 10) { - Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position. - return snakepos_b2; - } - Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position. - return snakepos_b2; - //printf("snakepos_b2 from player = %f %f \n", snakepos_b2.x, snakepos_b2.y); -} - -Vector2D Snake::get_pos_before3() //this sends data of following sprites. -{ - for(int i=4; i<=10; i++) { - if(_length == i) { - Vector2D snakepos_b3 = {_x[i-4],_y[i-4]}; //Obtains the snake position. - return snakepos_b3; - } - } - if(_length >= 10) { - Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position. - return snakepos_b3; - } - Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position. - return snakepos_b3; - //printf("snakepos_b3 from player = %f %f \n", snakepos_b3.x, snakepos_b3.y); -} - -Vector2D Snake::get_pos_before4() //this sends data of following sprites. -{ - for(int i=5; i<=10; i++) { - if(_length == i) { - Vector2D snakepos_b4 = {_x[i-5],_y[i-5]}; //Obtains the snake position. - return snakepos_b4; - } - } - if(_length >= 10) { - Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position. - return snakepos_b4; - } - Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position. - return snakepos_b4; - //printf("snakepos_b4 from player = %f %f \n", snakepos_b4.x, snakepos_b4.y); -} - -Vector2D Snake::get_pos_before5() //this sends data of following sprites. -{ - for(int i=6; i<=10; i++) { - if(_length == i) { - Vector2D snakepos_b5 = {_x[i-6],_y[i-6]}; //Obtains the snake position. - return snakepos_b5; - } - } - if(_length >= 10) { - Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position. - return snakepos_b5; - } - Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position. - return snakepos_b5; - //printf("snakepos_b5 from player = %f %f \n", snakepos_b5.x, snakepos_b5.y); -} - -Vector2D Snake::get_pos_before6() //this sends data of following sprites. -{ - for(int i=7; i<=10; i++) { - if(_length == i) { - Vector2D snakepos_b6 = {_x[i-7],_y[i-7]}; //Obtains the snake position. - return snakepos_b6; - } - } - if(_length >= 10) { - Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position. - return snakepos_b6; - } - Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position. - return snakepos_b6; - //printf("snakepos_b6 from player = %f %f \n", snakepos_b6.x, snakepos_b6.y); -} - -Vector2D Snake::get_pos_before7() //this sends data of following sprites. -{ - for(int i=8; i<=10; i++) { - if(_length == i) { - Vector2D snakepos_b7 = {_x[i-8],_y[i-8]}; //Obtains the snake position. - return snakepos_b7; - } - } - if(_length >= 10) { - Vector2D snakepos_b7 = {_x[2],_y[2]}; //Obtains the snake position. - return snakepos_b7; - } - Vector2D snakepos_b7 = {-20,-20}; //Obtains the snake position. - return snakepos_b7; - //printf("snakepos_b7 from player = %f %f \n", snakepos_b7.x, snakepos_b7.y); -} - -Vector2D Snake::get_pos_before8() //this sends data of following sprites. -{ - for(int i=9; i<=10; i++) { - if(_length == i) { - Vector2D snakepos_b8 = {_x[i-9],_y[i-9]}; //Obtains the snake position. - return snakepos_b8; - } - } - if(_length >= 10) { - Vector2D snakepos_b8 = {_x[1],_y[1]}; //Obtains the snake position. - return snakepos_b8; - } - Vector2D snakepos_b8 = {-20,-20}; //Obtains the snake position. - return snakepos_b8; - //printf("snakepos_b8 from player = %f %f \n", snakepos_b8.x, snakepos_b8.y); -} - -Vector2D Snake::get_pos_before9() //this sends data of following sprites. -{ - for(int i=10; i<=10; i++) { - if(_length == i) { - Vector2D snakepos_b9 = {_x[i-10],_y[i-10]}; //Obtains the snake position. - return snakepos_b9; - } - } - if(_length >= 10) { - Vector2D snakepos_b9 = {_x[0],_y[0]}; //Obtains the snake position. - return snakepos_b9; - } - Vector2D snakepos_b9 = {-20,-20}; //Obtains the snake position. - return snakepos_b9; - //printf("snakepos_b9 from player = %f %f \n", snakepos_b9.x, snakepos_b9.y); -} - void Snake::update(Direction d, int* b) { if(_length >= 10) { @@ -291,5 +128,4 @@ _x[i] = 81; } } -} - +} \ No newline at end of file