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
Frogger/Frogger.cpp@29:2151cd327a36, 2020-05-16 (annotated)
- Committer:
- el19tb
- Date:
- Sat May 16 00:33:51 2020 +0000
- Revision:
- 29:2151cd327a36
- Child:
- 30:a3ed33350320
- Child:
- 34:501474a4dc59
changed the name of the game, cleaned out files, made the level object and graphic objects be made in the int main() and passed accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el19tb | 29:2151cd327a36 | 1 | #include "Frogger |
el19tb | 29:2151cd327a36 | 2 | #include "Menu.h" |
el19tb | 29:2151cd327a36 | 3 | #include "GraphicEngine.h" |
el19tb | 29:2151cd327a36 | 4 | #include <vector> |
el19tb | 29:2151cd327a36 | 5 | #include <stdio.h> |
el19tb | 29:2151cd327a36 | 6 | #include <cstddef> |
el19tb | 29:2151cd327a36 | 7 | |
el19tb | 29:2151cd327a36 | 8 | //there will be multiple cars |
el19tb | 29:2151cd327a36 | 9 | Car firstLane[2]; |
el19tb | 29:2151cd327a36 | 10 | Car secondLane[2]; |
el19tb | 29:2151cd327a36 | 11 | Car thirdLane[3]; |
el19tb | 29:2151cd327a36 | 12 | Car fourthLane[3]; |
el19tb | 29:2151cd327a36 | 13 | Car log1Lane[2]; |
el19tb | 29:2151cd327a36 | 14 | Car lo[2]; |
el19tb | 29:2151cd327a36 | 15 | Car logger[2]; |
el19tb | 29:2151cd327a36 | 16 | Car vop[2]; |
el19tb | 29:2151cd327a36 | 17 | |
el19tb | 29:2151cd327a36 | 18 | |
el19tb | 29:2151cd327a36 | 19 | bool frogLog = false; |
el19tb | 29:2151cd327a36 | 20 | bool frogDie = false; // whenever the frog is above safety lane in the middle |
el19tb | 29:2151cd327a36 | 21 | bool attach = false; |
el19tb | 29:2151cd327a36 | 22 | |
el19tb | 29:2151cd327a36 | 23 | Frogger::Frogger(GraphicEngine *engine, Level *level, Frog *frog) |
el19tb | 29:2151cd327a36 | 24 | { |
el19tb | 29:2151cd327a36 | 25 | this->graphics = engine; // get the renderer in place |
el19tb | 29:2151cd327a36 | 26 | this->level = level; // one level object per engine |
el19tb | 29:2151cd327a36 | 27 | this->frog = frog; // one frog object per run |
el19tb | 29:2151cd327a36 | 28 | |
el19tb | 29:2151cd327a36 | 29 | initializeParams(); // initialize the software parameters |
el19tb | 29:2151cd327a36 | 30 | initializeEmbeddedSystem(); // initialize the hardware paramaters |
el19tb | 29:2151cd327a36 | 31 | } |
el19tb | 29:2151cd327a36 | 32 | |
el19tb | 29:2151cd327a36 | 33 | void Frogger::initializeParams() |
el19tb | 29:2151cd327a36 | 34 | { |
el19tb | 29:2151cd327a36 | 35 | // screen sizes |
el19tb | 29:2151cd327a36 | 36 | screenW = 84; // width |
el19tb | 29:2151cd327a36 | 37 | screenH = 48; // height |
el19tb | 29:2151cd327a36 | 38 | |
el19tb | 29:2151cd327a36 | 39 | // grid values |
el19tb | 29:2151cd327a36 | 40 | grid = 4; // size of game grid system |
el19tb | 29:2151cd327a36 | 41 | grid_width = 22; // size of screen width in grid units |
el19tb | 29:2151cd327a36 | 42 | } |
el19tb | 29:2151cd327a36 | 43 | |
el19tb | 29:2151cd327a36 | 44 | void Frogger::initializeEmbeddedSystem() |
el19tb | 29:2151cd327a36 | 45 | { |
el19tb | 29:2151cd327a36 | 46 | //game setup |
el19tb | 29:2151cd327a36 | 47 | graphics.init(); // initialize the LCD object |
el19tb | 29:2151cd327a36 | 48 | graphics.contrast(); // set the contrast to 0.4 |
el19tb | 29:2151cd327a36 | 49 | graphics.backLightOn(); // turn on the backlight |
el19tb | 29:2151cd327a36 | 50 | gamepad.init(); // initialize the actual game pad |
el19tb | 29:2151cd327a36 | 51 | } |
el19tb | 29:2151cd327a36 | 52 | |
el19tb | 29:2151cd327a36 | 53 | //main function that starts the game |
el19tb | 29:2151cd327a36 | 54 | void CrossyChicken::start(){ |
el19tb | 29:2151cd327a36 | 55 | |
el19tb | 29:2151cd327a36 | 56 | row_number = 1; |
el19tb | 29:2151cd327a36 | 57 | |
el19tb | 29:2151cd327a36 | 58 | //keep reading and processing user input |
el19tb | 29:2151cd327a36 | 59 | while(1) { |
el19tb | 29:2151cd327a36 | 60 | graphics.clear(); |
el19tb | 29:2151cd327a36 | 61 | |
el19tb | 29:2151cd327a36 | 62 | for(int i = 0; i < 2; i++){ |
el19tb | 29:2151cd327a36 | 63 | moveCar(&firstLane[i], 2, (-2)); // change x position (moving) |
el19tb | 29:2151cd327a36 | 64 | } |
el19tb | 29:2151cd327a36 | 65 | |
el19tb | 29:2151cd327a36 | 66 | for(int x = 0; x < 2; x++){ |
el19tb | 29:2151cd327a36 | 67 | moveCar(&secondLane[x], 1, 1); |
el19tb | 29:2151cd327a36 | 68 | } |
el19tb | 29:2151cd327a36 | 69 | |
el19tb | 29:2151cd327a36 | 70 | for(int t = 0; t < 3; t++){ |
el19tb | 29:2151cd327a36 | 71 | moveCar(&thirdLane[t], 2, 1); |
el19tb | 29:2151cd327a36 | 72 | } |
el19tb | 29:2151cd327a36 | 73 | |
el19tb | 29:2151cd327a36 | 74 | for(int c = 0; c < 3; c++){ |
el19tb | 29:2151cd327a36 | 75 | moveCar(&fourthLane[c], 2, 1); |
el19tb | 29:2151cd327a36 | 76 | } |
el19tb | 29:2151cd327a36 | 77 | |
el19tb | 29:2151cd327a36 | 78 | for(int v = 0; v < 2; v++){ |
el19tb | 29:2151cd327a36 | 79 | moveCar(&log1Lane[v], 1, 1); |
el19tb | 29:2151cd327a36 | 80 | } |
el19tb | 29:2151cd327a36 | 81 | |
el19tb | 29:2151cd327a36 | 82 | for(int b = 0; b < 2; b++){ |
el19tb | 29:2151cd327a36 | 83 | moveCar(&lo[b], 2, 1); |
el19tb | 29:2151cd327a36 | 84 | // moveCar(&logger[b], 2, 2); |
el19tb | 29:2151cd327a36 | 85 | moveCar(&vop[b], 1, 2); |
el19tb | 29:2151cd327a36 | 86 | |
el19tb | 29:2151cd327a36 | 87 | } |
el19tb | 29:2151cd327a36 | 88 | |
el19tb | 29:2151cd327a36 | 89 | graphics.showCar(firstLane); |
el19tb | 29:2151cd327a36 | 90 | graphics.showCar(secondLane); |
el19tb | 29:2151cd327a36 | 91 | graphics.showCar(thirdLane); |
el19tb | 29:2151cd327a36 | 92 | graphics.showCar(fourthLane); |
el19tb | 29:2151cd327a36 | 93 | graphics.showCar(log1Lane); |
el19tb | 29:2151cd327a36 | 94 | graphics.showCar(lo); |
el19tb | 29:2151cd327a36 | 95 | graphics.showCar(logger); |
el19tb | 29:2151cd327a36 | 96 | graphics.showCar(vop); |
el19tb | 29:2151cd327a36 | 97 | |
el19tb | 29:2151cd327a36 | 98 | graphics.showChicken(); |
el19tb | 29:2151cd327a36 | 99 | process_input(); |
el19tb | 29:2151cd327a36 | 100 | |
el19tb | 29:2151cd327a36 | 101 | // now when the chicken is above the safety |
el19tb | 29:2151cd327a36 | 102 | // lane we need to ensure that it only can go on logs |
el19tb | 29:2151cd327a36 | 103 | if(chicken.y < screenH - grid*6){ |
el19tb | 29:2151cd327a36 | 104 | frogDie = true; // frog can die if it is in water |
el19tb | 29:2151cd327a36 | 105 | |
el19tb | 29:2151cd327a36 | 106 | //if it is not in a log |
el19tb | 29:2151cd327a36 | 107 | for(int x = 0; x < 2; x++){ |
el19tb | 29:2151cd327a36 | 108 | setCollision(&log1Lane[x]); |
el19tb | 29:2151cd327a36 | 109 | setCollision(&lo[x]); |
el19tb | 29:2151cd327a36 | 110 | setCollision(&logger[x]); |
el19tb | 29:2151cd327a36 | 111 | setCollision(&vop[x]); |
el19tb | 29:2151cd327a36 | 112 | |
el19tb | 29:2151cd327a36 | 113 | if(attach){ |
el19tb | 29:2151cd327a36 | 114 | frogOnLog(&log1Lane[x]); |
el19tb | 29:2151cd327a36 | 115 | frogOnLog(&lo[x]); |
el19tb | 29:2151cd327a36 | 116 | frogOnLog(&logger[x]); |
el19tb | 29:2151cd327a36 | 117 | frogOnLog(&vop[x]); |
el19tb | 29:2151cd327a36 | 118 | |
el19tb | 29:2151cd327a36 | 119 | } |
el19tb | 29:2151cd327a36 | 120 | } |
el19tb | 29:2151cd327a36 | 121 | |
el19tb | 29:2151cd327a36 | 122 | if(!attach){ |
el19tb | 29:2151cd327a36 | 123 | graphics.print(); |
el19tb | 29:2151cd327a36 | 124 | } |
el19tb | 29:2151cd327a36 | 125 | } |
el19tb | 29:2151cd327a36 | 126 | |
el19tb | 29:2151cd327a36 | 127 | graphics.refresh(); |
el19tb | 29:2151cd327a36 | 128 | wait_ms(70); |
el19tb | 29:2151cd327a36 | 129 | } |
el19tb | 29:2151cd327a36 | 130 | } |
el19tb | 29:2151cd327a36 | 131 | |
el19tb | 29:2151cd327a36 | 132 | // log intersects then frog.x = log[i].speed |
el19tb | 29:2151cd327a36 | 133 | // frog is attached function |
el19tb | 29:2151cd327a36 | 134 | // detach the frog when user goes up or down |
el19tb | 29:2151cd327a36 | 135 | // if the frog is back to the safe zone detatch also |
el19tb | 29:2151cd327a36 | 136 | // whenever the frog moves detach |
el19tb | 29:2151cd327a36 | 137 | //A moves right |
el19tb | 29:2151cd327a36 | 138 | //X moves upward |
el19tb | 29:2151cd327a36 | 139 | //B moves downward |
el19tb | 29:2151cd327a36 | 140 | //Y moves left |
el19tb | 29:2151cd327a36 | 141 | void CrossyChicken::process_input() { |
el19tb | 29:2151cd327a36 | 142 | //determine the input |
el19tb | 29:2151cd327a36 | 143 | /* make this a switch statement */ |
el19tb | 29:2151cd327a36 | 144 | if(gamepad.A_pressed()){ |
el19tb | 29:2151cd327a36 | 145 | moveChicken(1,0); |
el19tb | 29:2151cd327a36 | 146 | attach = false; |
el19tb | 29:2151cd327a36 | 147 | } else if(gamepad.X_pressed()){ |
el19tb | 29:2151cd327a36 | 148 | moveChicken(0,-1); |
el19tb | 29:2151cd327a36 | 149 | attach = false; |
el19tb | 29:2151cd327a36 | 150 | } else if(gamepad.B_pressed()){ |
el19tb | 29:2151cd327a36 | 151 | moveChicken(0,1); |
el19tb | 29:2151cd327a36 | 152 | attach = false; |
el19tb | 29:2151cd327a36 | 153 | } else if(gamepad.Y_pressed()){ |
el19tb | 29:2151cd327a36 | 154 | moveChicken(-1,0); |
el19tb | 29:2151cd327a36 | 155 | attach = false; |
el19tb | 29:2151cd327a36 | 156 | } |
el19tb | 29:2151cd327a36 | 157 | } |
el19tb | 29:2151cd327a36 | 158 | |
el19tb | 29:2151cd327a36 | 159 | void CrossyChicken::createMultipleSafetyLane() |
el19tb | 29:2151cd327a36 | 160 | { |
el19tb | 29:2151cd327a36 | 161 | int min = 0; // start from the top, and draw till 0 |
el19tb | 29:2151cd327a36 | 162 | int max = grid_width; // draw 21 objects to the first lane |
el19tb | 29:2151cd327a36 | 163 | int row = 1; |
el19tb | 29:2151cd327a36 | 164 | int x_fac = 1; |
el19tb | 29:2151cd327a36 | 165 | |
el19tb | 29:2151cd327a36 | 166 | for(int x = 0; x < 2; x++){ |
el19tb | 29:2151cd327a36 | 167 | creatSafetyObject(min, max, row, x_fac); |
el19tb | 29:2151cd327a36 | 168 | row += 6; // increment the rows to the next lane |
el19tb | 29:2151cd327a36 | 169 | min += 22; // fill in the vector with 22 grid units |
el19tb | 29:2151cd327a36 | 170 | max += 22; |
el19tb | 29:2151cd327a36 | 171 | } |
el19tb | 29:2151cd327a36 | 172 | } |
el19tb | 29:2151cd327a36 | 173 | |
el19tb | 29:2151cd327a36 | 174 | void CrossyChicken::createSafetyObject(int min, int max, int row, int x_fac) |
el19tb | 29:2151cd327a36 | 175 | { |
el19tb | 29:2151cd327a36 | 176 | int state = 1; |
el19tb | 29:2151cd327a36 | 177 | |
el19tb | 29:2151cd327a36 | 178 | for(int z = 0; z < 22; z++){ |
el19tb | 29:2151cd327a36 | 179 | switch(state) |
el19tb | 29:2151cd327a36 | 180 | { |
el19tb | 29:2151cd327a36 | 181 | case 1: |
el19tb | 29:2151cd327a36 | 182 | safety_lane.push_back(z/x_fac, row, 'K'); // safety lane so row 0 |
el19tb | 29:2151cd327a36 | 183 | state++; |
el19tb | 29:2151cd327a36 | 184 | break; |
el19tb | 29:2151cd327a36 | 185 | case 2: |
el19tb | 29:2151cd327a36 | 186 | safety_lane.push_back(z/x_fac, row, 'P'); // safety lane so row 0 |
el19tb | 29:2151cd327a36 | 187 | state--; // back to inital state |
el19tb | 29:2151cd327a36 | 188 | break; |
el19tb | 29:2151cd327a36 | 189 | } |
el19tb | 29:2151cd327a36 | 190 | } |
el19tb | 29:2151cd327a36 | 191 | } |
el19tb | 29:2151cd327a36 | 192 | |
el19tb | 29:2151cd327a36 | 193 | void CrossyChicken::drawSafety() |
el19tb | 29:2151cd327a36 | 194 | { |
el19tb | 29:2151cd327a36 | 195 | graphics.drawSafetyLanes(safety_lane); |
el19tb | 29:2151cd327a36 | 196 | } |
el19tb | 29:2151cd327a36 | 197 | |
el19tb | 29:2151cd327a36 | 198 | |
el19tb | 29:2151cd327a36 | 199 | void CrossyChicken::createMultipleRoadLane() |
el19tb | 29:2151cd327a36 | 200 | { |
el19tb | 29:2151cd327a36 | 201 | int min = 0; // start from the top, and draw till 0 |
el19tb | 29:2151cd327a36 | 202 | int max = grid_width; // draw 21 objects to the first lane |
el19tb | 29:2151cd327a36 | 203 | int row = 1; |
el19tb | 29:2151cd327a36 | 204 | |
el19tb | 29:2151cd327a36 | 205 | for(int x = 0; x < 4; x++){ |
el19tb | 29:2151cd327a36 | 206 | createRoadLane(min, max, row); |
el19tb | 29:2151cd327a36 | 207 | row++; // increment the rows to the next lane |
el19tb | 29:2151cd327a36 | 208 | min += 22; // fill in the vector with 22 grid units |
el19tb | 29:2151cd327a36 | 209 | max += 22; |
el19tb | 29:2151cd327a36 | 210 | } |
el19tb | 29:2151cd327a36 | 211 | } |
el19tb | 29:2151cd327a36 | 212 | |
el19tb | 29:2151cd327a36 | 213 | // every level is going to have the same amount of blocks |
el19tb | 29:2151cd327a36 | 214 | void CrossyChicken::createRoadLane(int min, int max, int row) |
el19tb | 29:2151cd327a36 | 215 | { |
el19tb | 29:2151cd327a36 | 216 | std::vector<Background>::size_type it; |
el19tb | 29:2151cd327a36 | 217 | |
el19tb | 29:2151cd327a36 | 218 | // fill the road objects |
el19tb | 29:2151cd327a36 | 219 | for(int it = min; it != max; it++){ |
el19tb | 29:2151cd327a36 | 220 | roads.push_back(it/row, row); // it is the x pos of the obj, row is the y pos |
el19tb | 29:2151cd327a36 | 221 | } |
el19tb | 29:2151cd327a36 | 222 | } |
el19tb | 29:2151cd327a36 | 223 | |
el19tb | 29:2151cd327a36 | 224 | void CrossyChicken::drawRoadObjects() |
el19tb | 29:2151cd327a36 | 225 | { |
el19tb | 29:2151cd327a36 | 226 | graphics.getRoadObjects(roads); |
el19tb | 29:2151cd327a36 | 227 | } |
el19tb | 29:2151cd327a36 | 228 | |
el19tb | 29:2151cd327a36 | 229 | void CrossyChicken::createMultipleLanesWater() |
el19tb | 29:2151cd327a36 | 230 | { |
el19tb | 29:2151cd327a36 | 231 | int min = 0; // start from the top, and draw till 0 |
el19tb | 29:2151cd327a36 | 232 | int max = grid_width; // draw 21 objects to the first lane |
el19tb | 29:2151cd327a36 | 233 | int row = 7; |
el19tb | 29:2151cd327a36 | 234 | int x_fac = 1; |
el19tb | 29:2151cd327a36 | 235 | |
el19tb | 29:2151cd327a36 | 236 | for(int x = 0; x < 3; x++){ |
el19tb | 29:2151cd327a36 | 237 | if(x == 1){ |
el19tb | 29:2151cd327a36 | 238 | createWaterLane(min, max, row, -0.6); // second row goes left |
el19tb | 29:2151cd327a36 | 239 | } else { |
el19tb | 29:2151cd327a36 | 240 | createWaterLane(min, max, row, 0.4); // rest of the rows go right |
el19tb | 29:2151cd327a36 | 241 | } |
el19tb | 29:2151cd327a36 | 242 | |
el19tb | 29:2151cd327a36 | 243 | row++; // increment the rows to the next lane |
el19tb | 29:2151cd327a36 | 244 | min += grid_width; // fill in the vector with 22 grid units |
el19tb | 29:2151cd327a36 | 245 | max += grid_width; |
el19tb | 29:2151cd327a36 | 246 | } |
el19tb | 29:2151cd327a36 | 247 | } |
el19tb | 29:2151cd327a36 | 248 | |
el19tb | 29:2151cd327a36 | 249 | void CrossyChicken::createWaterLane(int min, int max, int row, float speed, int x_fac){ |
el19tb | 29:2151cd327a36 | 250 | int state = 1; |
el19tb | 29:2151cd327a36 | 251 | for(it = min; it < max; it++) |
el19tb | 29:2151cd327a36 | 252 | { |
el19tb | 29:2151cd327a36 | 253 | switch(state) |
el19tb | 29:2151cd327a36 | 254 | { |
el19tb | 29:2151cd327a36 | 255 | case 1: // initial state |
el19tb | 29:2151cd327a36 | 256 | water[it].push_back(it/x_fac, row, speed, 'F'); // first type of water |
el19tb | 29:2151cd327a36 | 257 | state++; // transition to next state |
el19tb | 29:2151cd327a36 | 258 | break; |
el19tb | 29:2151cd327a36 | 259 | case 2: |
el19tb | 29:2151cd327a36 | 260 | water[it].push_back(it/x_fac, row, speed, 'S'); // first type of water |
el19tb | 29:2151cd327a36 | 261 | state++; // transition to state 3 |
el19tb | 29:2151cd327a36 | 262 | break; |
el19tb | 29:2151cd327a36 | 263 | case 3: |
el19tb | 29:2151cd327a36 | 264 | water[it].push_back(it/x_fac, row, speed, 'T'); // first type of water |
el19tb | 29:2151cd327a36 | 265 | state = 1; // back to start state |
el19tb | 29:2151cd327a36 | 266 | break; |
el19tb | 29:2151cd327a36 | 267 | } |
el19tb | 29:2151cd327a36 | 268 | } |
el19tb | 29:2151cd327a36 | 269 | } |
el19tb | 29:2151cd327a36 | 270 | |
el19tb | 29:2151cd327a36 | 271 | // moves the water based on the velocity |
el19tb | 29:2151cd327a36 | 272 | void CrossyChicken::moveWater(std::vector<Water>& water_lanes) |
el19tb | 29:2151cd327a36 | 273 | { |
el19tb | 29:2151cd327a36 | 274 | for(unsigned int i = 0; i < water_lanes.size(); i++) |
el19tb | 29:2151cd327a36 | 275 | { |
el19tb | 29:2151cd327a36 | 276 | if(water_lanes(i).speed > 0){ |
el19tb | 29:2151cd327a36 | 277 | water_lanes(i).x += speed; |
el19tb | 29:2151cd327a36 | 278 | } else { |
el19tb | 29:2151cd327a36 | 279 | water_lanes(i).x -= speed; |
el19tb | 29:2151cd327a36 | 280 | } |
el19tb | 29:2151cd327a36 | 281 | } |
el19tb | 29:2151cd327a36 | 282 | |
el19tb | 29:2151cd327a36 | 283 | loopWater(water_lanes); |
el19tb | 29:2151cd327a36 | 284 | } |
el19tb | 29:2151cd327a36 | 285 | |
el19tb | 29:2151cd327a36 | 286 | |
el19tb | 29:2151cd327a36 | 287 | void CrossyChicken::loopWater(std::vector<Water>& water_lanes) |
el19tb | 29:2151cd327a36 | 288 | { |
el19tb | 29:2151cd327a36 | 289 | for(unsigned int i = 0; i < water_lanes.size(); i++) |
el19tb | 29:2151cd327a36 | 290 | { |
el19tb | 29:2151cd327a36 | 291 | if(water_lanes(i).x > 84+grid){ |
el19tb | 29:2151cd327a36 | 292 | water_lanes(i).x = -4; |
el19tb | 29:2151cd327a36 | 293 | } else if(water_lanes[i].x < -4){ |
el19tb | 29:2151cd327a36 | 294 | water_lanes(i).x = 84 + grid; |
el19tb | 29:2151cd327a36 | 295 | } |
el19tb | 29:2151cd327a36 | 296 | } |
el19tb | 29:2151cd327a36 | 297 | } |
el19tb | 29:2151cd327a36 | 298 | |
el19tb | 29:2151cd327a36 | 299 | void CrossyChicken::drawWater() |
el19tb | 29:2151cd327a36 | 300 | { |
el19tb | 29:2151cd327a36 | 301 | graphics.drawWater(water); |
el19tb | 29:2151cd327a36 | 302 | } |
el19tb | 29:2151cd327a36 | 303 | |
el19tb | 29:2151cd327a36 | 304 | void CrossyChicken::drawEndPost() |
el19tb | 29:2151cd327a36 | 305 | { |
el19tb | 29:2151cd327a36 | 306 | graphics.drawGoal(84/2)-grid/2, 48-grid*11); |
el19tb | 29:2151cd327a36 | 307 | } |
el19tb | 29:2151cd327a36 | 308 | |
el19tb | 29:2151cd327a36 | 309 | // make the frog move same speed as log |
el19tb | 29:2151cd327a36 | 310 | // if the frog moves then detach |
el19tb | 29:2151cd327a36 | 311 | void CrossyChicken::frogOnLog(Car *log) { |
el19tb | 29:2151cd327a36 | 312 | if(log->seperation != 0){ |
el19tb | 29:2151cd327a36 | 313 | chicken.x += 1.0; |
el19tb | 29:2151cd327a36 | 314 | } |
el19tb | 29:2151cd327a36 | 315 | } |
el19tb | 29:2151cd327a36 | 316 | |
el19tb | 29:2151cd327a36 | 317 | //moves the chicken around the grid |
el19tb | 29:2151cd327a36 | 318 | void CrossyChicken::moveChicken(int xWay, int yWay){ |
el19tb | 29:2151cd327a36 | 319 | //increment the left side of the chicken by a value of the grid size |
el19tb | 29:2151cd327a36 | 320 | chicken.x += xWay * 4; |
el19tb | 29:2151cd327a36 | 321 | |
el19tb | 29:2151cd327a36 | 322 | //increment the top side by a value of grid sizw |
el19tb | 29:2151cd327a36 | 323 | chicken.y += yWay * 4; |
el19tb | 29:2151cd327a36 | 324 | |
el19tb | 29:2151cd327a36 | 325 | chicken.left_side = chicken.x; |
el19tb | 29:2151cd327a36 | 326 | chicken.right_side = grid + chicken.x; |
el19tb | 29:2151cd327a36 | 327 | chicken.up = chicken.y; |
el19tb | 29:2151cd327a36 | 328 | chicken.down = grid + chicken.y; |
el19tb | 29:2151cd327a36 | 329 | |
el19tb | 29:2151cd327a36 | 330 | //display the new state of the chicken |
el19tb | 29:2151cd327a36 | 331 | //graphics.showChicken(); |
el19tb | 29:2151cd327a36 | 332 | |
el19tb | 29:2151cd327a36 | 333 | //wait_ms(30); |
el19tb | 29:2151cd327a36 | 334 | } |
el19tb | 29:2151cd327a36 | 335 | |
el19tb | 29:2151cd327a36 | 336 | void CrossyChicken::moveCar(Car *car, int dir, int x) { |
el19tb | 29:2151cd327a36 | 337 | car->speedMedium(dir, x); |
el19tb | 29:2151cd327a36 | 338 | |
el19tb | 29:2151cd327a36 | 339 | // check if car goes out of bounds |
el19tb | 29:2151cd327a36 | 340 | if(car->vehicle.x > 84+grid){ |
el19tb | 29:2151cd327a36 | 341 | car->vehicle.x = -4; |
el19tb | 29:2151cd327a36 | 342 | |
el19tb | 29:2151cd327a36 | 343 | } else if(car->vehicle.x < -4){ |
el19tb | 29:2151cd327a36 | 344 | car->vehicle.x = 84 + grid; |
el19tb | 29:2151cd327a36 | 345 | } |
el19tb | 29:2151cd327a36 | 346 | } |
el19tb | 29:2151cd327a36 | 347 | |
el19tb | 29:2151cd327a36 | 348 | // debug |
el19tb | 29:2151cd327a36 | 349 | void CrossyChicken::setCollision(Car *car){ |
el19tb | 29:2151cd327a36 | 350 | |
el19tb | 29:2151cd327a36 | 351 | float other_bottom = car->vehicle.height + car->vehicle.y; |
el19tb | 29:2151cd327a36 | 352 | |
el19tb | 29:2151cd327a36 | 353 | |
el19tb | 29:2151cd327a36 | 354 | if(!(chicken.up >= other_bottom || |
el19tb | 29:2151cd327a36 | 355 | (chicken.right_side <= car->vehicle.x) || |
el19tb | 29:2151cd327a36 | 356 | (chicken.down <= car->vehicle.y) || |
el19tb | 29:2151cd327a36 | 357 | chicken.left_side >= (car->vehicle.width + car->vehicle.x))){ |
el19tb | 29:2151cd327a36 | 358 | graphics.printTest(); |
el19tb | 29:2151cd327a36 | 359 | |
el19tb | 29:2151cd327a36 | 360 | if(chicken.y < screenH - grid*6){ |
el19tb | 29:2151cd327a36 | 361 | attach = true; |
el19tb | 29:2151cd327a36 | 362 | } else { |
el19tb | 29:2151cd327a36 | 363 | attach = false; |
el19tb | 29:2151cd327a36 | 364 | } |
el19tb | 29:2151cd327a36 | 365 | } |
el19tb | 29:2151cd327a36 | 366 | } |
el19tb | 29:2151cd327a36 | 367 | |
el19tb | 29:2151cd327a36 | 368 | bool CrossyChicken::returnCollision(Car* log){ |
el19tb | 29:2151cd327a36 | 369 | |
el19tb | 29:2151cd327a36 | 370 | float ob = log->vehicle.height + log->vehicle.y; |
el19tb | 29:2151cd327a36 | 371 | |
el19tb | 29:2151cd327a36 | 372 | if(!(chicken.up >= ob|| |
el19tb | 29:2151cd327a36 | 373 | (chicken.right_side <= log->vehicle.x) || |
el19tb | 29:2151cd327a36 | 374 | (chicken.down <= log->vehicle.y) || |
el19tb | 29:2151cd327a36 | 375 | chicken.left_side >= (log->vehicle.width + log->vehicle.x))){ |
el19tb | 29:2151cd327a36 | 376 | return true; |
el19tb | 29:2151cd327a36 | 377 | } |
el19tb | 29:2151cd327a36 | 378 | } |