Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Diff: SnakevsBlock/SnakevsBlock.cpp
- Revision:
- 25:e827f1a8fadc
- Parent:
- 24:1c118b071430
- Child:
- 26:3495f7b0ede7
--- a/SnakevsBlock/SnakevsBlock.cpp Wed Apr 10 10:59:15 2019 +0000 +++ b/SnakevsBlock/SnakevsBlock.cpp Wed Apr 10 14:37:05 2019 +0000 @@ -23,6 +23,11 @@ for(int i=0; i<=14; i++) { b[i] = 1; } + _s.init(); + _f.init(); + _ff.init(); + _fff.init(); + _b.init(); } @@ -37,11 +42,11 @@ { length = _s.draw(pad, lcd, length, level); //Draws the Snake. //Make these snake buffs relative to the snake drops which in turn relate to the game speed if(foodbuff >=0) { - _f.draw(lcd); //Draws the first food. + _f.draw(lcd, blockgap, blockbuff); //Draws the first food. if(foodbuff >=50) { - _ff.draw(lcd); //Draws the second food. + _ff.draw(lcd, blockgap, blockbuff); //Draws the second food. if(foodbuff >=80) { - _fff.draw(lcd); //Draws the third food. + _fff.draw(lcd, blockgap, blockbuff); //Draws the third food. } } foodbuff +=1; @@ -56,19 +61,12 @@ //Code to print length on game screen. char bufferscore[14]; sprintf(bufferscore,"%d",length); - lcd.printString(bufferscore,1,0); - - if(pad.check_event(Gamepad::BACK_PRESSED)){ //Waits for Back button to be pressed. - - NVIC_SystemReset(); //Software Reset. - - } - + lcd.printString(bufferscore,1,0); } -void SnakevsBlock::update(Gamepad &pad) //Updates objects on screen. +int SnakevsBlock::update(Gamepad &pad) //Updates objects on screen. { CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks. CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food. @@ -92,6 +90,15 @@ blockgap -= 20; level += 1; } + if(pad.check_event(Gamepad::BACK_PRESSED)){ //Waits for Back button to be pressed. + back = 1; + //add some warning here and use A as the button to confirm + SnakevsBlock::init(); + } + else { + back = 0; + } + return back; } void SnakevsBlock::get_pos() @@ -120,106 +127,68 @@ //If statements check if the snake sprite has collided with any //of the three food sprites, if so then the food location is reset and //length of the snake is increased using the length variable. - + for(int i=0; i<=2; i++) { //this loop automatically detects each combination of collision + if ( + ((snake_pos.y + i == f_pos.y) || + (snake_pos.y + i == f_pos.y + 1) || + (snake_pos.y + i == f_pos.y + 2)) && + ((snake_pos.x + i == f_pos.x) || + (snake_pos.x + i == f_pos.x + 1) || + (snake_pos.x + i == f_pos.x + 2)) + ) { + + //printf("snake feast working \n"); + //audio feedback + pad.tone(1000.0,0.1); + f_pos.x = rand() % 82; + if((blockbuff>=11)&&(blockbuff<=blockgap-11)) { //this makes sure that the snake food appears seperated from the block + f_pos.y = -2; + } + length+=1; + + } + + if ( - ((snake_pos.y == f_pos.y) || - (snake_pos.y == f_pos.y + 1) || - (snake_pos.y == f_pos.y + 2) || - (snake_pos.y + 1== f_pos.y) || - (snake_pos.y + 1== f_pos.y + 1) || - (snake_pos.y + 1== f_pos.y + 2) || - (snake_pos.y + 2 == f_pos.y) || - (snake_pos.y + 2 == f_pos.y + 1) || - (snake_pos.y + 2 == f_pos.y + 2)) && - ((snake_pos.x == f_pos.x) || - (snake_pos.x == f_pos.x + 1) || - (snake_pos.x == f_pos.x + 2) || - (snake_pos.x + 1 == f_pos.x) || - (snake_pos.x + 1== f_pos.x + 1) || - (snake_pos.x + 1 == f_pos.x + 2) || - (snake_pos.x + 2 == f_pos.x) || - (snake_pos.x + 2 == f_pos.x + 1) || - (snake_pos.x + 2 == f_pos.x + 2)) + ((snake_pos.y + i== ff_pos.y) || + (snake_pos.y + i == ff_pos.y + 1) || + (snake_pos.y + i == ff_pos.y + 2)) && + ((snake_pos.x + i == ff_pos.x) || + (snake_pos.x + i == ff_pos.x + 1) || + (snake_pos.x + i == ff_pos.x + 2)) ) { //printf("snake feast working \n"); - //audio feedback + // audio feedback pad.tone(1000.0,0.1); - f_pos.x = rand() % 82; - if(blockbuff>25) { //change this while changing the block drop gap - f_pos.y = -2; + ff_pos.x = rand() % 82; + if((blockbuff>=11)&&(blockbuff<=blockgap-11)) { //this makes sure that the snake food appears seperated from the block + ff_pos.y = -2; } length+=1; } if ( - ((snake_pos.y == ff_pos.y) || - (snake_pos.y == ff_pos.y + 1) || - (snake_pos.y == ff_pos.y + 2) || - (snake_pos.y + 1== ff_pos.y) || - (snake_pos.y + 1== ff_pos.y + 1) || - (snake_pos.y + 1== ff_pos.y + 2) || - (snake_pos.y + 2 == ff_pos.y) || - (snake_pos.y + 2 == ff_pos.y + 1) || - (snake_pos.y + 2 == ff_pos.y + 2)) && - ((snake_pos.x == ff_pos.x) || - (snake_pos.x == ff_pos.x + 1) || - (snake_pos.x == ff_pos.x + 2) || - (snake_pos.x + 1 == ff_pos.x) || - (snake_pos.x + 1== ff_pos.x + 1) || - (snake_pos.x + 1 == ff_pos.x + 2) || - (snake_pos.x + 2 == ff_pos.x) || - (snake_pos.x + 2 == ff_pos.x + 1) || - (snake_pos.x + 2 == ff_pos.x + 2)) - ) { - - //printf("snake feast working \n"); - // audio feedback - pad.tone(1000.0,0.1); - ff_pos.x = rand() % 82; - ff_pos.y = -2; - if(blockbuff>25) { //change this while changing the block drop gap - f_pos.y = -2; - } - length+=1; - - } - - if ( - ((snake_pos.y == fff_pos.y) || - (snake_pos.y == fff_pos.y + 1) || - (snake_pos.y == fff_pos.y + 2) || - (snake_pos.y + 1== fff_pos.y) || - (snake_pos.y + 1== fff_pos.y + 1) || - (snake_pos.y + 1== fff_pos.y + 2) || - (snake_pos.y + 2 == fff_pos.y) || - (snake_pos.y + 2 == fff_pos.y + 1) || - (snake_pos.y + 2 == fff_pos.y + 2)) && - ((snake_pos.x == fff_pos.x) || - (snake_pos.x == fff_pos.x + 1) || - (snake_pos.x == fff_pos.x + 2) || - (snake_pos.x + 1 == fff_pos.x) || - (snake_pos.x + 1== fff_pos.x + 1) || - (snake_pos.x + 1 == fff_pos.x + 2) || - (snake_pos.x + 2 == fff_pos.x) || - (snake_pos.x + 2 == fff_pos.x + 1) || - (snake_pos.x + 2 == fff_pos.x + 2)) + ((snake_pos.y + i == fff_pos.y) || + (snake_pos.y + i == fff_pos.y + 1) || + (snake_pos.y + i == fff_pos.y + 2)) && + ((snake_pos.x + i == fff_pos.x) || + (snake_pos.x + i == fff_pos.x + 1) || + (snake_pos.x + i == fff_pos.x + 2)) ) { //printf("snake feast working \n"); // audio feedback pad.tone(1000.0,0.1); fff_pos.x = rand() % 82; - fff_pos.y = -2; - if(blockbuff>25) { //change this while changing the block drop gap - f_pos.y = -2; + if((blockbuff>=11)&&(blockbuff<=blockgap-11)) { //this makes sure that the snake food appears seperated from the block + fff_pos.y = -2; } length+=1; } - - + } _f.set_pos(f_pos); _ff.set_pos(ff_pos); _fff.set_pos(fff_pos); @@ -239,9 +208,7 @@ //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.y == b_pos.y + 10) || - (snake_pos.y == b_pos.y + 10)) && + ((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) || @@ -283,9 +250,7 @@ } if ( - ((snake_pos.y == b_pos.y + 10) || - (snake_pos.y == b_pos.y + 10) || - (snake_pos.y == b_pos.y + 10)) && + ((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) || @@ -326,9 +291,7 @@ } if ( - ((snake_pos.y == b_pos.y + 10) || - (snake_pos.y == b_pos.y + 10) || - (snake_pos.y == b_pos.y + 10)) && + ((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) || @@ -369,9 +332,7 @@ } if ( - ((snake_pos.y == b_pos.y + 10) || - (snake_pos.y == b_pos.y + 10) || - (snake_pos.y == b_pos.y + 10)) && + ((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) || @@ -411,9 +372,7 @@ } if ( - ((snake_pos.y == b_pos.y + 10) || - (snake_pos.y == b_pos.y + 10) || - (snake_pos.y == b_pos.y + 10)) && + ((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) || @@ -483,10 +442,9 @@ b[i] = 1; } - //For West 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 + 1) || (snake_pos[i].y == b_pos.y + 2) || (snake_pos[i].y == b_pos.y + 3) || @@ -518,90 +476,49 @@ (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)) && - ((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 + 2 == b_pos.x + 4) || //W - (snake_pos[i].x + 2 == b_pos.x + 36) || //W - (snake_pos[i].x + 2 == 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) || //W - (snake_pos[i].x + 2 == b_pos.x + 20) || //W - (snake_pos[i].x + 2 == b_pos.x + 52) || //W - (snake_pos[i].x + 2 == 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; + (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 East - 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)) && - ((snake_pos[i].x == b_pos.x + 18) || //E - (snake_pos[i].x == b_pos.x + 50) || //E - (snake_pos[i].x == b_pos.x + 82) || //E - (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 == b_pos.x + 2) || //E - (snake_pos[i].x == b_pos.x + 34) || //E - (snake_pos[i].x == b_pos.x + 66) || //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; + } } } } \ No newline at end of file