Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
SnakevsBlock/SnakevsBlock.cpp@38:30e4e6191762, 2019-04-22 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Mon Apr 22 04:24:38 2019 +0000
- Revision:
- 38:30e4e6191762
- Parent:
- 37:ee47699915b8
- Child:
- 39:210ac915e0a0
This might be the biggest yet!!! THIS HAS MOTION CONTROL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 7:48ba87cd79b5 | 1 | #include "SnakevsBlock.h" |
AhmedPlaymaker | 7:48ba87cd79b5 | 2 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 3 | SnakevsBlock::SnakevsBlock() |
AhmedPlaymaker | 7:48ba87cd79b5 | 4 | { |
AhmedPlaymaker | 7:48ba87cd79b5 | 5 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 6 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 7 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 8 | SnakevsBlock::~SnakevsBlock() |
AhmedPlaymaker | 7:48ba87cd79b5 | 9 | { |
AhmedPlaymaker | 7:48ba87cd79b5 | 10 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 11 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 12 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 13 | void SnakevsBlock::init() |
AhmedPlaymaker | 7:48ba87cd79b5 | 14 | { |
AhmedPlaymaker | 7:48ba87cd79b5 | 15 | //The snake length configuration and all the other initial information passing will be done here |
AhmedPlaymaker | 12:1e601b176437 | 16 | length = 3; |
AhmedPlaymaker | 19:05cc9f801468 | 17 | level = 1; |
AhmedPlaymaker | 38:30e4e6191762 | 18 | garbage = 0; |
AhmedPlaymaker | 38:30e4e6191762 | 19 | angle = 0; |
AhmedPlaymaker | 12:1e601b176437 | 20 | foodbuff = 0; |
AhmedPlaymaker | 12:1e601b176437 | 21 | send=0; |
AhmedPlaymaker | 13:9785f2404045 | 22 | speed = 1; |
AhmedPlaymaker | 36:dfdd619874ae | 23 | blockgap = 400; |
AhmedPlaymaker | 32:3a3bdeffdf62 | 24 | blockbuff = -50; |
AhmedPlaymaker | 22:ee698f66146f | 25 | for(int i=0; i<=14; i++) { |
AhmedPlaymaker | 22:ee698f66146f | 26 | b[i] = 1; |
AhmedPlaymaker | 22:ee698f66146f | 27 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 28 | _s.init(); |
AhmedPlaymaker | 25:e827f1a8fadc | 29 | _f.init(); |
AhmedPlaymaker | 25:e827f1a8fadc | 30 | _ff.init(); |
AhmedPlaymaker | 25:e827f1a8fadc | 31 | _fff.init(); |
AhmedPlaymaker | 25:e827f1a8fadc | 32 | _b.init(); |
AhmedPlaymaker | 7:48ba87cd79b5 | 33 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 34 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 35 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 36 | |
AhmedPlaymaker | 38:30e4e6191762 | 37 | void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int gm) |
AhmedPlaymaker | 7:48ba87cd79b5 | 38 | { |
AhmedPlaymaker | 38:30e4e6191762 | 39 | device.get_values(); |
AhmedPlaymaker | 38:30e4e6191762 | 40 | angle = -device.get_roll_angle(); |
AhmedPlaymaker | 38:30e4e6191762 | 41 | //if button A is pressed then reset that particular position to center |
AhmedPlaymaker | 38:30e4e6191762 | 42 | if (pad.check_event(Gamepad::A_PRESSED) == true) { |
AhmedPlaymaker | 38:30e4e6191762 | 43 | garbage = angle; |
AhmedPlaymaker | 38:30e4e6191762 | 44 | } |
AhmedPlaymaker | 38:30e4e6191762 | 45 | device.get_values(); |
AhmedPlaymaker | 38:30e4e6191762 | 46 | angle = -device.get_roll_angle() - garbage; |
AhmedPlaymaker | 38:30e4e6191762 | 47 | if(gm == 1) { |
AhmedPlaymaker | 38:30e4e6191762 | 48 | _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick. |
AhmedPlaymaker | 38:30e4e6191762 | 49 | _mag = pad.get_mag(); //Obtains Magnitude of Joystick. |
AhmedPlaymaker | 38:30e4e6191762 | 50 | } |
AhmedPlaymaker | 38:30e4e6191762 | 51 | else if(gm == 2) { |
AhmedPlaymaker | 38:30e4e6191762 | 52 | if (angle >= 8) { |
AhmedPlaymaker | 38:30e4e6191762 | 53 | _d = E; |
AhmedPlaymaker | 38:30e4e6191762 | 54 | } |
AhmedPlaymaker | 38:30e4e6191762 | 55 | else if (angle <= -8) { |
AhmedPlaymaker | 38:30e4e6191762 | 56 | _d = W; |
AhmedPlaymaker | 38:30e4e6191762 | 57 | } |
AhmedPlaymaker | 38:30e4e6191762 | 58 | else { |
AhmedPlaymaker | 38:30e4e6191762 | 59 | _d = CENTRE; |
AhmedPlaymaker | 38:30e4e6191762 | 60 | } |
AhmedPlaymaker | 38:30e4e6191762 | 61 | } |
AhmedPlaymaker | 38:30e4e6191762 | 62 | device.get_values(); |
AhmedPlaymaker | 38:30e4e6191762 | 63 | //printf("%d",gm); |
AhmedPlaymaker | 38:30e4e6191762 | 64 | //printf("%f",angle); |
AhmedPlaymaker | 38:30e4e6191762 | 65 | //printf("%f",device.get_roll_angle()); |
AhmedPlaymaker | 7:48ba87cd79b5 | 66 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 67 | |
AhmedPlaymaker | 27:3b4775a0d0bb | 68 | void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad) { |
AhmedPlaymaker | 20:1e6338403427 | 69 | 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 |
AhmedPlaymaker | 9:d1d79d4ee673 | 70 | if(foodbuff >=0) { |
AhmedPlaymaker | 36:dfdd619874ae | 71 | _f.draw(lcd, blockbuff); //Draws the first food. |
AhmedPlaymaker | 9:d1d79d4ee673 | 72 | if(foodbuff >=50) { |
AhmedPlaymaker | 36:dfdd619874ae | 73 | _ff.draw(lcd, blockbuff); //Draws the second food. |
AhmedPlaymaker | 9:d1d79d4ee673 | 74 | if(foodbuff >=80) { |
AhmedPlaymaker | 36:dfdd619874ae | 75 | _fff.draw(lcd, blockbuff); //Draws the third food. |
AhmedPlaymaker | 9:d1d79d4ee673 | 76 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 77 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 78 | foodbuff +=1; |
AhmedPlaymaker | 9:d1d79d4ee673 | 79 | } |
AhmedPlaymaker | 16:7b474f873683 | 80 | if(foodbuff >=8) { |
AhmedPlaymaker | 36:dfdd619874ae | 81 | send = _b.draw(lcd, length, blockgap); |
AhmedPlaymaker | 19:05cc9f801468 | 82 | } |
AhmedPlaymaker | 36:dfdd619874ae | 83 | if(foodbuff == 8) { |
AhmedPlaymaker | 38:30e4e6191762 | 84 | blockbuff = -10; |
AhmedPlaymaker | 13:9785f2404045 | 85 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 86 | //Code to print length on game screen. |
AhmedPlaymaker | 7:48ba87cd79b5 | 87 | char bufferscore[14]; |
AhmedPlaymaker | 7:48ba87cd79b5 | 88 | sprintf(bufferscore,"%d",length); |
AhmedPlaymaker | 25:e827f1a8fadc | 89 | lcd.printString(bufferscore,1,0); |
AhmedPlaymaker | 7:48ba87cd79b5 | 90 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 91 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 92 | |
AhmedPlaymaker | 33:249cf423fb18 | 93 | int SnakevsBlock::update(Gamepad &pad, SDFileSystem &sd) //Updates objects on screen. |
AhmedPlaymaker | 7:48ba87cd79b5 | 94 | { |
AhmedPlaymaker | 18:b391caa5754c | 95 | CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks. |
AhmedPlaymaker | 12:1e601b176437 | 96 | CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food. |
AhmedPlaymaker | 38:30e4e6191762 | 97 | CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides. |
AhmedPlaymaker | 38:30e4e6191762 | 98 | _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 |
AhmedPlaymaker | 9:d1d79d4ee673 | 99 | _f.update(); |
AhmedPlaymaker | 9:d1d79d4ee673 | 100 | _ff.update(); |
AhmedPlaymaker | 9:d1d79d4ee673 | 101 | _fff.update(); |
AhmedPlaymaker | 12:1e601b176437 | 102 | _b.update(blocknum, srn, send); |
AhmedPlaymaker | 19:05cc9f801468 | 103 | blockbuff++; |
AhmedPlaymaker | 36:dfdd619874ae | 104 | if(blockbuff == blockgap) { //this makes blockbuff reset every time the new set of blocks appear. |
AhmedPlaymaker | 36:dfdd619874ae | 105 | blockbuff = -11; |
AhmedPlaymaker | 19:05cc9f801468 | 106 | } |
AhmedPlaymaker | 37:ee47699915b8 | 107 | if(length >= 20) { //to make progressive levels harder |
AhmedPlaymaker | 33:249cf423fb18 | 108 | if(blockgap>=50) { //to make progressive levels harder |
AhmedPlaymaker | 33:249cf423fb18 | 109 | blockgap -= 20; |
AhmedPlaymaker | 33:249cf423fb18 | 110 | } |
AhmedPlaymaker | 19:05cc9f801468 | 111 | level += 1; |
AhmedPlaymaker | 33:249cf423fb18 | 112 | _statset.write(level, sd); |
AhmedPlaymaker | 16:7b474f873683 | 113 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 114 | if(pad.check_event(Gamepad::BACK_PRESSED)){ //Waits for Back button to be pressed. |
AhmedPlaymaker | 25:e827f1a8fadc | 115 | back = 1; |
AhmedPlaymaker | 25:e827f1a8fadc | 116 | //add some warning here and use A as the button to confirm |
AhmedPlaymaker | 25:e827f1a8fadc | 117 | SnakevsBlock::init(); |
AhmedPlaymaker | 25:e827f1a8fadc | 118 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 119 | else { |
AhmedPlaymaker | 25:e827f1a8fadc | 120 | back = 0; |
AhmedPlaymaker | 25:e827f1a8fadc | 121 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 122 | return back; |
AhmedPlaymaker | 7:48ba87cd79b5 | 123 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 124 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 125 | void SnakevsBlock::get_pos() |
AhmedPlaymaker | 7:48ba87cd79b5 | 126 | { |
AhmedPlaymaker | 9:d1d79d4ee673 | 127 | Vector2D snake_pos = _s.get_pos(length); |
AhmedPlaymaker | 7:48ba87cd79b5 | 128 | //printf("player pos = %f %f \n", player_pos.x, player_pos.y); //top left of player sprite |
AhmedPlaymaker | 7:48ba87cd79b5 | 129 | // 81.000000 0.000000 top right |
AhmedPlaymaker | 7:48ba87cd79b5 | 130 | // 0.000000 0.000000 is top left |
AhmedPlaymaker | 7:48ba87cd79b5 | 131 | // 81.000000 45.000000 bottom right |
AhmedPlaymaker | 7:48ba87cd79b5 | 132 | snakex = snake_pos.x; |
AhmedPlaymaker | 7:48ba87cd79b5 | 133 | snakey = snake_pos.y; |
AhmedPlaymaker | 7:48ba87cd79b5 | 134 | //printf("snakexy in GAME = %d %d \n", snakex, snakey); |
AhmedPlaymaker | 7:48ba87cd79b5 | 135 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 136 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 137 | |
AhmedPlaymaker | 27:3b4775a0d0bb | 138 | void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 139 | //Obtains all required coordinates. |
AhmedPlaymaker | 32:3a3bdeffdf62 | 140 | Vector2D food_pos[3]; |
AhmedPlaymaker | 32:3a3bdeffdf62 | 141 | food_pos[0] = _f.get_pos(); |
AhmedPlaymaker | 32:3a3bdeffdf62 | 142 | food_pos[1] = _ff.get_pos(); |
AhmedPlaymaker | 32:3a3bdeffdf62 | 143 | food_pos[2] = _fff.get_pos(); |
AhmedPlaymaker | 32:3a3bdeffdf62 | 144 | Vector2D snake_pos = _s.get_pos(length); |
AhmedPlaymaker | 32:3a3bdeffdf62 | 145 | //If statements check if the snake sprite has collided with any |
AhmedPlaymaker | 32:3a3bdeffdf62 | 146 | //of the three food sprites, if so then the food location is reset and |
AhmedPlaymaker | 32:3a3bdeffdf62 | 147 | //length of the snake is increased using the length variable. |
AhmedPlaymaker | 29:c6358c39a70e | 148 | for(int y=0; y<=2; y++) { //this loop automatically detects each combination of collision in the y postion |
AhmedPlaymaker | 29:c6358c39a70e | 149 | for(int x=0; x<=2; x++) { //this loop automatically detects each combination of collision in the x postion |
AhmedPlaymaker | 29:c6358c39a70e | 150 | for(int food_sr=0; food_sr<=2; food_sr++) { //this loop automatically detects which food we are interacting with. |
AhmedPlaymaker | 29:c6358c39a70e | 151 | if ( |
AhmedPlaymaker | 29:c6358c39a70e | 152 | ((snake_pos.y + y == food_pos[food_sr].y) || |
AhmedPlaymaker | 29:c6358c39a70e | 153 | (snake_pos.y + y == food_pos[food_sr].y + 1) || |
AhmedPlaymaker | 29:c6358c39a70e | 154 | (snake_pos.y + y == food_pos[food_sr].y + 2)) && |
AhmedPlaymaker | 29:c6358c39a70e | 155 | ((snake_pos.x + x == food_pos[food_sr].x) || |
AhmedPlaymaker | 29:c6358c39a70e | 156 | (snake_pos.x + x == food_pos[food_sr].x + 1) || |
AhmedPlaymaker | 29:c6358c39a70e | 157 | (snake_pos.x + x == food_pos[food_sr].x + 2)) |
AhmedPlaymaker | 29:c6358c39a70e | 158 | ) { |
AhmedPlaymaker | 29:c6358c39a70e | 159 | //printf("snake feast working \n"); |
AhmedPlaymaker | 29:c6358c39a70e | 160 | //audio feedback |
AhmedPlaymaker | 29:c6358c39a70e | 161 | pad.tone(1000.0,0.1); |
AhmedPlaymaker | 38:30e4e6191762 | 162 | food_pos[food_sr].x = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis. |
AhmedPlaymaker | 38:30e4e6191762 | 163 | food_pos[food_sr].y = -3; |
AhmedPlaymaker | 29:c6358c39a70e | 164 | length+=1; |
AhmedPlaymaker | 29:c6358c39a70e | 165 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 166 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 167 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 168 | } |
AhmedPlaymaker | 29:c6358c39a70e | 169 | _f.set_pos(food_pos[0]); |
AhmedPlaymaker | 29:c6358c39a70e | 170 | _ff.set_pos(food_pos[1]); |
AhmedPlaymaker | 29:c6358c39a70e | 171 | _fff.set_pos(food_pos[2]); |
AhmedPlaymaker | 29:c6358c39a70e | 172 | } |
AhmedPlaymaker | 12:1e601b176437 | 173 | |
AhmedPlaymaker | 12:1e601b176437 | 174 | void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad) |
AhmedPlaymaker | 12:1e601b176437 | 175 | { |
AhmedPlaymaker | 12:1e601b176437 | 176 | //Obtains all required coordinates. |
AhmedPlaymaker | 12:1e601b176437 | 177 | Vector2D b_pos = _b.get_pos(); |
AhmedPlaymaker | 12:1e601b176437 | 178 | int *b_number; |
AhmedPlaymaker | 12:1e601b176437 | 179 | b_number = _b.get_number(); |
AhmedPlaymaker | 12:1e601b176437 | 180 | Vector2D snake_pos = _s.get_pos(length); |
AhmedPlaymaker | 12:1e601b176437 | 181 | //If statements check if the snake sprite has collided with any |
AhmedPlaymaker | 12:1e601b176437 | 182 | //of the blocks which are a maximum of 5, if so then the snake length reduces and the block number reduces |
AhmedPlaymaker | 12:1e601b176437 | 183 | //the block has to move slower and come down after every 2/3 iterations(dependent on the snake size.(think about this) |
AhmedPlaymaker | 33:249cf423fb18 | 184 | for(int block=0; block<=83; block+=1) { //this loop automatically detects for each section of block and each combination of collision |
AhmedPlaymaker | 33:249cf423fb18 | 185 | if ((snake_pos.y == b_pos.y + 10) && (snake_pos.x + 1 == b_pos.x + block)) { |
AhmedPlaymaker | 33:249cf423fb18 | 186 | //printf("snake collision working \n"); |
AhmedPlaymaker | 33:249cf423fb18 | 187 | //audio feedback |
AhmedPlaymaker | 33:249cf423fb18 | 188 | if(blocknum > 0) {b_pos.y = 0;} //change this to speed y = 0 when length = 10. |
AhmedPlaymaker | 33:249cf423fb18 | 189 | srn = CheckBlock(block); //this tells us which of the 5 blocks we are colliding with |
AhmedPlaymaker | 33:249cf423fb18 | 190 | blocknum = b_number[srn]; |
AhmedPlaymaker | 33:249cf423fb18 | 191 | ImplementCollision(pad); |
AhmedPlaymaker | 37:ee47699915b8 | 192 | 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. |
AhmedPlaymaker | 37:ee47699915b8 | 193 | _b.velocity.y = 0; |
AhmedPlaymaker | 38:30e4e6191762 | 194 | _f.velocity.y = 0; |
AhmedPlaymaker | 38:30e4e6191762 | 195 | _ff.velocity.y = 0; |
AhmedPlaymaker | 38:30e4e6191762 | 196 | _fff.velocity.y = 0; |
AhmedPlaymaker | 37:ee47699915b8 | 197 | break;//change this so that it collides slower. |
AhmedPlaymaker | 37:ee47699915b8 | 198 | } |
AhmedPlaymaker | 38:30e4e6191762 | 199 | else { |
AhmedPlaymaker | 38:30e4e6191762 | 200 | _b.velocity.y = 1; |
AhmedPlaymaker | 38:30e4e6191762 | 201 | _f.velocity.y = 1; |
AhmedPlaymaker | 38:30e4e6191762 | 202 | _ff.velocity.y = 1; |
AhmedPlaymaker | 38:30e4e6191762 | 203 | _fff.velocity.y = 1; |
AhmedPlaymaker | 38:30e4e6191762 | 204 | } |
AhmedPlaymaker | 33:249cf423fb18 | 205 | } |
AhmedPlaymaker | 12:1e601b176437 | 206 | } |
AhmedPlaymaker | 27:3b4775a0d0bb | 207 | } |
AhmedPlaymaker | 29:c6358c39a70e | 208 | |
AhmedPlaymaker | 29:c6358c39a70e | 209 | int SnakevsBlock::CheckBlock(int block) { |
AhmedPlaymaker | 29:c6358c39a70e | 210 | int srn; |
AhmedPlaymaker | 29:c6358c39a70e | 211 | if((block>=0)&&(block<=18)) {srn = 0;} |
AhmedPlaymaker | 29:c6358c39a70e | 212 | if((block>=19)&&(block<=34)) {srn = 1;} |
AhmedPlaymaker | 29:c6358c39a70e | 213 | if((block>=35)&&(block<=50)) {srn = 2;} |
AhmedPlaymaker | 29:c6358c39a70e | 214 | if((block>=51)&&(block<=66)) {srn = 3;} |
AhmedPlaymaker | 29:c6358c39a70e | 215 | if((block>=67)&&(block<=83)) {srn = 4;} |
AhmedPlaymaker | 29:c6358c39a70e | 216 | return srn; |
AhmedPlaymaker | 29:c6358c39a70e | 217 | } |
AhmedPlaymaker | 27:3b4775a0d0bb | 218 | |
AhmedPlaymaker | 27:3b4775a0d0bb | 219 | void SnakevsBlock::ImplementCollision(Gamepad &pad) { |
AhmedPlaymaker | 27:3b4775a0d0bb | 220 | send=1; |
AhmedPlaymaker | 27:3b4775a0d0bb | 221 | blocknum-=1; |
AhmedPlaymaker | 27:3b4775a0d0bb | 222 | if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1; |
AhmedPlaymaker | 27:3b4775a0d0bb | 223 | length-=1; |
AhmedPlaymaker | 27:3b4775a0d0bb | 224 | pad.tone(1000.0,0.1); |
AhmedPlaymaker | 29:c6358c39a70e | 225 | wait(0.04); |
AhmedPlaymaker | 12:1e601b176437 | 226 | } |
AhmedPlaymaker | 13:9785f2404045 | 227 | } |
AhmedPlaymaker | 13:9785f2404045 | 228 | |
AhmedPlaymaker | 38:30e4e6191762 | 229 | void SnakevsBlock::CheckSnakeBlockSidesCollision(Gamepad &pad) |
AhmedPlaymaker | 13:9785f2404045 | 230 | { |
AhmedPlaymaker | 26:3495f7b0ede7 | 231 | //Obtains all required coordinates for checking block sides and snake collision. |
AhmedPlaymaker | 13:9785f2404045 | 232 | Vector2D b_pos = _b.get_pos(); |
AhmedPlaymaker | 24:1c118b071430 | 233 | snake_pos[0] = _s.get_pos(length); |
AhmedPlaymaker | 24:1c118b071430 | 234 | snake_pos[1] = _s.get_pos_before1(length); |
AhmedPlaymaker | 24:1c118b071430 | 235 | snake_pos[2] = _s.get_pos_before2(length); |
AhmedPlaymaker | 24:1c118b071430 | 236 | snake_pos[3] = _s.get_pos_before3(length); |
AhmedPlaymaker | 24:1c118b071430 | 237 | snake_pos[4] = _s.get_pos_before4(length); |
AhmedPlaymaker | 24:1c118b071430 | 238 | snake_pos[5] = _s.get_pos_before5(length); |
AhmedPlaymaker | 24:1c118b071430 | 239 | snake_pos[6] = _s.get_pos_before6(length); |
AhmedPlaymaker | 24:1c118b071430 | 240 | snake_pos[7] = _s.get_pos_before7(length); |
AhmedPlaymaker | 24:1c118b071430 | 241 | snake_pos[8] = _s.get_pos_before8(length); |
AhmedPlaymaker | 24:1c118b071430 | 242 | snake_pos[9] = _s.get_pos_before9(length); |
AhmedPlaymaker | 37:ee47699915b8 | 243 | /* |
AhmedPlaymaker | 24:1c118b071430 | 244 | snake_pos[10] = _s.get_pos_before10(length); |
AhmedPlaymaker | 24:1c118b071430 | 245 | snake_pos[11] = _s.get_pos_before11(length); |
AhmedPlaymaker | 24:1c118b071430 | 246 | snake_pos[12] = _s.get_pos_before12(length); |
AhmedPlaymaker | 24:1c118b071430 | 247 | snake_pos[13] = _s.get_pos_before13(length); |
AhmedPlaymaker | 24:1c118b071430 | 248 | snake_pos[14] = _s.get_pos_before14(length); |
AhmedPlaymaker | 37:ee47699915b8 | 249 | */ |
AhmedPlaymaker | 13:9785f2404045 | 250 | //If statements check if the snake sprite has collided with any |
AhmedPlaymaker | 13:9785f2404045 | 251 | //of the blocks' sides and then stop the snake moving in x axis |
AhmedPlaymaker | 13:9785f2404045 | 252 | |
AhmedPlaymaker | 37:ee47699915b8 | 253 | int _length; |
AhmedPlaymaker | 37:ee47699915b8 | 254 | |
AhmedPlaymaker | 37:ee47699915b8 | 255 | if(length<=10) {_length = length;} //to stop the snake length virtually at 10 when it goes past it. |
AhmedPlaymaker | 37:ee47699915b8 | 256 | else {_length = 10;} |
AhmedPlaymaker | 37:ee47699915b8 | 257 | |
AhmedPlaymaker | 37:ee47699915b8 | 258 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 259 | b[i] = 1; |
AhmedPlaymaker | 24:1c118b071430 | 260 | } |
AhmedPlaymaker | 23:592345a70329 | 261 | |
AhmedPlaymaker | 37:ee47699915b8 | 262 | for(int i=0; i<=9; i++) { //i checks for all possible collisions with the snake respective to it's length. |
AhmedPlaymaker | 36:dfdd619874ae | 263 | for(int b_y_combination=0; b_y_combination<=10; b_y_combination++) { |
AhmedPlaymaker | 26:3495f7b0ede7 | 264 | if ( |
AhmedPlaymaker | 36:dfdd619874ae | 265 | (snake_pos[i].y == b_pos.y + b_y_combination) || |
AhmedPlaymaker | 36:dfdd619874ae | 266 | (snake_pos[i].y + 1 == b_pos.y + b_y_combination) || |
AhmedPlaymaker | 36:dfdd619874ae | 267 | (snake_pos[i].y + 2 == b_pos.y + b_y_combination)) { |
AhmedPlaymaker | 36:dfdd619874ae | 268 | for(int b_x_combination=2; b_x_combination<=82; b_x_combination+=16) { |
AhmedPlaymaker | 36:dfdd619874ae | 269 | //For West side of walls |
AhmedPlaymaker | 36:dfdd619874ae | 270 | if( |
AhmedPlaymaker | 36:dfdd619874ae | 271 | ((snake_pos[i].x == b_pos.x + b_x_combination+2) || //W |
AhmedPlaymaker | 38:30e4e6191762 | 272 | (snake_pos[i].x + 1 == b_x_combination+2))&&(_d != E)&&(_length > i) //W |
AhmedPlaymaker | 36:dfdd619874ae | 273 | ) { |
AhmedPlaymaker | 36:dfdd619874ae | 274 | //code makes sure that the colliding part doesn't move in x axis. |
AhmedPlaymaker | 37:ee47699915b8 | 275 | for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++) { |
AhmedPlaymaker | 37:ee47699915b8 | 276 | if(_length == snake_beed_num + i) { |
AhmedPlaymaker | 36:dfdd619874ae | 277 | b[snake_beed_num - 1] = 0; |
AhmedPlaymaker | 36:dfdd619874ae | 278 | } |
AhmedPlaymaker | 26:3495f7b0ede7 | 279 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 280 | } |
AhmedPlaymaker | 36:dfdd619874ae | 281 | |
AhmedPlaymaker | 36:dfdd619874ae | 282 | //for East side of walls |
AhmedPlaymaker | 36:dfdd619874ae | 283 | else if ( |
AhmedPlaymaker | 36:dfdd619874ae | 284 | ((snake_pos[i].x + 1 == b_x_combination) || //E |
AhmedPlaymaker | 38:30e4e6191762 | 285 | (snake_pos[i].x + 2 == b_x_combination))&&(_d != W)&&(_length > i) //E |
AhmedPlaymaker | 36:dfdd619874ae | 286 | ) { |
AhmedPlaymaker | 36:dfdd619874ae | 287 | //code makes sure that the colliding part doesn't move in x axis. |
AhmedPlaymaker | 37:ee47699915b8 | 288 | for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++) { |
AhmedPlaymaker | 37:ee47699915b8 | 289 | if(_length == snake_beed_num + i) { |
AhmedPlaymaker | 36:dfdd619874ae | 290 | b[snake_beed_num - 1] = 0; |
AhmedPlaymaker | 36:dfdd619874ae | 291 | } |
AhmedPlaymaker | 32:3a3bdeffdf62 | 292 | } |
AhmedPlaymaker | 32:3a3bdeffdf62 | 293 | } |
AhmedPlaymaker | 26:3495f7b0ede7 | 294 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 295 | } |
AhmedPlaymaker | 24:1c118b071430 | 296 | } |
AhmedPlaymaker | 15:f4d069da093d | 297 | } |
AhmedPlaymaker | 22:ee698f66146f | 298 | } |