![](/media/cache/profiles/LinkedIn_0NghoZJ.jpeg.50x50_q85.jpg)
Snake vs Block Game to be run upon K64F.
Snake/Snake.cpp
- Committer:
- AhmedPlaymaker
- Date:
- 2019-04-10
- Revision:
- 21:e41126528cc6
- Parent:
- 20:1e6338403427
- Child:
- 22:ee698f66146f
File content as of revision 21:e41126528cc6:
#include "Snake.h" Snake::Snake() { } Snake::~Snake() { } //The Snake Sprite. int snake_sprite[3][3] = { {0,1,0}, {1,0,1}, {0,1,0}, }; int m = 0; //Variable used to allow a starting location for the player. void Snake::init() { _speed = 1;// change this according to the options selected } int Snake::draw(Gamepad &pad, N5110 &lcd, int length, int level) { if(m == 0){ _x[0] = WIDTH/2; //Spawns player sprite near the middle of the screen. _x[1] = _x[0]; _x[2] = _x[1]; _x[3] = _x[2]; _x[4] = _x[3]; _x[5] = _x[4]; _x[6] = _x[5]; _x[7] = _x[6]; _x[8] = _x[7]; _x[9] = _x[8]; _x[10] = _x[9]; _x[11] = _x[10]; _x[12] = _x[11]; _x[13] = _x[12]; _x[14] = _x[13]; _y[0] = HEIGHT - 3; _y[1] = _y[0] - 3; _y[2] = _y[0] - 6; _y[3] = _y[0] - 9; _y[4] = _y[0] - 12; _y[5] = _y[0] - 15; _y[6] = _y[0] - 18; _y[7] = _y[0] - 21; _y[8] = _y[0] - 24; _y[9] = _y[0] - 27; _y[10] = _y[0] - 30; _y[11] = _y[0] - 33; _y[12] = _y[0] - 36; _y[13] = _y[0] - 39; _y[14] = _y[0] - 42; b[0] = 1; b[1] = 1; b[2] = 1; b[3] = 1; b[4] = 1; b[5] = 1; b[6] = 1; b[7] = 1; b[8] = 1; b[9] = 1; b[10] = 1; b[11] = 1; b[12] = 1; b[13] = 1; b[14] = 1; m = m+1; } //printf("SPRITE %d %d \n", _x[0], _y[0]); if(length == 0) { while ((pad.check_event(Gamepad::BACK_PRESSED) == false)) { lcd.clear(); lcd.printString("Game",33,1); lcd.printString("Over",33,3); lcd.printString("Press Back",14,5); //Function used to promt the user to Restart. lcd.refresh(); wait(0.1); } NVIC_SystemReset(); //Software Reset. //change this condition to loosing screen. } else if(length == 1) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. } else if(length == 2) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); } else if(length == 3) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); } else if(length == 4) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); } else if(length == 5) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); } else if(length == 6) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); } else if(length == 7) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite); } else if(length == 8) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite); lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite); } else if(length == 9) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite); lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite); lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite); } else if(length == 10) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite); lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite); lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite); lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite); } else if(length == 11) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite); lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite); lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite); lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite); lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite); } else if(length == 12) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite); lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite); lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite); lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite); lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite); lcd.drawSprite(_x[11],_y[11],3,3,(int *)snake_sprite); } else if(length == 13) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite); lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite); lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite); lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite); lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite); lcd.drawSprite(_x[11],_y[11],3,3,(int *)snake_sprite); lcd.drawSprite(_x[12],_y[12],3,3,(int *)snake_sprite); } else if(length == 14) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite); lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite); lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite); lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite); lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite); lcd.drawSprite(_x[11],_y[11],3,3,(int *)snake_sprite); lcd.drawSprite(_x[12],_y[12],3,3,(int *)snake_sprite); lcd.drawSprite(_x[13],_y[13],3,3,(int *)snake_sprite); } else if(length == 15) { lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite); lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite); lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite); lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite); lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite); lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite); lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite); lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite); lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite); lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite); lcd.drawSprite(_x[11],_y[11],3,3,(int *)snake_sprite); lcd.drawSprite(_x[12],_y[12],3,3,(int *)snake_sprite); lcd.drawSprite(_x[13],_y[13],3,3,(int *)snake_sprite); lcd.drawSprite(_x[14],_y[14],3,3,(int *)snake_sprite); } else if(length > 15) { lcd.clear(); lcd.printString("Level Complete",0,1); lcd.refresh(); wait(2); lcd.clear(); lcd.printString("Press Start",10,1); lcd.printString("to Proceed",12,3); lcd.refresh(); int sound = 1000; //helps to set max freq for random sound while ( pad.check_event(Gamepad::START_PRESSED) == false) { pad.leds_on(); pad.tone(rand()%sound,0.1); wait(0.1); pad.leds_off(); wait(0.1); } lcd.clear(); char bufferlevel[14]; sprintf(bufferlevel,"%d",level); lcd.printString("Level:",25,1); lcd.printString(bufferlevel,40,3); lcd.refresh(); wait(2); length = 3; } return length; } Vector2D Snake::get_pos(int length) { if(length == 1) { Vector2D snakepos = {_x[0],_y[0]}; //Obtains the snake position. return snakepos; } else if(length == 2) { Vector2D snakepos = {_x[1],_y[1]}; //Obtains the snake position. return snakepos; } else if(length==3) { Vector2D snakepos = {_x[2],_y[2]}; //Obtains the snake position. return snakepos; } else if(length==4) { Vector2D snakepos = {_x[3],_y[3]}; //Obtains the snake position. return snakepos; } else if(length==5) { Vector2D snakepos = {_x[4],_y[4]}; //Obtains the snake position. return snakepos; } else if(length==6) { Vector2D snakepos = {_x[5],_y[5]}; //Obtains the snake position. return snakepos; } else if(length==7) { Vector2D snakepos = {_x[6],_y[6]}; //Obtains the snake position. return snakepos; } else if(length==8) { Vector2D snakepos = {_x[7],_y[7]}; //Obtains the snake position. return snakepos; } else if(length==9) { Vector2D snakepos = {_x[8],_y[8]}; //Obtains the snake position. return snakepos; } else if(length==10) { Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position. return snakepos; } else if(length==11) { Vector2D snakepos = {_x[10],_y[10]}; //Obtains the snake position. return snakepos; } else if(length==12) { Vector2D snakepos = {_x[11],_y[11]}; //Obtains the snake position. return snakepos; } else if(length==13) { Vector2D snakepos = {_x[12],_y[12]}; //Obtains the snake position. return snakepos; } else if(length == 14) { Vector2D snakepos = {_x[13],_y[13]}; //Obtains the snake position. return snakepos; } else if(length==15) { Vector2D snakepos = {_x[14],_y[14]}; //Obtains the snake position. return snakepos; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos = {-20,-20}; //Obtains the snake position. return snakepos; } } Vector2D Snake::get_pos_before1(int length) //this sends data of following sprites. { if(length == 2) { Vector2D snakepos_b1 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b1; } else if(length==3) { Vector2D snakepos_b1 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b1; } else if(length==4) { Vector2D snakepos_b1 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b1; } else if(length==5) { Vector2D snakepos_b1 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b1; } else if(length==6) { Vector2D snakepos_b1 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b1; } else if(length==7) { Vector2D snakepos_b1 = {_x[5],_y[5]}; //Obtains the snake position. return snakepos_b1; } else if(length==8) { Vector2D snakepos_b1 = {_x[6],_y[6]}; //Obtains the snake position. return snakepos_b1; } else if(length==9) { Vector2D snakepos_b1 = {_x[7],_y[7]}; //Obtains the snake position. return snakepos_b1; } else if(length==10) { Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position. return snakepos_b1; } else if(length==11) { Vector2D snakepos_b1 = {_x[9],_y[9]}; //Obtains the snake position. return snakepos_b1; } else if(length==12) { Vector2D snakepos_b1 = {_x[10],_y[10]}; //Obtains the snake position. return snakepos_b1; } else if(length==13) { Vector2D snakepos_b1 = {_x[11],_y[11]}; //Obtains the snake position. return snakepos_b1; } else if(length == 14) { Vector2D snakepos_b1 = {_x[12],_y[12]}; //Obtains the snake position. return snakepos_b1; } else if(length==15) { Vector2D snakepos_b1 = {_x[13],_y[13]}; //Obtains the snake position. return snakepos_b1; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position. return snakepos_b1; } } Vector2D Snake::get_pos_before2(int length) //this sends data of following sprites. { if(length == 3) { Vector2D snakepos_b2 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b2; } else if(length==4) { Vector2D snakepos_b2 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b2; } else if(length==5) { Vector2D snakepos_b2 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b2; } else if(length==6) { Vector2D snakepos_b2 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b2; } else if(length==7) { Vector2D snakepos_b2 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b2; } else if(length==8) { Vector2D snakepos_b2 = {_x[5],_y[5]}; //Obtains the snake position. return snakepos_b2; } else if(length==9) { Vector2D snakepos_b2 = {_x[6],_y[6]}; //Obtains the snake position. return snakepos_b2; } else if(length==10) { Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position. return snakepos_b2; } else if(length==11) { Vector2D snakepos_b2 = {_x[8],_y[8]}; //Obtains the snake position. return snakepos_b2; } else if(length==12) { Vector2D snakepos_b2 = {_x[9],_y[9]}; //Obtains the snake position. return snakepos_b2; } else if(length==13) { Vector2D snakepos_b2 = {_x[10],_y[10]}; //Obtains the snake position. return snakepos_b2; } else if(length==14) { Vector2D snakepos_b2 = {_x[11],_y[11]}; //Obtains the snake position. return snakepos_b2; } else if(length == 15) { Vector2D snakepos_b2 = {_x[12],_y[12]}; //Obtains the snake position. return snakepos_b2; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position. return snakepos_b2; } } Vector2D Snake::get_pos_before3(int length) //this sends data of following sprites. { if(length == 4) { Vector2D snakepos_b3 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b3; } else if(length==5) { Vector2D snakepos_b3 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b3; } else if(length==6) { Vector2D snakepos_b3 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b3; } else if(length==7) { Vector2D snakepos_b3 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b3; } else if(length==8) { Vector2D snakepos_b3 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b3; } else if(length==9) { Vector2D snakepos_b3 = {_x[5],_y[5]}; //Obtains the snake position. return snakepos_b3; } else if(length==10) { Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position. return snakepos_b3; } else if(length==11) { Vector2D snakepos_b3 = {_x[7],_y[7]}; //Obtains the snake position. return snakepos_b3; } else if(length==12) { Vector2D snakepos_b3 = {_x[8],_y[8]}; //Obtains the snake position. return snakepos_b3; } else if(length==13) { Vector2D snakepos_b3 = {_x[9],_y[9]}; //Obtains the snake position. return snakepos_b3; } else if(length==14) { Vector2D snakepos_b3 = {_x[10],_y[10]}; //Obtains the snake position. return snakepos_b3; } else if(length==15) { Vector2D snakepos_b3 = {_x[11],_y[11]}; //Obtains the snake position. return snakepos_b3; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position. return snakepos_b3; } } Vector2D Snake::get_pos_before4(int length) //this sends data of following sprites. { if(length == 5) { Vector2D snakepos_b4 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b4; } else if(length==6) { Vector2D snakepos_b4 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b4; } else if(length==7) { Vector2D snakepos_b4 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b4; } else if(length==8) { Vector2D snakepos_b4 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b4; } else if(length==9) { Vector2D snakepos_b4 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b4; } else if(length==10) { Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position. return snakepos_b4; } else if(length==11) { Vector2D snakepos_b4 = {_x[6],_y[6]}; //Obtains the snake position. return snakepos_b4; } else if(length==12) { Vector2D snakepos_b4 = {_x[7],_y[7]}; //Obtains the snake position. return snakepos_b4; } else if(length==13) { Vector2D snakepos_b4 = {_x[8],_y[8]}; //Obtains the snake position. return snakepos_b4; } else if(length==14) { Vector2D snakepos_b4 = {_x[9],_y[9]}; //Obtains the snake position. return snakepos_b4; } else if(length==15) { Vector2D snakepos_b4 = {_x[10],_y[10]}; //Obtains the snake position. return snakepos_b4; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position. return snakepos_b4; } } Vector2D Snake::get_pos_before5(int length) //this sends data of following sprites. { if(length == 6) { Vector2D snakepos_b5 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b5; } else if(length==7) { Vector2D snakepos_b5 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b5; } else if(length==8) { Vector2D snakepos_b5 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b5; } else if(length==9) { Vector2D snakepos_b5 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b5; } else if(length==10) { Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b5; } else if(length==11) { Vector2D snakepos_b5 = {_x[5],_y[5]}; //Obtains the snake position. return snakepos_b5; } else if(length==12) { Vector2D snakepos_b5 = {_x[6],_y[6]}; //Obtains the snake position. return snakepos_b5; } else if(length==13) { Vector2D snakepos_b5 = {_x[7],_y[7]}; //Obtains the snake position. return snakepos_b5; } else if(length==14) { Vector2D snakepos_b5 = {_x[8],_y[8]}; //Obtains the snake position. return snakepos_b5; } else if(length==15) { Vector2D snakepos_b5 = {_x[9],_y[9]}; //Obtains the snake position. return snakepos_b5; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position. return snakepos_b5; } } Vector2D Snake::get_pos_before6(int length) //this sends data of following sprites. { if(length == 7) { Vector2D snakepos_b6 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b6; } else if(length==8) { Vector2D snakepos_b6 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b6; } else if(length==9) { Vector2D snakepos_b6 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b6; } else if(length==10) { Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b6; } else if(length==11) { Vector2D snakepos_b6 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b6; } else if(length==12) { Vector2D snakepos_b6 = {_x[5],_y[5]}; //Obtains the snake position. return snakepos_b6; } else if(length==13) { Vector2D snakepos_b6 = {_x[6],_y[6]}; //Obtains the snake position. return snakepos_b6; } else if(length==14) { Vector2D snakepos_b6 = {_x[7],_y[7]}; //Obtains the snake position. return snakepos_b6; } else if(length==15) { Vector2D snakepos_b6 = {_x[8],_y[8]}; //Obtains the snake position. return snakepos_b6; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position. return snakepos_b6; } } Vector2D Snake::get_pos_before7(int length) //this sends data of following sprites. { if(length == 8) { Vector2D snakepos_b7 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b7; } else if(length==9) { Vector2D snakepos_b7 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b7; } else if(length==10) { Vector2D snakepos_b7 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b7; } else if(length==11) { Vector2D snakepos_b7 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b7; } else if(length==12) { Vector2D snakepos_b7 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b7; } else if(length==13) { Vector2D snakepos_b7 = {_x[5],_y[5]}; //Obtains the snake position. return snakepos_b7; } else if(length==14) { Vector2D snakepos_b7 = {_x[6],_y[6]}; //Obtains the snake position. return snakepos_b7; } else if(length==15) { Vector2D snakepos_b7 = {_x[7],_y[7]}; //Obtains the snake position. return snakepos_b7; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b7 = {-20,-20}; //Obtains the snake position. return snakepos_b7; } } Vector2D Snake::get_pos_before8(int length) //this sends data of following sprites. { if(length == 9) { Vector2D snakepos_b8 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b8; } else if(length==10) { Vector2D snakepos_b8 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b8; } else if(length==11) { Vector2D snakepos_b8 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b8; } else if(length==12) { Vector2D snakepos_b8 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b8; } else if(length==13) { Vector2D snakepos_b8 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b8; } else if(length==14) { Vector2D snakepos_b8 = {_x[5],_y[5]}; //Obtains the snake position. return snakepos_b8; } else if(length==15) { Vector2D snakepos_b8 = {_x[6],_y[6]}; //Obtains the snake position. return snakepos_b8; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b8 = {-20,-20}; //Obtains the snake position. return snakepos_b8; } } Vector2D Snake::get_pos_before9(int length) //this sends data of following sprites. { if(length == 10) { Vector2D snakepos_b9 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b9; } else if(length==11) { Vector2D snakepos_b9 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b9; } else if(length==12) { Vector2D snakepos_b9 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b9; } else if(length==13) { Vector2D snakepos_b9 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b9; } else if(length==14) { Vector2D snakepos_b9 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b9; } else if(length==15) { Vector2D snakepos_b9 = {_x[5],_y[5]}; //Obtains the snake position. return snakepos_b9; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b9 = {-20,-20}; //Obtains the snake position. return snakepos_b9; } } Vector2D Snake::get_pos_before10(int length) //this sends data of following sprites. { if(length == 11) { Vector2D snakepos_b10 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b10; } else if(length==12) { Vector2D snakepos_b10 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b10; } else if(length==13) { Vector2D snakepos_b10 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b10; } else if(length==14) { Vector2D snakepos_b10 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b10; } else if(length==15) { Vector2D snakepos_b10 = {_x[4],_y[4]}; //Obtains the snake position. return snakepos_b10; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b10 = {-20,-20}; //Obtains the snake position. return snakepos_b10; } } Vector2D Snake::get_pos_before11(int length) //this sends data of following sprites. { if(length == 12) { Vector2D snakepos_b11 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b11; } else if(length==13) { Vector2D snakepos_b11 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b11; } else if(length==14) { Vector2D snakepos_b11 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b11; } else if(length==15) { Vector2D snakepos_b11 = {_x[3],_y[3]}; //Obtains the snake position. return snakepos_b11; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b11 = {-20,-20}; //Obtains the snake position. return snakepos_b11; } } Vector2D Snake::get_pos_before12(int length) //this sends data of following sprites. { if(length == 13) { Vector2D snakepos_b12 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b12; } else if(length==14) { Vector2D snakepos_b12 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b12; } else if(length==15) { Vector2D snakepos_b12 = {_x[2],_y[2]}; //Obtains the snake position. return snakepos_b12; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b12 = {-20,-20}; //Obtains the snake position. return snakepos_b12; } } Vector2D Snake::get_pos_before13(int length) //this sends data of following sprites. { if(length == 14) { Vector2D snakepos_b13 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b13; } else if(length==15) { Vector2D snakepos_b13 = {_x[1],_y[1]}; //Obtains the snake position. return snakepos_b13; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); else { Vector2D snakepos_b13 = {-20,-20}; //Obtains the snake position. return snakepos_b13; } } Vector2D Snake::get_pos_before14(int length) //this sends data of following sprites. { if(length==15) { Vector2D snakepos_b14 = {_x[0],_y[0]}; //Obtains the snake position. return snakepos_b14; } else { Vector2D snakepos_b14 = {-20,-20}; //Obtains the snake position. return snakepos_b14; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); } void Snake::update(Direction d,float mag,int length, int speed, int* b0_to_b14) { _speed = speed; //Speed changes depending on how much you push the joystick.(As Of Now) b[0] = b0_to_b14[0]; b[1] = b0_to_b14[1]; b[2] = b0_to_b14[2]; b[3] = b0_to_b14[3]; b[4] = b0_to_b14[4]; b[5] = b0_to_b14[5]; b[6] = b0_to_b14[6]; b[7] = b0_to_b14[7]; b[8] = b0_to_b14[8]; b[9] = b0_to_b14[9]; b[10] = b0_to_b14[10]; b[11] = b0_to_b14[11]; b[12] = b0_to_b14[12]; b[13] = b0_to_b14[13]; b[14] = b0_to_b14[14]; if(length > 1) { if ((_x[0] > _x[1])&&(b[1] == 1)&&(b[0] == 1)) { _x[0]-=_speed; } if ((_x[0] < _x[1])&&(b[1] == 1)&&(b[0] == 1)) { _x[0]+=_speed; } } if(length > 2) { if ((_x[1] > _x[2])&&(b[2] == 1)&&(b[1] == 1)) { _x[1]-=_speed; } if ((_x[1] < _x[2])&&(b[2] == 1)&&(b[1] == 1)) { _x[1]+=_speed; } } if(length > 3) { if ((_x[2] > _x[3])&&(b[3] == 1)&&(b[2] == 1)) { _x[2]-=_speed; } if ((_x[2] < _x[3])&&(b[3] == 1)&&(b[2] == 1)) { _x[2]+=_speed; } } if(length > 4) { if ((_x[3] > _x[4])&&(b[4] == 1)&&(b[3] == 1)) { _x[3]-=_speed; } if ((_x[3] < _x[4])&&(b[4] == 1)&&(b[3] == 1)) { _x[3]+=_speed; } } if(length > 5) { if ((_x[4] > _x[5])&&(b[5] == 1)&&(b[4] == 1)) { _x[4]-=_speed; } if ((_x[4] < _x[5])&&(b[5] == 1)&&(b[4] == 1)) { _x[4]+=_speed; } } if(length > 6) { if ((_x[5] > _x[6])&&(b[6] == 1)&&(b[5] == 1)) { _x[5]-=_speed; } if ((_x[5] < _x[6])&&(b[6] == 1)&&(b[5] == 1)) { _x[5]+=_speed; } } if(length > 7) { if ((_x[6] > _x[7])&&(b[7] == 1)&&(b[6] == 1)) { _x[6]-=_speed; } if ((_x[6] < _x[7])&&(b[7] == 1)&&(b[6] == 1)) { _x[6]+=_speed; } } if(length > 8) { if ((_x[7] > _x[8])&&(b[8] == 1)&&(b[7] == 1)) { _x[7]-=_speed; } if ((_x[7] < _x[8])&&(b[8] == 1)&&(b[7] == 1)) { _x[7]+=_speed; } } if(length > 9) { if ((_x[8] > _x[9])&&(b[9] == 1)&&(b[8] == 1)) { _x[8]-=_speed; } if ((_x[8] < _x[9])&&(b[9] == 1)&&(b[8] == 1)) { _x[8]+=_speed; } } if(length > 10) { if ((_x[9] > _x[10])&&(b[10] == 1)&&(b[9] == 1)) { _x[9]-=_speed; } if ((_x[9] < _x[10])&&(b[10] == 1)&&(b[9] == 1)) { _x[9]+=_speed; } } if(length > 11) { if ((_x[10] > _x[11])&&(b[11] == 1)&&(b[10] == 1)) { _x[10]-=_speed; } if ((_x[10] < _x[11])&&(b[11] == 1)&&(b[10] == 1)) { _x[10]+=_speed; } } if(length > 12) { if ((_x[11] > _x[12])&&(b[12] == 1)&&(b[11] == 1)) { _x[11]-=_speed; } if ((_x[11] < _x[12])&&(b[12] == 1)&&(b[11] == 1)) { _x[11]+=_speed; } } if(length > 13) { if ((_x[12] > _x[13])&&(b[13] == 1)&&(b[12] == 1)) { _x[12]-=_speed; } if ((_x[12] < _x[13])&&(b[13] == 1)&&(b[12] == 1)) { _x[12]+=_speed; } } if(length > 14) { if ((_x[13] > _x[14])&&(b[14] == 1)&&(b[13] == 1)) { _x[13]-=_speed; } if ((_x[13] < _x[14])&&(b[14] == 1)&&(b[13] == 1)) { _x[13]+=_speed; } } if((length == 15)&&(b[14] == 1)) { if (d == E) { _x[14]+= _speed; } if (d == W) { _x[14]-= _speed; } } if((length == 14)&&(b[13] == 1)) { if (d == E) { _x[13]+= _speed; } if (d == W) { _x[13]-= _speed; } } if((length == 13)&&(b[12] == 1)) { if (d == E) { _x[12]+= _speed; } if (d == W) { _x[12]-= _speed; } } if((length == 12)&&(b[11] == 1)) { if (d == E) { _x[11]+= _speed; } if (d == W) { _x[11]-= _speed; } } if((length == 11)&&(b[10] == 1)) { if (d == E) { _x[10]+= _speed; } if (d == W) { _x[10]-= _speed; } } if((length == 10)&&(b[9] == 1)) { if (d == E) { _x[9]+= _speed; } if (d == W) { _x[9]-= _speed; } } if((length == 9)&&(b[8] == 1)) { if (d == E) { _x[8]+= _speed; } if (d == W) { _x[8]-= _speed; } } if((length == 8)&&(b[7] == 1)) { if (d == E) { _x[7]+= _speed; } if (d == W) { _x[7]-= _speed; } } if((length == 7)&&(b[6] == 1)) { if (d == E) { _x[6]+= _speed; } if (d == W) { _x[6]-= _speed; } } if((length == 6)&&(b[5] == 1)) { if (d == E) { _x[5]+= _speed; } if (d == W) { _x[5]-= _speed; } } if((length == 5)&&(b[4] == 1)) { if (d == E) { _x[4]+= _speed; } if (d == W) { _x[4]-= _speed; } } if((length == 4)&&(b[3] == 1)) { if (d == E) { _x[3]+= _speed; } if (d == W) { _x[3]-= _speed; } } if((length == 3)&&(b[2] == 1)) { if (d == E) { _x[2]+= _speed; } if (d == W) { _x[2]-= _speed; } } if((length == 2)&&(b[1] == 1)) { if (d == E) { _x[1]+= _speed; } if (d == W) { _x[1]-= _speed; } } if((length == 1)&&(b[0] == 1)) { if (d == E) { _x[0]+= _speed; } if (d == W) { _x[0]-= _speed; } } // the following makes sure that when the length is increased, the snake stays where it was when it ate food. if(length < 2) { _x[1] = _x[0]; } else if(length < 3) { _x[2] = _x[1]; } else if(length < 4) { _x[3] = _x[2]; } else if(length < 5) { _x[4] = _x[3]; } else if(length < 6) { _x[5] = _x[4]; } else if(length < 7) { _x[6] = _x[5]; } else if(length < 8) { _x[7] = _x[6]; } else if(length < 9) { _x[8] = _x[7]; } else if(length < 10) { _x[9] = _x[8]; } else if(length < 11) { _x[10] = _x[9]; } else if(length < 12) { _x[11] = _x[10]; } else if(length < 13) { _x[12] = _x[11]; } else if(length < 14) { _x[13] = _x[12]; } else if(length < 15) { _x[14] = _x[13]; } //Limits set so that the sprite does not travel off the screen. if (_y[0] <= 0) { _y[0] = 0; } if (_x[0] <= 0) { _x[0] = 0; } if (_x[1] <= 0) { _x[1] = 0; } if (_x[2] <= 0) { _x[2] = 0; } if (_x[3] <= 0) { _x[3] = 0; } if (_x[4] <= 0) { _x[4] = 0; } if (_x[5] <= 0) { _x[5] = 0; } if (_x[6] <= 0) { _x[6] = 0; } if (_x[7] <= 0) { _x[7] = 0; } if (_x[8] <= 0) { _x[8] = 0; } if (_x[9] <= 0) { _x[9] = 0; } if (_x[10] <= 0) { _x[10] = 0; } if (_x[11] <= 0) { _x[11] = 0; } if (_x[12] <= 0) { _x[12] = 0; } if (_x[13] <= 0) { _x[13] = 0; } if (_x[14] <= 0) { _x[14] = 0; } if (_x[0] > 81) { _x[0] = 81; } if (_x[1] > 81) { _x[1] = 81; } if (_x[2] > 81) { _x[2] = 81; } if (_x[3] > 81) { _x[3] = 81; } if (_x[4] > 81) { _x[4] = 81; } if (_x[5] > 81) { _x[5] = 81; } if (_x[6] > 81) { _x[6] = 81; } if (_x[7] > 81) { _x[7] = 81; } if (_x[8] > 81) { _x[8] = 81; } if (_x[9] > 81) { _x[9] = 81; } if (_x[10] > 81) { _x[10] = 81; } if (_x[11] > 81) { _x[11] = 81; } if (_x[12] > 81) { _x[12] = 81; } if (_x[13] > 81) { _x[13] = 81; } if (_x[14] > 81) { _x[14] = 81; } }