Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
SnakevsBlock/SnakevsBlock.cpp
- Committer:
- AhmedPlaymaker
- Date:
- 2019-04-02
- Revision:
- 12:1e601b176437
- Parent:
- 10:751bd953fa27
- Child:
- 13:9785f2404045
File content as of revision 12:1e601b176437:
#include "SnakevsBlock.h" SnakevsBlock::SnakevsBlock() { } SnakevsBlock::~SnakevsBlock() { } void SnakevsBlock::init() { //The snake length configuration and all the other initial information passing will be done here length = 3; foodbuff = 0; send=0; //memory = 0; } void SnakevsBlock::read_input(Gamepad &pad) { _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick. _mag = pad.get_mag(); //Obtains Magnitude of Joystick. } void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad) { _s.draw(lcd, length); //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. if(foodbuff >=50) { _ff.draw(lcd); //Draws the second food. if(foodbuff >=80) { _fff.draw(lcd); //Draws the third food. } } foodbuff +=1; if(foodbuff == 110) { foodbuff = 110; } } send = _b.draw(lcd, length, blocknum, srn); //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. } } void SnakevsBlock::update(Gamepad &pad) //Updates objects on screen. { CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food. CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks. _s.update(_d,_mag, length); _f.update(); _ff.update(); _fff.update(); _b.update(blocknum, srn, send); } 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; //printf("snakexy in GAME = %d %d \n", snakex, snakey); } void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad) { //Obtains all required coordinates. Vector2D f_pos = _f.get_pos(); Vector2D ff_pos = _ff.get_pos(); Vector2D fff_pos = _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. 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)) ) { //printf("snake feast working \n"); //audio feedback pad.tone(1000.0,0.1); f_pos.x = rand() % 82; f_pos.y = 0; 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 = 0; 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)) ) { //printf("snake feast working \n"); // audio feedback pad.tone(1000.0,0.1); fff_pos.x = rand() % 82; fff_pos.y = 0; length+=1; } _f.set_pos(f_pos); _ff.set_pos(ff_pos); _fff.set_pos(fff_pos); } void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad) { //Obtains all required coordinates. Vector2D b_pos = _b.get_pos(); int *b_number; 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) if ( ((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) || (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 > -1) { b_pos.y = 0; pad.tone(1000.0,0.1); } srn = 0; blocknum = b_number[srn]; send=1; blocknum-=1; if(blocknum >= -1) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; length-=1; } wait(0.1); } if ( ((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) || (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 > -1) { b_pos.y = 0; pad.tone(1000.0,0.1); } srn = 1; blocknum = b_number[srn]; send=1; blocknum-=1; if(blocknum >= -1) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; length-=1; } wait(0.1); } if ( ((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) || (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 > -1) { b_pos.y = 0; pad.tone(1000.0,0.1); } srn = 2; blocknum = b_number[srn]; send=1; blocknum-=1; if(blocknum >= -1) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; length-=1; } wait(0.1); } if ( ((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) || (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 > -1) { b_pos.y = 0; pad.tone(1000.0,0.1); } srn = 3; blocknum = b_number[srn]; send=1; blocknum-=1; if(blocknum >= -1) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; length-=1; } wait(0.1); } if ( ((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) || (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 > -1) { b_pos.y = 0; pad.tone(1000.0,0.1); } srn = 4; blocknum = b_number[srn]; send=1; blocknum-=1; if(blocknum >= -1) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; length-=1; } wait(0.1); } }