Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Diff: SnakevsBlock/SnakevsBlock.cpp
- Revision:
- 41:4edac50f010d
- Parent:
- 40:1debed7ec01c
- Child:
- 42:973bb6036f81
--- a/SnakevsBlock/SnakevsBlock.cpp Mon Apr 22 07:10:52 2019 +0000 +++ b/SnakevsBlock/SnakevsBlock.cpp Tue Apr 23 12:25:49 2019 +0000 @@ -12,20 +12,17 @@ void SnakevsBlock::init() { - //The snake length configuration and all the other initial information passing will be done here - length = 3; + //The level initialisation and all the other initial information passing will be done here level = 1; - garbage = 0; - angle = 0; - foodbuff = 0; - send=0; - speed = 1; - blockgap = 450; + garbage = 0; //this is to allow the user to change the position of reference for motion control by saving the absolute angle. + foodbuff = 0; //this makes the food fall at diffrent times when a particular level starts. + send = 0; + blockgap = 500; blockbuff = -50; - velocity = 1; for(int i=0; i<=14; i++) { b[i] = 1; } + _l.init(); _s.init(); _f.init(); _ff.init(); @@ -33,7 +30,15 @@ _b.init(); } - +void SnakevsBlock::reset() +{ + //This prepares the game for the next level by reseting certain variables. + foodbuff = 0; + _f.init(); + _ff.init(); + _fff.init(); + _b.init(); +} void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int g_mode) { @@ -66,56 +71,72 @@ //printf("%f",device.get_roll_angle()); } -void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad) { - 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) { +void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad, SDFileSystem &sd) { + _length = _l._getLength(); + _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, blockbuff); //Draws the first food. } - if(foodbuff >=50) { + if(foodbuff >= 50) { _ff.draw(lcd, blockbuff); //Draws the second food. } - if(foodbuff >=80) { + if(foodbuff >= 80) { _fff.draw(lcd, blockbuff); //Draws the third food. } foodbuff +=1; - if(foodbuff >=8) { - send = _b.draw(lcd, length, blockgap); + if(foodbuff >= 8) { + _b.draw(lcd, _length, blockgap); } if(foodbuff == 8) { blockbuff = -10; } //Code to print length on game screen. - SnakevsBlock::print_length_on_screen(lcd); -} + _l.print_length_on_screen(lcd); + + if(_length >= 20) { + if(blockgap >= 50) { //to make progressive levels harder + blockgap -= 40; + } + level += 1; + _statset.write(level, sd); + SnakevsBlock::reset(); + lcd.clear(); + lcd.printString("Level Complete",0,1); + lcd.printString("Press Start",10,3); + lcd.printString("to Proceed",12,4); + lcd.refresh(); + while (pad.check_event(Gamepad::START_PRESSED) == false) { + pad.leds_on(); + pad.tone(rand()%1001,0.1); //helps to set max freq for random sound + wait(0.1); + pad.leds_off(); + wait(0.1); + } + lcd.clear(); + sprintf(bufferlevel,"%d",level); + lcd.printString("Level:",25,1); + lcd.printString(bufferlevel,40,3); + lcd.refresh(); + wait(2); + _l._setLength(3); + } +} -void SnakevsBlock::print_length_on_screen(N5110 &lcd) +int SnakevsBlock::update(Gamepad &pad) //Updates objects on screen. { - char bufferscore[14]; - sprintf(bufferscore,"%d",length); - lcd.printString(bufferscore,1,0); -} - -int SnakevsBlock::update(Gamepad &pad, SDFileSystem &sd) //Updates objects on screen. -{ + send = 0; 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. CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides. - _s.update(_d, length, speed, b); //_d is the direction of joystick and b controls the motion of a section of the snake relative to obstruction + _s.update(_d, b); //_d is the direction of joystick and b controls the motion of a section of the snake relative to obstruction _f.update(); _ff.update(); _fff.update(); _b.update(blocknum, srn, send); blockbuff++; - if(blockbuff == blockgap) { //this makes blockbuff reset every time the new set of blocks appear. + if(blockbuff >= blockgap) { //this makes blockbuff reset every time the new set of blocks appear. blockbuff = -11; } - if(length >= 20) { //to make progressive levels harder - if(blockgap>=50) { //to make progressive levels harder - blockgap -= 20; - } - level += 1; - _statset.write(level, sd); - } 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 @@ -129,24 +150,34 @@ void SnakevsBlock::get_pos() { - Vector2D snake_pos = _s.get_pos(length); //printf("player pos = %f %f \n", player_pos.x, player_pos.y); //top left of player sprite // 81.000000 0.000000 top right // 0.000000 0.000000 is top left // 81.000000 45.000000 bottom right - snakex = snake_pos.x; - snakey = snake_pos.y; + snakex = _s.get_pos().x; + snakey = _s.get_pos().y; //printf("snakexy in GAME = %d %d \n", snakex, snakey); + //Obtains all required coordinates. + food_pos[0] = _f.get_pos(); + food_pos[1] = _ff.get_pos(); + food_pos[2] = _fff.get_pos(); + //obtains origin cordinates of block. + b_pos = _b.get_pos(); + //this saves the positions of each snake beed (the first to the last) in a single array. Element[0] is the top beed and soo on. + snake_pos[0] = _s.get_pos(); //gets the position of the top beed and saves in array. + snake_pos[1] = _s.get_pos_before1(); //gets the position of the second beed and saves in array. + snake_pos[2] = _s.get_pos_before2(); //gets the position of the third beed and saves in array. + snake_pos[3] = _s.get_pos_before3(); //gets the position of the fourth beed and saves in array. + snake_pos[4] = _s.get_pos_before4(); //gets the position of the fifth beed and saves in array. + snake_pos[5] = _s.get_pos_before5(); //gets the position of the sixth beed and saves in array. + snake_pos[6] = _s.get_pos_before6(); //gets the position of the seventh beed and saves in array. + snake_pos[7] = _s.get_pos_before7(); //gets the position of the eight beed and saves in array. + snake_pos[8] = _s.get_pos_before8(); //gets the position of the ninth beed and saves in array. + snake_pos[9] = _s.get_pos_before9(); //gets the position of the last beed and saves in array. } void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad) { - //Obtains all required coordinates. - Vector2D food_pos[3]; - food_pos[0] = _f.get_pos(); - food_pos[1] = _ff.get_pos(); - food_pos[2] = _fff.get_pos(); - Vector2D snake_pos = _s.get_pos(length); //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. @@ -154,19 +185,19 @@ for(int x=0; x<=2; x++) { //this loop automatically detects each combination of collision in the x postion for(int food_sr=0; food_sr<=2; food_sr++) { //this loop automatically detects which food we are interacting with. if ( - ((snake_pos.y + y == food_pos[food_sr].y) || - (snake_pos.y + y == food_pos[food_sr].y + 1) || - (snake_pos.y + y == food_pos[food_sr].y + 2)) && - ((snake_pos.x + x == food_pos[food_sr].x) || - (snake_pos.x + x == food_pos[food_sr].x + 1) || - (snake_pos.x + x == food_pos[food_sr].x + 2)) + ((snakey + y == food_pos[food_sr].y) || + (snakey + y == food_pos[food_sr].y + 1) || + (snakey + y == food_pos[food_sr].y + 2)) && + ((snakex + x == food_pos[food_sr].x) || + (snakex + x == food_pos[food_sr].x + 1) || + (snakex + x == food_pos[food_sr].x + 2)) ) { //printf("snake feast working \n"); //audio feedback pad.tone(1000.0,0.1); food_pos[food_sr].x = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis. food_pos[food_sr].y = -3; - length+=1; + _l.PlusLength(); } } } @@ -177,23 +208,20 @@ } void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad) { - //Obtains all required coordinates. - Vector2D b_pos = _b.get_pos(); - int *b_number; + //Obtains the numbers inside the block. b_number = _b.get_number(); - Vector2D snake_pos = _s.get_pos(length); //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) for(int block=0; block<=83; block+=1) { //this loop automatically detects for each section of block and each combination of collision - if ((snake_pos.y == b_pos.y + 10) && (snake_pos.x + 1 == b_pos.x + block)) { + if ((snakey == b_pos.y + 10) && (snakex + 1 == b_pos.x + block)) { //printf("snake collision working \n"); //audio feedback if(blocknum > 0) {b_pos.y = 0;} //change this to speed y = 0 when length = 10. srn = CheckBlock(block); //this tells us which of the 5 blocks we are colliding with blocknum = b_number[srn]; ImplementCollision(pad); - if((length>=10)&&(b_number[srn]>0)) { //this makes the block stop moving down if it's length is more than 10 and still collides. + if((_length>=10)&&(b_number[srn]>0)) { //this makes the block stop moving down if it's length is more than 10 and still collides. velocity = 0; } else { @@ -222,10 +250,10 @@ } void SnakevsBlock::ImplementCollision(Gamepad &pad) { - send=1; + 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; + if(blocknum >= 0) { // to make sure that snake doesn't decrease in _length if number on the block is less than 1; + _l.MinusLength(); pad.tone(1000.0,0.1); wait(0.04); } @@ -233,25 +261,10 @@ void SnakevsBlock::CheckSnakeBlockSidesCollision(Gamepad &pad) { - //Obtains all required coordinates for checking block sides and snake collision. - Vector2D b_pos = _b.get_pos(); - snake_pos[0] = _s.get_pos(length); //gets the position of the top beed and saves in array. - snake_pos[1] = _s.get_pos_before1(length); //gets the position of the second beed and saves in array. - snake_pos[2] = _s.get_pos_before2(length); //gets the position of the third beed and saves in array. - snake_pos[3] = _s.get_pos_before3(length); //gets the position of the fourth beed and saves in array. - snake_pos[4] = _s.get_pos_before4(length); //gets the position of the fifth beed and saves in array. - snake_pos[5] = _s.get_pos_before5(length); //gets the position of the sixth beed and saves in array. - snake_pos[6] = _s.get_pos_before6(length); //gets the position of the seventh beed and saves in array. - snake_pos[7] = _s.get_pos_before7(length); //gets the position of the eight beed and saves in array. - snake_pos[8] = _s.get_pos_before8(length); //gets the position of the ninth beed and saves in array. - snake_pos[9] = _s.get_pos_before9(length); //gets the position of the last beed and saves in array. //If statements check if the snake sprite has collided with any //of the blocks' sides and then stop the snake moving in x axis - int _length; - - if(length<=10) {_length = length;} //to stop the snake length virtually at 10 when it goes past it. - else {_length = 10;} + if(_length>=10) {_length = 10;} //to stop the snake length virtually at 10 when it goes past it. for(int i=0; i<=9; i++) { b[i] = 1;