Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
CrossyChicken/CrossyChicken.cpp@18:6be4c3c94a3d, 2020-05-14 (annotated)
- Committer:
- el19tb
- Date:
- Thu May 14 03:53:16 2020 +0000
- Revision:
- 18:6be4c3c94a3d
- Parent:
- 17:67dbdfcdcec2
- Child:
- 19:6d9721ffc078
fixed bugs regarding car lanes, started fixing bugs regarding safety lanes
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el19tb | 2:86cef2afa648 | 1 | #include "CrossyChicken.h" |
| el19tb | 2:86cef2afa648 | 2 | #include "Menu.h" |
| el19tb | 3:648c9d5001be | 3 | #include "GraphicEngine.h" |
| el19tb | 3:648c9d5001be | 4 | #include <vector> |
| el19tb | 3:648c9d5001be | 5 | #include <stdio.h> |
| el19tb | 3:648c9d5001be | 6 | #include <cstddef> |
| el19tb | 13:cd6d2f999057 | 7 | #include "Vehicle.h" |
| el19tb | 2:86cef2afa648 | 8 | |
| el19tb | 5:6e3afee7eac3 | 9 | // start from the top (height) |
| el19tb | 2:86cef2afa648 | 10 | Gamepad gamepad; |
| el19tb | 2:86cef2afa648 | 11 | |
| el19tb | 3:648c9d5001be | 12 | //create three class: CAR LANE, SAFETY LANE, OBSTACLE LANE(water) |
| el19tb | 3:648c9d5001be | 13 | //random |
| el19tb | 3:648c9d5001be | 14 | //algoritmic way of generating lanes |
| el19tb | 3:648c9d5001be | 15 | |
| el19tb | 2:86cef2afa648 | 16 | //size of each each tile in the game |
| el19tb | 4:aae7f8d4ab78 | 17 | int grid = 4; |
| el19tb | 2:86cef2afa648 | 18 | |
| el19tb | 3:648c9d5001be | 19 | //make one object of chicken |
| el19tb | 3:648c9d5001be | 20 | //use this to move around the lcd |
| el19tb | 6:e285eaf8bdcd | 21 | Chicken chicken((84/2)-grid/2, 48-grid, grid); |
| el19tb | 3:648c9d5001be | 22 | Chicken *chickenptr= &chicken; |
| el19tb | 3:648c9d5001be | 23 | |
| el19tb | 3:648c9d5001be | 24 | //there will be multiple cars |
| el19tb | 5:6e3afee7eac3 | 25 | Car firstLane[2]; |
| el19tb | 5:6e3afee7eac3 | 26 | Car secondLane[2]; |
| el19tb | 5:6e3afee7eac3 | 27 | Car thirdLane[3]; |
| el19tb | 7:1dce07fd0867 | 28 | Car fourthLane[3]; |
| el19tb | 7:1dce07fd0867 | 29 | Car log1Lane[2]; |
| el19tb | 7:1dce07fd0867 | 30 | Car lo[2]; |
| el19tb | 7:1dce07fd0867 | 31 | Car logger[2]; |
| el19tb | 9:d210eede4a1b | 32 | Car vop[2]; |
| el19tb | 3:648c9d5001be | 33 | |
| el19tb | 8:52794de935b2 | 34 | int screenW = 84; |
| el19tb | 8:52794de935b2 | 35 | int screenH = 48; |
| el19tb | 8:52794de935b2 | 36 | |
| el19tb | 3:648c9d5001be | 37 | //class that whill show objects |
| el19tb | 3:648c9d5001be | 38 | GraphicEngine graphics(chickenptr); |
| el19tb | 2:86cef2afa648 | 39 | |
| el19tb | 8:52794de935b2 | 40 | bool frogLog = false; |
| el19tb | 8:52794de935b2 | 41 | bool frogDie = false; // whenever the frog is above safety lane in the middle |
| el19tb | 8:52794de935b2 | 42 | bool attach = false; |
| el19tb | 8:52794de935b2 | 43 | |
| el19tb | 11:cc5861abfca5 | 44 | CrossyChicken::CrossyChicken(Level *level) |
| el19tb | 11:cc5861abfca5 | 45 | { |
| el19tb | 18:6be4c3c94a3d | 46 | this->local_level = level; |
| el19tb | 18:6be4c3c94a3d | 47 | grid = 22; |
| el19tb | 11:cc5861abfca5 | 48 | } |
| el19tb | 11:cc5861abfca5 | 49 | |
| el19tb | 2:86cef2afa648 | 50 | //main function that starts the game |
| el19tb | 3:648c9d5001be | 51 | void CrossyChicken::start(){ |
| el19tb | 2:86cef2afa648 | 52 | //game setup |
| el19tb | 3:648c9d5001be | 53 | graphics.init(); |
| el19tb | 3:648c9d5001be | 54 | graphics.contrast(); |
| el19tb | 3:648c9d5001be | 55 | graphics.backLightOn(); |
| el19tb | 2:86cef2afa648 | 56 | gamepad.init(); |
| el19tb | 15:b15bf9357cd9 | 57 | row_number = 1; |
| el19tb | 15:b15bf9357cd9 | 58 | |
| el19tb | 4:aae7f8d4ab78 | 59 | |
| el19tb | 5:6e3afee7eac3 | 60 | //first lane of left racers |
| el19tb | 5:6e3afee7eac3 | 61 | firstLane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 62 | firstLane[1].setSeperation(20); |
| el19tb | 5:6e3afee7eac3 | 63 | firstLane[0].setRow(2); |
| el19tb | 5:6e3afee7eac3 | 64 | firstLane[1].setRow(2); |
| el19tb | 4:aae7f8d4ab78 | 65 | |
| el19tb | 5:6e3afee7eac3 | 66 | secondLane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 67 | secondLane[1].setSeperation(20); |
| el19tb | 5:6e3afee7eac3 | 68 | secondLane[0].setRow(3); |
| el19tb | 5:6e3afee7eac3 | 69 | secondLane[1].setRow(3); |
| el19tb | 5:6e3afee7eac3 | 70 | |
| el19tb | 7:1dce07fd0867 | 71 | thirdLane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 72 | thirdLane[1].setSeperation(20); |
| el19tb | 7:1dce07fd0867 | 73 | thirdLane[2].setSeperation(40); |
| el19tb | 5:6e3afee7eac3 | 74 | thirdLane[0].setRow(4); |
| el19tb | 5:6e3afee7eac3 | 75 | thirdLane[1].setRow(4); |
| el19tb | 5:6e3afee7eac3 | 76 | thirdLane[2].setRow(4); |
| el19tb | 7:1dce07fd0867 | 77 | |
| el19tb | 7:1dce07fd0867 | 78 | fourthLane[0].setSeperation(10); |
| el19tb | 7:1dce07fd0867 | 79 | fourthLane[1].setSeperation(30); |
| el19tb | 7:1dce07fd0867 | 80 | fourthLane[2].setSeperation(50); |
| el19tb | 7:1dce07fd0867 | 81 | fourthLane[0].setRow(5); |
| el19tb | 7:1dce07fd0867 | 82 | fourthLane[1].setRow(5); |
| el19tb | 7:1dce07fd0867 | 83 | fourthLane[2].setRow(5); |
| el19tb | 7:1dce07fd0867 | 84 | |
| el19tb | 7:1dce07fd0867 | 85 | // log lane 1 |
| el19tb | 7:1dce07fd0867 | 86 | log1Lane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 87 | log1Lane[1].setSeperation(20); |
| el19tb | 7:1dce07fd0867 | 88 | log1Lane[0].setRow(7); |
| el19tb | 7:1dce07fd0867 | 89 | log1Lane[1].setRow(7); |
| el19tb | 7:1dce07fd0867 | 90 | |
| el19tb | 7:1dce07fd0867 | 91 | // log lane 2 |
| el19tb | 7:1dce07fd0867 | 92 | lo[0].setSeperation(10); |
| el19tb | 7:1dce07fd0867 | 93 | lo[1].setSeperation(30); |
| el19tb | 7:1dce07fd0867 | 94 | lo[0].setRow(8); |
| el19tb | 7:1dce07fd0867 | 95 | lo[1].setRow(8); |
| el19tb | 7:1dce07fd0867 | 96 | |
| el19tb | 7:1dce07fd0867 | 97 | // log lane 3 |
| el19tb | 7:1dce07fd0867 | 98 | logger[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 99 | logger[1].setSeperation(20); |
| el19tb | 7:1dce07fd0867 | 100 | logger[0].setRow(9); |
| el19tb | 7:1dce07fd0867 | 101 | logger[1].setRow(9); |
| el19tb | 9:d210eede4a1b | 102 | |
| el19tb | 9:d210eede4a1b | 103 | // log lane 3 |
| el19tb | 9:d210eede4a1b | 104 | vop[0].setSeperation(0); |
| el19tb | 9:d210eede4a1b | 105 | vop[1].setSeperation(20); |
| el19tb | 9:d210eede4a1b | 106 | vop[0].setRow(10); |
| el19tb | 9:d210eede4a1b | 107 | vop[1].setRow(10); |
| el19tb | 5:6e3afee7eac3 | 108 | |
| el19tb | 2:86cef2afa648 | 109 | //keep reading and processing user input |
| el19tb | 3:648c9d5001be | 110 | while(1) { |
| el19tb | 3:648c9d5001be | 111 | graphics.clear(); |
| el19tb | 8:52794de935b2 | 112 | |
| el19tb | 8:52794de935b2 | 113 | for(int i = 0; i < 2; i++){ |
| el19tb | 5:6e3afee7eac3 | 114 | moveCar(&firstLane[i], 2, (-2)); // change x position (moving) |
| el19tb | 5:6e3afee7eac3 | 115 | } |
| el19tb | 5:6e3afee7eac3 | 116 | |
| el19tb | 5:6e3afee7eac3 | 117 | for(int x = 0; x < 2; x++){ |
| el19tb | 5:6e3afee7eac3 | 118 | moveCar(&secondLane[x], 1, 1); |
| el19tb | 5:6e3afee7eac3 | 119 | } |
| el19tb | 5:6e3afee7eac3 | 120 | |
| el19tb | 5:6e3afee7eac3 | 121 | for(int t = 0; t < 3; t++){ |
| el19tb | 5:6e3afee7eac3 | 122 | moveCar(&thirdLane[t], 2, 1); |
| el19tb | 7:1dce07fd0867 | 123 | } |
| el19tb | 7:1dce07fd0867 | 124 | |
| el19tb | 7:1dce07fd0867 | 125 | for(int c = 0; c < 3; c++){ |
| el19tb | 7:1dce07fd0867 | 126 | moveCar(&fourthLane[c], 2, 1); |
| el19tb | 7:1dce07fd0867 | 127 | } |
| el19tb | 7:1dce07fd0867 | 128 | |
| el19tb | 7:1dce07fd0867 | 129 | for(int v = 0; v < 2; v++){ |
| el19tb | 8:52794de935b2 | 130 | moveCar(&log1Lane[v], 1, 1); |
| el19tb | 7:1dce07fd0867 | 131 | } |
| el19tb | 7:1dce07fd0867 | 132 | |
| el19tb | 7:1dce07fd0867 | 133 | for(int b = 0; b < 2; b++){ |
| el19tb | 7:1dce07fd0867 | 134 | moveCar(&lo[b], 2, 1); |
| el19tb | 9:d210eede4a1b | 135 | // moveCar(&logger[b], 2, 2); |
| el19tb | 9:d210eede4a1b | 136 | moveCar(&vop[b], 1, 2); |
| el19tb | 5:6e3afee7eac3 | 137 | |
| el19tb | 5:6e3afee7eac3 | 138 | } |
| el19tb | 5:6e3afee7eac3 | 139 | |
| el19tb | 5:6e3afee7eac3 | 140 | graphics.showCar(firstLane); |
| el19tb | 5:6e3afee7eac3 | 141 | graphics.showCar(secondLane); |
| el19tb | 5:6e3afee7eac3 | 142 | graphics.showCar(thirdLane); |
| el19tb | 7:1dce07fd0867 | 143 | graphics.showCar(fourthLane); |
| el19tb | 7:1dce07fd0867 | 144 | graphics.showCar(log1Lane); |
| el19tb | 7:1dce07fd0867 | 145 | graphics.showCar(lo); |
| el19tb | 9:d210eede4a1b | 146 | graphics.showCar(logger); |
| el19tb | 9:d210eede4a1b | 147 | graphics.showCar(vop); |
| el19tb | 9:d210eede4a1b | 148 | |
| el19tb | 8:52794de935b2 | 149 | graphics.showChicken(); |
| el19tb | 8:52794de935b2 | 150 | process_input(); |
| el19tb | 8:52794de935b2 | 151 | |
| el19tb | 8:52794de935b2 | 152 | // now when the chicken is above the safety |
| el19tb | 8:52794de935b2 | 153 | // lane we need to ensure that it only can go on logs |
| el19tb | 8:52794de935b2 | 154 | if(chicken.y < screenH - grid*6){ |
| el19tb | 8:52794de935b2 | 155 | frogDie = true; // frog can die if it is in water |
| el19tb | 7:1dce07fd0867 | 156 | |
| el19tb | 8:52794de935b2 | 157 | //if it is not in a log |
| el19tb | 8:52794de935b2 | 158 | for(int x = 0; x < 2; x++){ |
| el19tb | 8:52794de935b2 | 159 | setCollision(&log1Lane[x]); |
| el19tb | 9:d210eede4a1b | 160 | setCollision(&lo[x]); |
| el19tb | 9:d210eede4a1b | 161 | setCollision(&logger[x]); |
| el19tb | 9:d210eede4a1b | 162 | setCollision(&vop[x]); |
| el19tb | 9:d210eede4a1b | 163 | |
| el19tb | 8:52794de935b2 | 164 | if(attach){ |
| el19tb | 8:52794de935b2 | 165 | frogOnLog(&log1Lane[x]); |
| el19tb | 9:d210eede4a1b | 166 | frogOnLog(&lo[x]); |
| el19tb | 9:d210eede4a1b | 167 | frogOnLog(&logger[x]); |
| el19tb | 9:d210eede4a1b | 168 | frogOnLog(&vop[x]); |
| el19tb | 9:d210eede4a1b | 169 | |
| el19tb | 8:52794de935b2 | 170 | } |
| el19tb | 8:52794de935b2 | 171 | } |
| el19tb | 8:52794de935b2 | 172 | |
| el19tb | 8:52794de935b2 | 173 | if(!attach){ |
| el19tb | 8:52794de935b2 | 174 | graphics.print(); |
| el19tb | 9:d210eede4a1b | 175 | } |
| el19tb | 6:e285eaf8bdcd | 176 | } |
| el19tb | 9:d210eede4a1b | 177 | |
| el19tb | 3:648c9d5001be | 178 | graphics.refresh(); |
| el19tb | 9:d210eede4a1b | 179 | wait_ms(70); |
| el19tb | 2:86cef2afa648 | 180 | } |
| el19tb | 2:86cef2afa648 | 181 | } |
| el19tb | 2:86cef2afa648 | 182 | |
| el19tb | 7:1dce07fd0867 | 183 | // log intersects then frog.x = log[i].speed |
| el19tb | 7:1dce07fd0867 | 184 | // frog is attached function |
| el19tb | 7:1dce07fd0867 | 185 | // detach the frog when user goes up or down |
| el19tb | 7:1dce07fd0867 | 186 | // if the frog is back to the safe zone detatch also |
| el19tb | 7:1dce07fd0867 | 187 | // whenever the frog moves detach |
| el19tb | 2:86cef2afa648 | 188 | //A moves right |
| el19tb | 2:86cef2afa648 | 189 | //X moves upward |
| el19tb | 2:86cef2afa648 | 190 | //B moves downward |
| el19tb | 2:86cef2afa648 | 191 | //Y moves left |
| el19tb | 2:86cef2afa648 | 192 | void CrossyChicken::process_input() { |
| el19tb | 3:648c9d5001be | 193 | //determine the input |
| el19tb | 8:52794de935b2 | 194 | /* make this a switch statement */ |
| el19tb | 3:648c9d5001be | 195 | if(gamepad.A_pressed()){ |
| el19tb | 3:648c9d5001be | 196 | moveChicken(1,0); |
| el19tb | 8:52794de935b2 | 197 | attach = false; |
| el19tb | 3:648c9d5001be | 198 | } else if(gamepad.X_pressed()){ |
| el19tb | 3:648c9d5001be | 199 | moveChicken(0,-1); |
| el19tb | 8:52794de935b2 | 200 | attach = false; |
| el19tb | 3:648c9d5001be | 201 | } else if(gamepad.B_pressed()){ |
| el19tb | 3:648c9d5001be | 202 | moveChicken(0,1); |
| el19tb | 8:52794de935b2 | 203 | attach = false; |
| el19tb | 3:648c9d5001be | 204 | } else if(gamepad.Y_pressed()){ |
| el19tb | 3:648c9d5001be | 205 | moveChicken(-1,0); |
| el19tb | 8:52794de935b2 | 206 | attach = false; |
| el19tb | 8:52794de935b2 | 207 | } |
| el19tb | 8:52794de935b2 | 208 | } |
| el19tb | 8:52794de935b2 | 209 | |
| el19tb | 18:6be4c3c94a3d | 210 | |
| el19tb | 18:6be4c3c94a3d | 211 | |
| el19tb | 18:6be4c3c94a3d | 212 | |
| el19tb | 18:6be4c3c94a3d | 213 | |
| el19tb | 18:6be4c3c94a3d | 214 | |
| el19tb | 18:6be4c3c94a3d | 215 | void CrossyChicken::createMultipleRoadLane() |
| el19tb | 15:b15bf9357cd9 | 216 | { |
| el19tb | 18:6be4c3c94a3d | 217 | int min = 0; // start from the top, and draw till 0 |
| el19tb | 18:6be4c3c94a3d | 218 | int max = grid_width; // draw 21 objects to the first lane |
| el19tb | 18:6be4c3c94a3d | 219 | int row = 1; |
| el19tb | 15:b15bf9357cd9 | 220 | |
| el19tb | 18:6be4c3c94a3d | 221 | for(int x = 0; x < 4; x++){ |
| el19tb | 18:6be4c3c94a3d | 222 | createRoadLane(min, max, row); |
| el19tb | 18:6be4c3c94a3d | 223 | row++; // increment the rows to the next lane |
| el19tb | 18:6be4c3c94a3d | 224 | min += 22; // fill in the vector with 22 grid units |
| el19tb | 18:6be4c3c94a3d | 225 | max += 22; |
| el19tb | 16:8a65cccd9685 | 226 | } |
| el19tb | 16:8a65cccd9685 | 227 | } |
| el19tb | 18:6be4c3c94a3d | 228 | |
| el19tb | 18:6be4c3c94a3d | 229 | // every level is going to have the same amount of blocks |
| el19tb | 18:6be4c3c94a3d | 230 | void CrossyChicken::createRoadLane(int min, int max, int row) |
| el19tb | 18:6be4c3c94a3d | 231 | { |
| el19tb | 18:6be4c3c94a3d | 232 | std::vector<Background>::size_type it; |
| el19tb | 18:6be4c3c94a3d | 233 | |
| el19tb | 18:6be4c3c94a3d | 234 | // fill the road objects |
| el19tb | 18:6be4c3c94a3d | 235 | for(int it = min; it != max; it++){ |
| el19tb | 18:6be4c3c94a3d | 236 | roads.push_back(it/row, row); // it is the x pos of the obj, row is the y pos |
| el19tb | 18:6be4c3c94a3d | 237 | } |
| el19tb | 18:6be4c3c94a3d | 238 | } |
| el19tb | 18:6be4c3c94a3d | 239 | |
| el19tb | 18:6be4c3c94a3d | 240 | void CrossyChicken::drawRoadObjects() |
| el19tb | 18:6be4c3c94a3d | 241 | { |
| el19tb | 18:6be4c3c94a3d | 242 | graphics.getRoadObjects(roads); |
| el19tb | 18:6be4c3c94a3d | 243 | } |
| el19tb | 18:6be4c3c94a3d | 244 | |
| el19tb | 18:6be4c3c94a3d | 245 | void CrossyChicken::createTwoSafetyLanes() |
| el19tb | 18:6be4c3c94a3d | 246 | { |
| el19tb | 18:6be4c3c94a3d | 247 | int min = 0; |
| el19tb | 18:6be4c3c94a3d | 248 | int max = grid_width; |
| el19tb | 18:6be4c3c94a3d | 249 | int row = 0; // first starting lane is at row 0 |
| el19tb | 18:6be4c3c94a3d | 250 | |
| el19tb | 18:6be4c3c94a3d | 251 | // there are going to be two lanes |
| el19tb | 18:6be4c3c94a3d | 252 | for(int i = 0; i < 2; i++){ |
| el19tb | 18:6be4c3c94a3d | 253 | drawSafetyLanes(min+1, max+1, row); |
| el19tb | 18:6be4c3c94a3d | 254 | } |
| el19tb | 18:6be4c3c94a3d | 255 | } |
| el19tb | 18:6be4c3c94a3d | 256 | |
| el19tb | 16:8a65cccd9685 | 257 | // we are going to have two safety lanes |
| el19tb | 18:6be4c3c94a3d | 258 | void CrossyChicken::drawSafetyLanes(int min, int max, int row) |
| el19tb | 16:8a65cccd9685 | 259 | { |
| el19tb | 16:8a65cccd9685 | 260 | int state = 1; |
| el19tb | 18:6be4c3c94a3d | 261 | SafetyLane lane; |
| el19tb | 16:8a65cccd9685 | 262 | |
| el19tb | 18:6be4c3c94a3d | 263 | for(int z = 0; z < 22; z++){ |
| el19tb | 16:8a65cccd9685 | 264 | switch(state) |
| el19tb | 16:8a65cccd9685 | 265 | { |
| el19tb | 16:8a65cccd9685 | 266 | case 1: |
| el19tb | 18:6be4c3c94a3d | 267 | safety_lane.push_back(std::tr1::make_shared<Background>(z, row)); // safety lane so row 0 |
| el19tb | 18:6be4c3c94a3d | 268 | safety_lane.push_back(std::tr1::make_shared<SafetyLane>('K')); // safety lane so row 0 |
| el19tb | 16:8a65cccd9685 | 269 | state++; |
| el19tb | 16:8a65cccd9685 | 270 | break; |
| el19tb | 16:8a65cccd9685 | 271 | case 2: |
| el19tb | 16:8a65cccd9685 | 272 | graphics.drawSecondSafetyBackground(x, 0); // safety lane so row 0 |
| el19tb | 16:8a65cccd9685 | 273 | graphics.drawSecondSafetyBackground(x, 6); // middle lane so row 6 |
| el19tb | 16:8a65cccd9685 | 274 | state -= 1; // back to inital state |
| el19tb | 16:8a65cccd9685 | 275 | break; |
| el19tb | 16:8a65cccd9685 | 276 | } |
| el19tb | 16:8a65cccd9685 | 277 | } |
| el19tb | 16:8a65cccd9685 | 278 | } |
| el19tb | 15:b15bf9357cd9 | 279 | |
| el19tb | 17:67dbdfcdcec2 | 280 | // the water sprites are going to 'move' to make it realistic |
| el19tb | 18:6be4c3c94a3d | 281 | void CrossyChicken::createWater(int row, float speed){ |
| el19tb | 17:67dbdfcdcec2 | 282 | int state = 1; |
| el19tb | 17:67dbdfcdcec2 | 283 | |
| el19tb | 17:67dbdfcdcec2 | 284 | for(it = 0; it != 21; it++) |
| el19tb | 17:67dbdfcdcec2 | 285 | { |
| el19tb | 17:67dbdfcdcec2 | 286 | // fsm for drawing safety lane background |
| el19tb | 17:67dbdfcdcec2 | 287 | switch(state) |
| el19tb | 17:67dbdfcdcec2 | 288 | { |
| el19tb | 17:67dbdfcdcec2 | 289 | case 1: // initial state |
| el19tb | 18:6be4c3c94a3d | 290 | row_water_one[it].push_back('F', row, speed, it); // first type of water |
| el19tb | 17:67dbdfcdcec2 | 291 | state++; // transition to next state |
| el19tb | 17:67dbdfcdcec2 | 292 | break; |
| el19tb | 17:67dbdfcdcec2 | 293 | case 2: |
| el19tb | 18:6be4c3c94a3d | 294 | row_water_one[it].push_back('S', row, speed, it); // second type of water |
| el19tb | 17:67dbdfcdcec2 | 295 | state++; // transition to state 3 |
| el19tb | 18:6be4c3c94a3d | 296 | break; |
| el19tb | 17:67dbdfcdcec2 | 297 | case 3: |
| el19tb | 18:6be4c3c94a3d | 298 | row_water_one[it].push_back('T', row, speed, it); // third type of water |
| el19tb | 17:67dbdfcdcec2 | 299 | state = 1; // back to start state |
| el19tb | 17:67dbdfcdcec2 | 300 | break; |
| el19tb | 17:67dbdfcdcec2 | 301 | } |
| el19tb | 17:67dbdfcdcec2 | 302 | } |
| el19tb | 17:67dbdfcdcec2 | 303 | } |
| el19tb | 17:67dbdfcdcec2 | 304 | |
| el19tb | 18:6be4c3c94a3d | 305 | void CrossyChicken::moveWater(Water *water, int dir) |
| el19tb | 16:8a65cccd9685 | 306 | { |
| el19tb | 18:6be4c3c94a3d | 307 | switch(dir) |
| el19tb | 18:6be4c3c94a3d | 308 | case 1: |
| el19tb | 17:67dbdfcdcec2 | 309 | for(it = 0; it != row_water_one.size(); it++){ |
| el19tb | 18:6be4c3c94a3d | 310 | |
| el19tb | 17:67dbdfcdcec2 | 311 | } |
| el19tb | 17:67dbdfcdcec2 | 312 | } |
| el19tb | 16:8a65cccd9685 | 313 | |
| el19tb | 17:67dbdfcdcec2 | 314 | // if sprite goes out of bound then show again |
| el19tb | 17:67dbdfcdcec2 | 315 | void CrossyChicken::loopWater(Water *sprite) |
| el19tb | 17:67dbdfcdcec2 | 316 | { |
| el19tb | 17:67dbdfcdcec2 | 317 | // check if car goes out of bounds |
| el19tb | 17:67dbdfcdcec2 | 318 | if(sprite->x > 84+grid){ |
| el19tb | 17:67dbdfcdcec2 | 319 | sprite->x = -4; |
| el19tb | 17:67dbdfcdcec2 | 320 | |
| el19tb | 17:67dbdfcdcec2 | 321 | } else if(sprite->x < -4){ |
| el19tb | 17:67dbdfcdcec2 | 322 | sprite->x = 84 + grid; |
| el19tb | 17:67dbdfcdcec2 | 323 | } |
| el19tb | 15:b15bf9357cd9 | 324 | } |
| el19tb | 15:b15bf9357cd9 | 325 | |
| el19tb | 8:52794de935b2 | 326 | // make the frog move same speed as log |
| el19tb | 8:52794de935b2 | 327 | // if the frog moves then detach |
| el19tb | 8:52794de935b2 | 328 | void CrossyChicken::frogOnLog(Car *log) { |
| el19tb | 9:d210eede4a1b | 329 | if(log->seperation != 0){ |
| el19tb | 9:d210eede4a1b | 330 | chicken.x += 1.0; |
| el19tb | 9:d210eede4a1b | 331 | } |
| el19tb | 2:86cef2afa648 | 332 | } |
| el19tb | 2:86cef2afa648 | 333 | |
| el19tb | 3:648c9d5001be | 334 | //moves the chicken around the grid |
| el19tb | 3:648c9d5001be | 335 | void CrossyChicken::moveChicken(int xWay, int yWay){ |
| el19tb | 3:648c9d5001be | 336 | //increment the left side of the chicken by a value of the grid size |
| el19tb | 4:aae7f8d4ab78 | 337 | chicken.x += xWay * 4; |
| el19tb | 3:648c9d5001be | 338 | |
| el19tb | 3:648c9d5001be | 339 | //increment the top side by a value of grid sizw |
| el19tb | 4:aae7f8d4ab78 | 340 | chicken.y += yWay * 4; |
| el19tb | 3:648c9d5001be | 341 | |
| el19tb | 6:e285eaf8bdcd | 342 | chicken.left_side = chicken.x; |
| el19tb | 6:e285eaf8bdcd | 343 | chicken.right_side = grid + chicken.x; |
| el19tb | 6:e285eaf8bdcd | 344 | chicken.up = chicken.y; |
| el19tb | 6:e285eaf8bdcd | 345 | chicken.down = grid + chicken.y; |
| el19tb | 8:52794de935b2 | 346 | |
| el19tb | 3:648c9d5001be | 347 | //display the new state of the chicken |
| el19tb | 9:d210eede4a1b | 348 | //graphics.showChicken(); |
| el19tb | 3:648c9d5001be | 349 | |
| el19tb | 8:52794de935b2 | 350 | //wait_ms(30); |
| el19tb | 2:86cef2afa648 | 351 | } |
| el19tb | 2:86cef2afa648 | 352 | |
| el19tb | 5:6e3afee7eac3 | 353 | void CrossyChicken::moveCar(Car *car, int dir, int x) { |
| el19tb | 5:6e3afee7eac3 | 354 | car->speedMedium(dir, x); |
| el19tb | 4:aae7f8d4ab78 | 355 | |
| el19tb | 8:52794de935b2 | 356 | // check if car goes out of bounds |
| el19tb | 5:6e3afee7eac3 | 357 | if(car->vehicle.x > 84+grid){ |
| el19tb | 5:6e3afee7eac3 | 358 | car->vehicle.x = -4; |
| el19tb | 3:648c9d5001be | 359 | |
| el19tb | 5:6e3afee7eac3 | 360 | } else if(car->vehicle.x < -4){ |
| el19tb | 5:6e3afee7eac3 | 361 | car->vehicle.x = 84 + grid; |
| el19tb | 5:6e3afee7eac3 | 362 | } |
| el19tb | 5:6e3afee7eac3 | 363 | } |
| el19tb | 5:6e3afee7eac3 | 364 | |
| el19tb | 5:6e3afee7eac3 | 365 | // debug |
| el19tb | 8:52794de935b2 | 366 | void CrossyChicken::setCollision(Car *car){ |
| el19tb | 4:aae7f8d4ab78 | 367 | |
| el19tb | 5:6e3afee7eac3 | 368 | float other_bottom = car->vehicle.height + car->vehicle.y; |
| el19tb | 6:e285eaf8bdcd | 369 | |
| el19tb | 6:e285eaf8bdcd | 370 | |
| el19tb | 5:6e3afee7eac3 | 371 | if(!(chicken.up >= other_bottom || |
| el19tb | 5:6e3afee7eac3 | 372 | (chicken.right_side <= car->vehicle.x) || |
| el19tb | 5:6e3afee7eac3 | 373 | (chicken.down <= car->vehicle.y) || |
| el19tb | 5:6e3afee7eac3 | 374 | chicken.left_side >= (car->vehicle.width + car->vehicle.x))){ |
| el19tb | 5:6e3afee7eac3 | 375 | graphics.printTest(); |
| el19tb | 8:52794de935b2 | 376 | |
| el19tb | 8:52794de935b2 | 377 | if(chicken.y < screenH - grid*6){ |
| el19tb | 8:52794de935b2 | 378 | attach = true; |
| el19tb | 8:52794de935b2 | 379 | } else { |
| el19tb | 8:52794de935b2 | 380 | attach = false; |
| el19tb | 8:52794de935b2 | 381 | } |
| el19tb | 8:52794de935b2 | 382 | } |
| el19tb | 8:52794de935b2 | 383 | } |
| el19tb | 8:52794de935b2 | 384 | |
| el19tb | 8:52794de935b2 | 385 | bool CrossyChicken::returnCollision(Car* log){ |
| el19tb | 8:52794de935b2 | 386 | |
| el19tb | 8:52794de935b2 | 387 | float ob= log->vehicle.height + log->vehicle.y; |
| el19tb | 8:52794de935b2 | 388 | |
| el19tb | 8:52794de935b2 | 389 | if(!(chicken.up >= ob|| |
| el19tb | 8:52794de935b2 | 390 | (chicken.right_side <= log->vehicle.x) || |
| el19tb | 8:52794de935b2 | 391 | (chicken.down <= log->vehicle.y) || |
| el19tb | 8:52794de935b2 | 392 | chicken.left_side >= (log->vehicle.width + log->vehicle.x))){ |
| el19tb | 8:52794de935b2 | 393 | return true; |
| el19tb | 5:6e3afee7eac3 | 394 | } |
| el19tb | 5:6e3afee7eac3 | 395 | } |