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@19:6d9721ffc078, 2020-05-14 (annotated)
- Committer:
- el19tb
- Date:
- Thu May 14 05:52:50 2020 +0000
- Revision:
- 19:6d9721ffc078
- Parent:
- 18:6be4c3c94a3d
- Child:
- 20:077f845f09f2
fixed bugs for water, safety, and and car lanes, made the water mvoe, improved design
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 | 19:6d9721ffc078 | 210 | void CrossyChicken::createMultipleSafetyLane() |
| el19tb | 19:6d9721ffc078 | 211 | { |
| el19tb | 19:6d9721ffc078 | 212 | int min = 0; // start from the top, and draw till 0 |
| el19tb | 19:6d9721ffc078 | 213 | int max = grid_width; // draw 21 objects to the first lane |
| el19tb | 19:6d9721ffc078 | 214 | int row = 1; |
| el19tb | 19:6d9721ffc078 | 215 | int x_fac = 1; |
| el19tb | 19:6d9721ffc078 | 216 | |
| el19tb | 19:6d9721ffc078 | 217 | for(int x = 0; x < 2; x++){ |
| el19tb | 19:6d9721ffc078 | 218 | creatSafetyObject(min, max, row, x_fac); |
| el19tb | 19:6d9721ffc078 | 219 | row += 6; // increment the rows to the next lane |
| el19tb | 19:6d9721ffc078 | 220 | min += 22; // fill in the vector with 22 grid units |
| el19tb | 19:6d9721ffc078 | 221 | max += 22; |
| el19tb | 19:6d9721ffc078 | 222 | } |
| el19tb | 19:6d9721ffc078 | 223 | } |
| el19tb | 18:6be4c3c94a3d | 224 | |
| el19tb | 19:6d9721ffc078 | 225 | void CrossyChicken::createSafetyObject(int min, int max, int row, int x_fac) |
| el19tb | 19:6d9721ffc078 | 226 | { |
| el19tb | 19:6d9721ffc078 | 227 | int state = 1; |
| el19tb | 19:6d9721ffc078 | 228 | |
| el19tb | 19:6d9721ffc078 | 229 | for(int z = 0; z < 22; z++){ |
| el19tb | 19:6d9721ffc078 | 230 | switch(state) |
| el19tb | 19:6d9721ffc078 | 231 | { |
| el19tb | 19:6d9721ffc078 | 232 | case 1: |
| el19tb | 19:6d9721ffc078 | 233 | safety_lane.push_back(z/x_fac, row, 'K'); // safety lane so row 0 |
| el19tb | 19:6d9721ffc078 | 234 | state++; |
| el19tb | 19:6d9721ffc078 | 235 | break; |
| el19tb | 19:6d9721ffc078 | 236 | case 2: |
| el19tb | 19:6d9721ffc078 | 237 | safety_lane.push_back(z/x_fac, row, 'P'); // safety lane so row 0 |
| el19tb | 19:6d9721ffc078 | 238 | state--; // back to inital state |
| el19tb | 19:6d9721ffc078 | 239 | break; |
| el19tb | 19:6d9721ffc078 | 240 | } |
| el19tb | 19:6d9721ffc078 | 241 | } |
| el19tb | 19:6d9721ffc078 | 242 | } |
| el19tb | 18:6be4c3c94a3d | 243 | |
| el19tb | 19:6d9721ffc078 | 244 | void CrossyChicken::drawSafety() |
| el19tb | 19:6d9721ffc078 | 245 | { |
| el19tb | 19:6d9721ffc078 | 246 | graphics.drawSafetyLanes(safety_lane); |
| el19tb | 19:6d9721ffc078 | 247 | } |
| el19tb | 18:6be4c3c94a3d | 248 | |
| el19tb | 18:6be4c3c94a3d | 249 | |
| el19tb | 18:6be4c3c94a3d | 250 | void CrossyChicken::createMultipleRoadLane() |
| el19tb | 15:b15bf9357cd9 | 251 | { |
| el19tb | 18:6be4c3c94a3d | 252 | int min = 0; // start from the top, and draw till 0 |
| el19tb | 18:6be4c3c94a3d | 253 | int max = grid_width; // draw 21 objects to the first lane |
| el19tb | 18:6be4c3c94a3d | 254 | int row = 1; |
| el19tb | 15:b15bf9357cd9 | 255 | |
| el19tb | 18:6be4c3c94a3d | 256 | for(int x = 0; x < 4; x++){ |
| el19tb | 18:6be4c3c94a3d | 257 | createRoadLane(min, max, row); |
| el19tb | 18:6be4c3c94a3d | 258 | row++; // increment the rows to the next lane |
| el19tb | 18:6be4c3c94a3d | 259 | min += 22; // fill in the vector with 22 grid units |
| el19tb | 18:6be4c3c94a3d | 260 | max += 22; |
| el19tb | 16:8a65cccd9685 | 261 | } |
| el19tb | 16:8a65cccd9685 | 262 | } |
| el19tb | 18:6be4c3c94a3d | 263 | |
| el19tb | 18:6be4c3c94a3d | 264 | // every level is going to have the same amount of blocks |
| el19tb | 18:6be4c3c94a3d | 265 | void CrossyChicken::createRoadLane(int min, int max, int row) |
| el19tb | 18:6be4c3c94a3d | 266 | { |
| el19tb | 18:6be4c3c94a3d | 267 | std::vector<Background>::size_type it; |
| el19tb | 18:6be4c3c94a3d | 268 | |
| el19tb | 18:6be4c3c94a3d | 269 | // fill the road objects |
| el19tb | 18:6be4c3c94a3d | 270 | for(int it = min; it != max; it++){ |
| el19tb | 18:6be4c3c94a3d | 271 | roads.push_back(it/row, row); // it is the x pos of the obj, row is the y pos |
| el19tb | 18:6be4c3c94a3d | 272 | } |
| el19tb | 18:6be4c3c94a3d | 273 | } |
| el19tb | 18:6be4c3c94a3d | 274 | |
| el19tb | 18:6be4c3c94a3d | 275 | void CrossyChicken::drawRoadObjects() |
| el19tb | 18:6be4c3c94a3d | 276 | { |
| el19tb | 18:6be4c3c94a3d | 277 | graphics.getRoadObjects(roads); |
| el19tb | 18:6be4c3c94a3d | 278 | } |
| el19tb | 18:6be4c3c94a3d | 279 | |
| el19tb | 19:6d9721ffc078 | 280 | void CrossyChicken::createMultipleLanesWater() |
| el19tb | 19:6d9721ffc078 | 281 | { |
| el19tb | 19:6d9721ffc078 | 282 | int min = 0; // start from the top, and draw till 0 |
| el19tb | 19:6d9721ffc078 | 283 | int max = grid_width; // draw 21 objects to the first lane |
| el19tb | 19:6d9721ffc078 | 284 | int row = 7; |
| el19tb | 19:6d9721ffc078 | 285 | int x_fac = 1; |
| el19tb | 19:6d9721ffc078 | 286 | |
| el19tb | 19:6d9721ffc078 | 287 | for(int x = 0; x < 3; x++){ |
| el19tb | 19:6d9721ffc078 | 288 | if(x == 1){ |
| el19tb | 19:6d9721ffc078 | 289 | createWaterLane(min, max, row, -0.6); // second row goes left |
| el19tb | 19:6d9721ffc078 | 290 | } else { |
| el19tb | 19:6d9721ffc078 | 291 | createWaterLane(min, max, row, 0.4); // rest of the rows go right |
| el19tb | 19:6d9721ffc078 | 292 | } |
| el19tb | 19:6d9721ffc078 | 293 | |
| el19tb | 19:6d9721ffc078 | 294 | row++; // increment the rows to the next lane |
| el19tb | 19:6d9721ffc078 | 295 | min += grid_width; // fill in the vector with 22 grid units |
| el19tb | 19:6d9721ffc078 | 296 | max += grid_width; |
| el19tb | 19:6d9721ffc078 | 297 | } |
| el19tb | 19:6d9721ffc078 | 298 | } |
| el19tb | 19:6d9721ffc078 | 299 | |
| el19tb | 19:6d9721ffc078 | 300 | void CrossyChicken::createWaterLane(int min, int max, int row, float speed, int x_fac){ |
| el19tb | 19:6d9721ffc078 | 301 | int state = 1; |
| el19tb | 19:6d9721ffc078 | 302 | for(it = min; it < max; it++) |
| el19tb | 19:6d9721ffc078 | 303 | { |
| el19tb | 19:6d9721ffc078 | 304 | switch(state) |
| el19tb | 19:6d9721ffc078 | 305 | { |
| el19tb | 19:6d9721ffc078 | 306 | case 1: // initial state |
| el19tb | 19:6d9721ffc078 | 307 | water[it].push_back(it/x_fac, row, speed, 'F'); // first type of water |
| el19tb | 19:6d9721ffc078 | 308 | state++; // transition to next state |
| el19tb | 19:6d9721ffc078 | 309 | break; |
| el19tb | 19:6d9721ffc078 | 310 | case 2: |
| el19tb | 19:6d9721ffc078 | 311 | water[it].push_back(it/x_fac, row, speed, 'S'); // first type of water |
| el19tb | 19:6d9721ffc078 | 312 | state++; // transition to state 3 |
| el19tb | 19:6d9721ffc078 | 313 | break; |
| el19tb | 19:6d9721ffc078 | 314 | case 3: |
| el19tb | 19:6d9721ffc078 | 315 | water[it].push_back(it/x_fac, row, speed, 'T'); // first type of water |
| el19tb | 19:6d9721ffc078 | 316 | state = 1; // back to start state |
| el19tb | 19:6d9721ffc078 | 317 | break; |
| el19tb | 19:6d9721ffc078 | 318 | } |
| el19tb | 19:6d9721ffc078 | 319 | } |
| el19tb | 19:6d9721ffc078 | 320 | } |
| el19tb | 19:6d9721ffc078 | 321 | |
| el19tb | 19:6d9721ffc078 | 322 | // moves the water based on the velocity |
| el19tb | 19:6d9721ffc078 | 323 | void CrossyChicken::moveWater(std::vector<Water>& water_lanes) |
| el19tb | 18:6be4c3c94a3d | 324 | { |
| el19tb | 19:6d9721ffc078 | 325 | for(unsigned int i = 0; i < water_lanes.size(); i++) |
| el19tb | 19:6d9721ffc078 | 326 | { |
| el19tb | 19:6d9721ffc078 | 327 | if(water_lanes(i).speed > 0){ |
| el19tb | 19:6d9721ffc078 | 328 | water_lanes(i).x += speed; |
| el19tb | 19:6d9721ffc078 | 329 | } else { |
| el19tb | 19:6d9721ffc078 | 330 | water_lanes(i).x -= speed; |
| el19tb | 19:6d9721ffc078 | 331 | } |
| el19tb | 19:6d9721ffc078 | 332 | } |
| el19tb | 18:6be4c3c94a3d | 333 | |
| el19tb | 19:6d9721ffc078 | 334 | loopWater(water_lanes); |
| el19tb | 19:6d9721ffc078 | 335 | } |
| el19tb | 19:6d9721ffc078 | 336 | |
| el19tb | 19:6d9721ffc078 | 337 | |
| el19tb | 19:6d9721ffc078 | 338 | void CrossyChicken::loopWater(std::vector<Water>& water_lanes) |
| el19tb | 19:6d9721ffc078 | 339 | { |
| el19tb | 19:6d9721ffc078 | 340 | for(unsigned int i = 0; i < water_lanes.size(); i++) |
| el19tb | 19:6d9721ffc078 | 341 | { |
| el19tb | 19:6d9721ffc078 | 342 | if(water_lanes(i).x > 84+grid){ |
| el19tb | 19:6d9721ffc078 | 343 | water_lanes(i).x = -4; |
| el19tb | 19:6d9721ffc078 | 344 | } else if(water_lanes[i].x < -4){ |
| el19tb | 19:6d9721ffc078 | 345 | water_lanes(i).x = 84 + grid; |
| el19tb | 19:6d9721ffc078 | 346 | } |
| el19tb | 18:6be4c3c94a3d | 347 | } |
| el19tb | 18:6be4c3c94a3d | 348 | } |
| el19tb | 18:6be4c3c94a3d | 349 | |
| el19tb | 19:6d9721ffc078 | 350 | void CrossyChicken::drawWater() |
| el19tb | 16:8a65cccd9685 | 351 | { |
| el19tb | 19:6d9721ffc078 | 352 | graphics.drawWater(water); |
| el19tb | 15:b15bf9357cd9 | 353 | } |
| el19tb | 15:b15bf9357cd9 | 354 | |
| el19tb | 8:52794de935b2 | 355 | // make the frog move same speed as log |
| el19tb | 8:52794de935b2 | 356 | // if the frog moves then detach |
| el19tb | 8:52794de935b2 | 357 | void CrossyChicken::frogOnLog(Car *log) { |
| el19tb | 9:d210eede4a1b | 358 | if(log->seperation != 0){ |
| el19tb | 9:d210eede4a1b | 359 | chicken.x += 1.0; |
| el19tb | 9:d210eede4a1b | 360 | } |
| el19tb | 2:86cef2afa648 | 361 | } |
| el19tb | 2:86cef2afa648 | 362 | |
| el19tb | 3:648c9d5001be | 363 | //moves the chicken around the grid |
| el19tb | 3:648c9d5001be | 364 | void CrossyChicken::moveChicken(int xWay, int yWay){ |
| el19tb | 3:648c9d5001be | 365 | //increment the left side of the chicken by a value of the grid size |
| el19tb | 4:aae7f8d4ab78 | 366 | chicken.x += xWay * 4; |
| el19tb | 3:648c9d5001be | 367 | |
| el19tb | 3:648c9d5001be | 368 | //increment the top side by a value of grid sizw |
| el19tb | 4:aae7f8d4ab78 | 369 | chicken.y += yWay * 4; |
| el19tb | 3:648c9d5001be | 370 | |
| el19tb | 6:e285eaf8bdcd | 371 | chicken.left_side = chicken.x; |
| el19tb | 6:e285eaf8bdcd | 372 | chicken.right_side = grid + chicken.x; |
| el19tb | 6:e285eaf8bdcd | 373 | chicken.up = chicken.y; |
| el19tb | 6:e285eaf8bdcd | 374 | chicken.down = grid + chicken.y; |
| el19tb | 8:52794de935b2 | 375 | |
| el19tb | 3:648c9d5001be | 376 | //display the new state of the chicken |
| el19tb | 9:d210eede4a1b | 377 | //graphics.showChicken(); |
| el19tb | 3:648c9d5001be | 378 | |
| el19tb | 8:52794de935b2 | 379 | //wait_ms(30); |
| el19tb | 2:86cef2afa648 | 380 | } |
| el19tb | 2:86cef2afa648 | 381 | |
| el19tb | 5:6e3afee7eac3 | 382 | void CrossyChicken::moveCar(Car *car, int dir, int x) { |
| el19tb | 5:6e3afee7eac3 | 383 | car->speedMedium(dir, x); |
| el19tb | 4:aae7f8d4ab78 | 384 | |
| el19tb | 8:52794de935b2 | 385 | // check if car goes out of bounds |
| el19tb | 5:6e3afee7eac3 | 386 | if(car->vehicle.x > 84+grid){ |
| el19tb | 5:6e3afee7eac3 | 387 | car->vehicle.x = -4; |
| el19tb | 3:648c9d5001be | 388 | |
| el19tb | 5:6e3afee7eac3 | 389 | } else if(car->vehicle.x < -4){ |
| el19tb | 5:6e3afee7eac3 | 390 | car->vehicle.x = 84 + grid; |
| el19tb | 5:6e3afee7eac3 | 391 | } |
| el19tb | 5:6e3afee7eac3 | 392 | } |
| el19tb | 5:6e3afee7eac3 | 393 | |
| el19tb | 5:6e3afee7eac3 | 394 | // debug |
| el19tb | 8:52794de935b2 | 395 | void CrossyChicken::setCollision(Car *car){ |
| el19tb | 4:aae7f8d4ab78 | 396 | |
| el19tb | 5:6e3afee7eac3 | 397 | float other_bottom = car->vehicle.height + car->vehicle.y; |
| el19tb | 6:e285eaf8bdcd | 398 | |
| el19tb | 6:e285eaf8bdcd | 399 | |
| el19tb | 5:6e3afee7eac3 | 400 | if(!(chicken.up >= other_bottom || |
| el19tb | 5:6e3afee7eac3 | 401 | (chicken.right_side <= car->vehicle.x) || |
| el19tb | 5:6e3afee7eac3 | 402 | (chicken.down <= car->vehicle.y) || |
| el19tb | 5:6e3afee7eac3 | 403 | chicken.left_side >= (car->vehicle.width + car->vehicle.x))){ |
| el19tb | 5:6e3afee7eac3 | 404 | graphics.printTest(); |
| el19tb | 8:52794de935b2 | 405 | |
| el19tb | 8:52794de935b2 | 406 | if(chicken.y < screenH - grid*6){ |
| el19tb | 8:52794de935b2 | 407 | attach = true; |
| el19tb | 8:52794de935b2 | 408 | } else { |
| el19tb | 8:52794de935b2 | 409 | attach = false; |
| el19tb | 8:52794de935b2 | 410 | } |
| el19tb | 8:52794de935b2 | 411 | } |
| el19tb | 8:52794de935b2 | 412 | } |
| el19tb | 8:52794de935b2 | 413 | |
| el19tb | 8:52794de935b2 | 414 | bool CrossyChicken::returnCollision(Car* log){ |
| el19tb | 8:52794de935b2 | 415 | |
| el19tb | 8:52794de935b2 | 416 | float ob= log->vehicle.height + log->vehicle.y; |
| el19tb | 8:52794de935b2 | 417 | |
| el19tb | 8:52794de935b2 | 418 | if(!(chicken.up >= ob|| |
| el19tb | 8:52794de935b2 | 419 | (chicken.right_side <= log->vehicle.x) || |
| el19tb | 8:52794de935b2 | 420 | (chicken.down <= log->vehicle.y) || |
| el19tb | 8:52794de935b2 | 421 | chicken.left_side >= (log->vehicle.width + log->vehicle.x))){ |
| el19tb | 8:52794de935b2 | 422 | return true; |
| el19tb | 5:6e3afee7eac3 | 423 | } |
| el19tb | 5:6e3afee7eac3 | 424 | } |