Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Diff: SnakevsBlock/SnakevsBlock.cpp
- Revision:
- 26:3495f7b0ede7
- Parent:
- 25:e827f1a8fadc
- Child:
- 27:3b4775a0d0bb
--- a/SnakevsBlock/SnakevsBlock.cpp Wed Apr 10 14:37:05 2019 +0000 +++ b/SnakevsBlock/SnakevsBlock.cpp Thu Apr 11 05:34:03 2019 +0000 @@ -206,217 +206,144 @@ //If statements check if the snake sprite has collided with any //of the blocks which are a maximum of 5, if so then the snake length reduces and the block number reduces //the block has to move slower and come down after every 2/3 iterations(dependent on the snake size.(think about this) - - if ( - ((snake_pos.y == b_pos.y + 10)) && - ((snake_pos.x + 1 == b_pos.x + 1) || - (snake_pos.x + 1 == b_pos.x + 2) || - (snake_pos.x + 1 == b_pos.x + 3) || - (snake_pos.x + 1 == b_pos.x + 4) || - (snake_pos.x + 1 == b_pos.x + 5) || - (snake_pos.x + 1 == b_pos.x + 6) || - (snake_pos.x + 1 == b_pos.x + 7) || - (snake_pos.x + 1 == b_pos.x + 8) || - (snake_pos.x + 1 == b_pos.x + 9) || - (snake_pos.x + 1 == b_pos.x + 10) || - (snake_pos.x + 1 == b_pos.x + 11) || - (snake_pos.x + 1 == b_pos.x + 12) || - (snake_pos.x + 1 == b_pos.x + 13) || - (snake_pos.x + 1 == b_pos.x + 14) || - (snake_pos.x + 1 == b_pos.x + 15) || - (snake_pos.x + 1 == b_pos.x + 16) || - (snake_pos.x + 1 == b_pos.x + 17) || - (snake_pos.x + 1 == b_pos.x + 18)) - ) { - - //printf("snake collision working \n"); - //audio feedback - if(blocknum > 0) { - b_pos.y = 0; + for(int i=1; i<=18; i++) { //this loop automatically detects each combination of collision + if ( + ((snake_pos.y == b_pos.y + 10)) && + ((snake_pos.x + 1 == b_pos.x + i)) + ) { + + //printf("snake collision working \n"); + //audio feedback + if(blocknum > 0) { + b_pos.y = 0; + } + srn = 0; + blocknum = b_number[srn]; + send=1; + blocknum-=1; + if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; + speed = 2; + length-=1; + pad.tone(1000.0,0.1); + wait(0.05); + } + else { + speed = 1; + } } - srn = 0; - blocknum = b_number[srn]; - send=1; - blocknum-=1; - if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; - speed = 2; - length-=1; - pad.tone(1000.0,0.1); - wait(0.05); - } - else { - speed = 1; + } + for(int i=19; i<=34; i++) { //this loop automatically detects each combination of collision + if ( + ((snake_pos.y == b_pos.y + 10)) && + ((snake_pos.x + 1 == b_pos.x + i)) + ) { + + //printf("snake collision working \n"); + //audio feedback + if(blocknum > 0) { + b_pos.y = 0; + } + srn = 1; + blocknum = b_number[srn]; + send=1; + blocknum-=1; + if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; + speed = 2; + length-=1; + pad.tone(1000.0,0.1); + wait(0.05); + } + else { + speed = 1; + } + } } - - if ( - ((snake_pos.y == b_pos.y + 10)) && - ((snake_pos.x + 1 == b_pos.x + 19) || - (snake_pos.x + 1 == b_pos.x + 20) || - (snake_pos.x + 1 == b_pos.x + 21) || - (snake_pos.x + 1 == b_pos.x + 22) || - (snake_pos.x + 1 == b_pos.x + 23) || - (snake_pos.x + 1 == b_pos.x + 24) || - (snake_pos.x + 1 == b_pos.x + 25) || - (snake_pos.x + 1 == b_pos.x + 26) || - (snake_pos.x + 1 == b_pos.x + 27) || - (snake_pos.x + 1 == b_pos.x + 28) || - (snake_pos.x + 1 == b_pos.x + 29) || - (snake_pos.x + 1 == b_pos.x + 30) || - (snake_pos.x + 1 == b_pos.x + 31) || - (snake_pos.x + 1 == b_pos.x + 32) || - (snake_pos.x + 1 == b_pos.x + 33) || - (snake_pos.x + 1 == b_pos.x + 34)) - ) { - - //printf("snake collision working \n"); - //audio feedback - if(blocknum > 0) { - b_pos.y = 0; + for(int i=35; i<=50; i++) { //this loop automatically detects each combination of collision + if ( + ((snake_pos.y == b_pos.y + 10)) && + ((snake_pos.x + 1 == b_pos.x + i)) + ) { + + //printf("snake collision working \n"); + //audio feedback + if(blocknum > 0) { + b_pos.y = 0; + } + srn = 2; + blocknum = b_number[srn]; + send=1; + blocknum-=1; + if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; + speed = 2; + length-=1; + pad.tone(1000.0,0.1); + wait(0.05); + } + else { + speed = 1; + } + } - srn = 1; - blocknum = b_number[srn]; - send=1; - blocknum-=1; - if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; - speed = 2; - length-=1; - pad.tone(1000.0,0.1); - wait(0.05); + } + for(int i=51; i<=66; i++) { //this loop automatically detects each combination of collision + if ( + ((snake_pos.y == b_pos.y + 10)) && + ((snake_pos.x + 1 == b_pos.x + i)) + ) { + + //printf("snake collision working \n"); + //audio feedback + if(blocknum > 0) { + b_pos.y = 0; + } + srn = 3; + blocknum = b_number[srn]; + send=1; + blocknum-=1; + if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; + length-=1; + pad.tone(1000.0,0.1); + wait(0.05); + } + else { + speed = 1; + } + } - else { - speed = 1; + } + for(int i=67; i<=83; i++) { //this loop automatically detects each combination of collision + if ( + ((snake_pos.y == b_pos.y + 10)) && + ((snake_pos.x + 1 == b_pos.x + i)) + ) { + + //printf("snake collision working \n"); + //audio feedback + if(blocknum > 0) { + b_pos.y = 0; + } + srn = 4; + blocknum = b_number[srn]; + send=1; + blocknum-=1; + if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; + speed = 2; + length-=1; + pad.tone(1000.0,0.1); + wait(0.05); + } + else { + speed = 1; + } } } - - if ( - ((snake_pos.y == b_pos.y + 10)) && - ((snake_pos.x + 1 == b_pos.x + 35) || - (snake_pos.x + 1 == b_pos.x + 36) || - (snake_pos.x + 1 == b_pos.x + 37) || - (snake_pos.x + 1 == b_pos.x + 38) || - (snake_pos.x + 1 == b_pos.x + 39) || - (snake_pos.x + 1 == b_pos.x + 40) || - (snake_pos.x + 1 == b_pos.x + 41) || - (snake_pos.x + 1 == b_pos.x + 42) || - (snake_pos.x + 1 == b_pos.x + 43) || - (snake_pos.x + 1 == b_pos.x + 44) || - (snake_pos.x + 1 == b_pos.x + 45) || - (snake_pos.x + 1 == b_pos.x + 46) || - (snake_pos.x + 1 == b_pos.x + 47) || - (snake_pos.x + 1 == b_pos.x + 48) || - (snake_pos.x + 1 == b_pos.x + 49) || - (snake_pos.x + 1 == b_pos.x + 50)) - ) { - - //printf("snake collision working \n"); - //audio feedback - if(blocknum > 0) { - b_pos.y = 0; - } - srn = 2; - blocknum = b_number[srn]; - send=1; - blocknum-=1; - if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; - speed = 2; - length-=1; - pad.tone(1000.0,0.1); - wait(0.05); - } - else { - speed = 1; - } - - } - - if ( - ((snake_pos.y == b_pos.y + 10)) && - ((snake_pos.x + 1 == b_pos.x + 51) || - (snake_pos.x + 1 == b_pos.x + 52) || - (snake_pos.x + 1 == b_pos.x + 53) || - (snake_pos.x + 1 == b_pos.x + 54) || - (snake_pos.x + 1 == b_pos.x + 55) || - (snake_pos.x + 1 == b_pos.x + 56) || - (snake_pos.x + 1 == b_pos.x + 57) || - (snake_pos.x + 1 == b_pos.x + 58) || - (snake_pos.x + 1 == b_pos.x + 59) || - (snake_pos.x + 1 == b_pos.x + 60) || - (snake_pos.x + 1 == b_pos.x + 61) || - (snake_pos.x + 1 == b_pos.x + 62) || - (snake_pos.x + 1 == b_pos.x + 63) || - (snake_pos.x + 1 == b_pos.x + 64) || - (snake_pos.x + 1 == b_pos.x + 65) || - (snake_pos.x + 1 == b_pos.x + 66)) - ) { - - //printf("snake collision working \n"); - //audio feedback - if(blocknum > 0) { - b_pos.y = 0; - } - srn = 3; - blocknum = b_number[srn]; - send=1; - blocknum-=1; - if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; - length-=1; - pad.tone(1000.0,0.1); - wait(0.05); - } - else { - speed = 1; - } - - } - - if ( - ((snake_pos.y == b_pos.y + 10)) && - ((snake_pos.x + 1 == b_pos.x + 67) || - (snake_pos.x + 1 == b_pos.x + 68) || - (snake_pos.x + 1 == b_pos.x + 69) || - (snake_pos.x + 1 == b_pos.x + 70) || - (snake_pos.x + 1 == b_pos.x + 71) || - (snake_pos.x + 1 == b_pos.x + 72) || - (snake_pos.x + 1 == b_pos.x + 73) || - (snake_pos.x + 1 == b_pos.x + 74) || - (snake_pos.x + 1 == b_pos.x + 75) || - (snake_pos.x + 1 == b_pos.x + 76) || - (snake_pos.x + 1 == b_pos.x + 77) || - (snake_pos.x + 1 == b_pos.x + 78) || - (snake_pos.x + 1 == b_pos.x + 79) || - (snake_pos.x + 1 == b_pos.x + 80) || - (snake_pos.x + 1 == b_pos.x + 81) || - (snake_pos.x + 1 == b_pos.x + 82) || - (snake_pos.x + 1 == b_pos.x + 83)) - ) { - - //printf("snake collision working \n"); - //audio feedback - if(blocknum > 0) { - b_pos.y = 0; - } - srn = 4; - blocknum = b_number[srn]; - send=1; - blocknum-=1; - if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; - speed = 2; - length-=1; - pad.tone(1000.0,0.1); - wait(0.05); - } - else { - speed = 1; - } - } - } void SnakevsBlock::CheckSnakeBlockSidesCollision(Gamepad &pad, Direction d) { - //Obtains all required coordinates. + //Obtains all required coordinates for checking block sides and snake collision. Vector2D b_pos = _b.get_pos(); snake_pos[0] = _s.get_pos(length); snake_pos[1] = _s.get_pos_before1(length); @@ -443,81 +370,53 @@ } for(int i=0; i<=14; i++) { - if ( - ((snake_pos[i].y == b_pos.y + 1) || - (snake_pos[i].y == b_pos.y + 1) || - (snake_pos[i].y == b_pos.y + 2) || - (snake_pos[i].y == b_pos.y + 3) || - (snake_pos[i].y == b_pos.y + 4) || - (snake_pos[i].y == b_pos.y + 5) || - (snake_pos[i].y == b_pos.y + 6) || - (snake_pos[i].y == b_pos.y + 7) || - (snake_pos[i].y == b_pos.y + 8) || - (snake_pos[i].y == b_pos.y + 9) || - (snake_pos[i].y == b_pos.y + 10) || - (snake_pos[i].y + 1 == b_pos.y) || - (snake_pos[i].y + 1 == b_pos.y + 1) || - (snake_pos[i].y + 1 == b_pos.y + 2) || - (snake_pos[i].y + 1 == b_pos.y + 3) || - (snake_pos[i].y + 1 == b_pos.y + 4) || - (snake_pos[i].y + 1 == b_pos.y + 5) || - (snake_pos[i].y + 1 == b_pos.y + 6) || - (snake_pos[i].y + 1 == b_pos.y + 7) || - (snake_pos[i].y + 1 == b_pos.y + 8) || - (snake_pos[i].y + 1 == b_pos.y + 9) || - (snake_pos[i].y + 1 == b_pos.y + 10) || - (snake_pos[i].y + 2 == b_pos.y) || - (snake_pos[i].y + 2 == b_pos.y + 1) || - (snake_pos[i].y + 2 == b_pos.y + 2) || - (snake_pos[i].y + 2 == b_pos.y + 3) || - (snake_pos[i].y + 2 == b_pos.y + 4) || - (snake_pos[i].y + 2 == b_pos.y + 5) || - (snake_pos[i].y + 2 == b_pos.y + 6) || - (snake_pos[i].y + 2 == b_pos.y + 7) || - (snake_pos[i].y + 2 == b_pos.y + 8) || - (snake_pos[i].y + 2 == b_pos.y + 9) || - (snake_pos[i].y + 2 == b_pos.y + 10))) { - - //For West side of walls - if( - (((snake_pos[i].x == b_pos.x + 4) || //W - (snake_pos[i].x == b_pos.x + 36) || //W - (snake_pos[i].x == b_pos.x + 68) || //W - (snake_pos[i].x + 1 == b_pos.x + 4) || //W - (snake_pos[i].x + 1 == b_pos.x + 36) || //W - (snake_pos[i].x + 1 == b_pos.x + 68) || //W - (snake_pos[i].x == b_pos.x + 20) || //W - (snake_pos[i].x == b_pos.x + 52) || //W - (snake_pos[i].x == b_pos.x + 84) || //W - (snake_pos[i].x + 1 == b_pos.x + 20) || //W - (snake_pos[i].x + 1 == b_pos.x + 52) || //W - (snake_pos[i].x + 1 == b_pos.x + 84))&&(d != E)&&(length > i)) //W - ) { - //code makes sure that the colliding part doesn't move in x axis. - for(int snake_beed_num=0; snake_beed_num<=15; snake_beed_num++) { - if(length == snake_beed_num + i) { - b[snake_beed_num - 1] = 0; + for(int a=0; a<=10; a++) { + if ( + (snake_pos[i].y == b_pos.y + a) || + (snake_pos[i].y + 1 == b_pos.y + a) || + (snake_pos[i].y + 2 == b_pos.y + a)) { + + //For West side of walls + if( + (((snake_pos[i].x == b_pos.x + 4) || //W + (snake_pos[i].x == b_pos.x + 36) || //W + (snake_pos[i].x == b_pos.x + 68) || //W + (snake_pos[i].x + 1 == b_pos.x + 4) || //W + (snake_pos[i].x + 1 == b_pos.x + 36) || //W + (snake_pos[i].x + 1 == b_pos.x + 68) || //W + (snake_pos[i].x == b_pos.x + 20) || //W + (snake_pos[i].x == b_pos.x + 52) || //W + (snake_pos[i].x == b_pos.x + 84) || //W + (snake_pos[i].x + 1 == b_pos.x + 20) || //W + (snake_pos[i].x + 1 == b_pos.x + 52) || //W + (snake_pos[i].x + 1 == b_pos.x + 84))&&(d != E)&&(length > i)) //W + ) { + //code makes sure that the colliding part doesn't move in x axis. + for(int snake_beed_num=0; snake_beed_num<=15; snake_beed_num++) { + if(length == snake_beed_num + i) { + b[snake_beed_num - 1] = 0; + } } } - } - - //for East side of walls - else if ( - (((snake_pos[i].x + 1 == b_pos.x + 18) || //E - (snake_pos[i].x + 1 == b_pos.x + 50) || //E - (snake_pos[i].x + 1 == b_pos.x + 82) || //E - (snake_pos[i].x + 2 == b_pos.x + 18) || //E - (snake_pos[i].x + 2 == b_pos.x + 50) || //E - (snake_pos[i].x + 2 == b_pos.x + 82) || //E - (snake_pos[i].x + 1 == b_pos.x + 2) || //E - (snake_pos[i].x + 1 == b_pos.x + 34) || //E - (snake_pos[i].x + 1 == b_pos.x + 66) || //E - (snake_pos[i].x + 2 == b_pos.x + 2) || //E - (snake_pos[i].x + 2 == b_pos.x + 34) || //E - (snake_pos[i].x + 2 == b_pos.x + 66))&&(d != W)) //E - ) { - //add some code that it doesn't move through - speed = 0; + + //for East side of walls + else if ( + (((snake_pos[i].x + 1 == b_pos.x + 18) || //E + (snake_pos[i].x + 1 == b_pos.x + 50) || //E + (snake_pos[i].x + 1 == b_pos.x + 82) || //E + (snake_pos[i].x + 2 == b_pos.x + 18) || //E + (snake_pos[i].x + 2 == b_pos.x + 50) || //E + (snake_pos[i].x + 2 == b_pos.x + 82) || //E + (snake_pos[i].x + 1 == b_pos.x + 2) || //E + (snake_pos[i].x + 1 == b_pos.x + 34) || //E + (snake_pos[i].x + 1 == b_pos.x + 66) || //E + (snake_pos[i].x + 2 == b_pos.x + 2) || //E + (snake_pos[i].x + 2 == b_pos.x + 34) || //E + (snake_pos[i].x + 2 == b_pos.x + 66))&&(d != W)) //E + ) { + //add some code that it doesn't move through + speed = 0; + } } } }