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@20:077f845f09f2, 2020-05-15 (annotated)
- Committer:
- el19tb
- Date:
- Fri May 15 00:16:06 2020 +0000
- Revision:
- 20:077f845f09f2
- Parent:
- 19:6d9721ffc078
- Child:
- 27:e2af196bd9ab
created a draft 1 structure of vehicle allocation
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 | 20:077f845f09f2 | 50 | void CrossyChicken::drawCar() |
| el19tb | 20:077f845f09f2 | 51 | { |
| el19tb | 20:077f845f09f2 | 52 | graphics.drawCars(local_level-> |
| el19tb | 20:077f845f09f2 | 53 | |
| el19tb | 20:077f845f09f2 | 54 | } |
| el19tb | 20:077f845f09f2 | 55 | |
| el19tb | 2:86cef2afa648 | 56 | //main function that starts the game |
| el19tb | 3:648c9d5001be | 57 | void CrossyChicken::start(){ |
| el19tb | 2:86cef2afa648 | 58 | //game setup |
| el19tb | 3:648c9d5001be | 59 | graphics.init(); |
| el19tb | 3:648c9d5001be | 60 | graphics.contrast(); |
| el19tb | 3:648c9d5001be | 61 | graphics.backLightOn(); |
| el19tb | 2:86cef2afa648 | 62 | gamepad.init(); |
| el19tb | 15:b15bf9357cd9 | 63 | row_number = 1; |
| el19tb | 15:b15bf9357cd9 | 64 | |
| el19tb | 4:aae7f8d4ab78 | 65 | |
| el19tb | 5:6e3afee7eac3 | 66 | //first lane of left racers |
| el19tb | 5:6e3afee7eac3 | 67 | firstLane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 68 | firstLane[1].setSeperation(20); |
| el19tb | 5:6e3afee7eac3 | 69 | firstLane[0].setRow(2); |
| el19tb | 5:6e3afee7eac3 | 70 | firstLane[1].setRow(2); |
| el19tb | 4:aae7f8d4ab78 | 71 | |
| el19tb | 5:6e3afee7eac3 | 72 | secondLane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 73 | secondLane[1].setSeperation(20); |
| el19tb | 5:6e3afee7eac3 | 74 | secondLane[0].setRow(3); |
| el19tb | 5:6e3afee7eac3 | 75 | secondLane[1].setRow(3); |
| el19tb | 5:6e3afee7eac3 | 76 | |
| el19tb | 7:1dce07fd0867 | 77 | thirdLane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 78 | thirdLane[1].setSeperation(20); |
| el19tb | 7:1dce07fd0867 | 79 | thirdLane[2].setSeperation(40); |
| el19tb | 5:6e3afee7eac3 | 80 | thirdLane[0].setRow(4); |
| el19tb | 5:6e3afee7eac3 | 81 | thirdLane[1].setRow(4); |
| el19tb | 5:6e3afee7eac3 | 82 | thirdLane[2].setRow(4); |
| el19tb | 7:1dce07fd0867 | 83 | |
| el19tb | 7:1dce07fd0867 | 84 | fourthLane[0].setSeperation(10); |
| el19tb | 7:1dce07fd0867 | 85 | fourthLane[1].setSeperation(30); |
| el19tb | 7:1dce07fd0867 | 86 | fourthLane[2].setSeperation(50); |
| el19tb | 7:1dce07fd0867 | 87 | fourthLane[0].setRow(5); |
| el19tb | 7:1dce07fd0867 | 88 | fourthLane[1].setRow(5); |
| el19tb | 7:1dce07fd0867 | 89 | fourthLane[2].setRow(5); |
| el19tb | 7:1dce07fd0867 | 90 | |
| el19tb | 7:1dce07fd0867 | 91 | // log lane 1 |
| el19tb | 7:1dce07fd0867 | 92 | log1Lane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 93 | log1Lane[1].setSeperation(20); |
| el19tb | 7:1dce07fd0867 | 94 | log1Lane[0].setRow(7); |
| el19tb | 7:1dce07fd0867 | 95 | log1Lane[1].setRow(7); |
| el19tb | 7:1dce07fd0867 | 96 | |
| el19tb | 7:1dce07fd0867 | 97 | // log lane 2 |
| el19tb | 7:1dce07fd0867 | 98 | lo[0].setSeperation(10); |
| el19tb | 7:1dce07fd0867 | 99 | lo[1].setSeperation(30); |
| el19tb | 7:1dce07fd0867 | 100 | lo[0].setRow(8); |
| el19tb | 7:1dce07fd0867 | 101 | lo[1].setRow(8); |
| el19tb | 7:1dce07fd0867 | 102 | |
| el19tb | 7:1dce07fd0867 | 103 | // log lane 3 |
| el19tb | 7:1dce07fd0867 | 104 | logger[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 105 | logger[1].setSeperation(20); |
| el19tb | 7:1dce07fd0867 | 106 | logger[0].setRow(9); |
| el19tb | 7:1dce07fd0867 | 107 | logger[1].setRow(9); |
| el19tb | 9:d210eede4a1b | 108 | |
| el19tb | 9:d210eede4a1b | 109 | // log lane 3 |
| el19tb | 9:d210eede4a1b | 110 | vop[0].setSeperation(0); |
| el19tb | 9:d210eede4a1b | 111 | vop[1].setSeperation(20); |
| el19tb | 9:d210eede4a1b | 112 | vop[0].setRow(10); |
| el19tb | 9:d210eede4a1b | 113 | vop[1].setRow(10); |
| el19tb | 5:6e3afee7eac3 | 114 | |
| el19tb | 2:86cef2afa648 | 115 | //keep reading and processing user input |
| el19tb | 3:648c9d5001be | 116 | while(1) { |
| el19tb | 3:648c9d5001be | 117 | graphics.clear(); |
| el19tb | 8:52794de935b2 | 118 | |
| el19tb | 8:52794de935b2 | 119 | for(int i = 0; i < 2; i++){ |
| el19tb | 5:6e3afee7eac3 | 120 | moveCar(&firstLane[i], 2, (-2)); // change x position (moving) |
| el19tb | 5:6e3afee7eac3 | 121 | } |
| el19tb | 5:6e3afee7eac3 | 122 | |
| el19tb | 5:6e3afee7eac3 | 123 | for(int x = 0; x < 2; x++){ |
| el19tb | 5:6e3afee7eac3 | 124 | moveCar(&secondLane[x], 1, 1); |
| el19tb | 5:6e3afee7eac3 | 125 | } |
| el19tb | 5:6e3afee7eac3 | 126 | |
| el19tb | 5:6e3afee7eac3 | 127 | for(int t = 0; t < 3; t++){ |
| el19tb | 5:6e3afee7eac3 | 128 | moveCar(&thirdLane[t], 2, 1); |
| el19tb | 7:1dce07fd0867 | 129 | } |
| el19tb | 7:1dce07fd0867 | 130 | |
| el19tb | 7:1dce07fd0867 | 131 | for(int c = 0; c < 3; c++){ |
| el19tb | 7:1dce07fd0867 | 132 | moveCar(&fourthLane[c], 2, 1); |
| el19tb | 7:1dce07fd0867 | 133 | } |
| el19tb | 7:1dce07fd0867 | 134 | |
| el19tb | 7:1dce07fd0867 | 135 | for(int v = 0; v < 2; v++){ |
| el19tb | 8:52794de935b2 | 136 | moveCar(&log1Lane[v], 1, 1); |
| el19tb | 7:1dce07fd0867 | 137 | } |
| el19tb | 7:1dce07fd0867 | 138 | |
| el19tb | 7:1dce07fd0867 | 139 | for(int b = 0; b < 2; b++){ |
| el19tb | 7:1dce07fd0867 | 140 | moveCar(&lo[b], 2, 1); |
| el19tb | 9:d210eede4a1b | 141 | // moveCar(&logger[b], 2, 2); |
| el19tb | 9:d210eede4a1b | 142 | moveCar(&vop[b], 1, 2); |
| el19tb | 5:6e3afee7eac3 | 143 | |
| el19tb | 5:6e3afee7eac3 | 144 | } |
| el19tb | 5:6e3afee7eac3 | 145 | |
| el19tb | 5:6e3afee7eac3 | 146 | graphics.showCar(firstLane); |
| el19tb | 5:6e3afee7eac3 | 147 | graphics.showCar(secondLane); |
| el19tb | 5:6e3afee7eac3 | 148 | graphics.showCar(thirdLane); |
| el19tb | 7:1dce07fd0867 | 149 | graphics.showCar(fourthLane); |
| el19tb | 7:1dce07fd0867 | 150 | graphics.showCar(log1Lane); |
| el19tb | 7:1dce07fd0867 | 151 | graphics.showCar(lo); |
| el19tb | 9:d210eede4a1b | 152 | graphics.showCar(logger); |
| el19tb | 9:d210eede4a1b | 153 | graphics.showCar(vop); |
| el19tb | 9:d210eede4a1b | 154 | |
| el19tb | 8:52794de935b2 | 155 | graphics.showChicken(); |
| el19tb | 8:52794de935b2 | 156 | process_input(); |
| el19tb | 8:52794de935b2 | 157 | |
| el19tb | 8:52794de935b2 | 158 | // now when the chicken is above the safety |
| el19tb | 8:52794de935b2 | 159 | // lane we need to ensure that it only can go on logs |
| el19tb | 8:52794de935b2 | 160 | if(chicken.y < screenH - grid*6){ |
| el19tb | 8:52794de935b2 | 161 | frogDie = true; // frog can die if it is in water |
| el19tb | 7:1dce07fd0867 | 162 | |
| el19tb | 8:52794de935b2 | 163 | //if it is not in a log |
| el19tb | 8:52794de935b2 | 164 | for(int x = 0; x < 2; x++){ |
| el19tb | 8:52794de935b2 | 165 | setCollision(&log1Lane[x]); |
| el19tb | 9:d210eede4a1b | 166 | setCollision(&lo[x]); |
| el19tb | 9:d210eede4a1b | 167 | setCollision(&logger[x]); |
| el19tb | 9:d210eede4a1b | 168 | setCollision(&vop[x]); |
| el19tb | 9:d210eede4a1b | 169 | |
| el19tb | 8:52794de935b2 | 170 | if(attach){ |
| el19tb | 8:52794de935b2 | 171 | frogOnLog(&log1Lane[x]); |
| el19tb | 9:d210eede4a1b | 172 | frogOnLog(&lo[x]); |
| el19tb | 9:d210eede4a1b | 173 | frogOnLog(&logger[x]); |
| el19tb | 9:d210eede4a1b | 174 | frogOnLog(&vop[x]); |
| el19tb | 9:d210eede4a1b | 175 | |
| el19tb | 8:52794de935b2 | 176 | } |
| el19tb | 8:52794de935b2 | 177 | } |
| el19tb | 8:52794de935b2 | 178 | |
| el19tb | 8:52794de935b2 | 179 | if(!attach){ |
| el19tb | 8:52794de935b2 | 180 | graphics.print(); |
| el19tb | 9:d210eede4a1b | 181 | } |
| el19tb | 6:e285eaf8bdcd | 182 | } |
| el19tb | 9:d210eede4a1b | 183 | |
| el19tb | 3:648c9d5001be | 184 | graphics.refresh(); |
| el19tb | 9:d210eede4a1b | 185 | wait_ms(70); |
| el19tb | 2:86cef2afa648 | 186 | } |
| el19tb | 2:86cef2afa648 | 187 | } |
| el19tb | 2:86cef2afa648 | 188 | |
| el19tb | 7:1dce07fd0867 | 189 | // log intersects then frog.x = log[i].speed |
| el19tb | 7:1dce07fd0867 | 190 | // frog is attached function |
| el19tb | 7:1dce07fd0867 | 191 | // detach the frog when user goes up or down |
| el19tb | 7:1dce07fd0867 | 192 | // if the frog is back to the safe zone detatch also |
| el19tb | 7:1dce07fd0867 | 193 | // whenever the frog moves detach |
| el19tb | 2:86cef2afa648 | 194 | //A moves right |
| el19tb | 2:86cef2afa648 | 195 | //X moves upward |
| el19tb | 2:86cef2afa648 | 196 | //B moves downward |
| el19tb | 2:86cef2afa648 | 197 | //Y moves left |
| el19tb | 2:86cef2afa648 | 198 | void CrossyChicken::process_input() { |
| el19tb | 3:648c9d5001be | 199 | //determine the input |
| el19tb | 8:52794de935b2 | 200 | /* make this a switch statement */ |
| el19tb | 3:648c9d5001be | 201 | if(gamepad.A_pressed()){ |
| el19tb | 3:648c9d5001be | 202 | moveChicken(1,0); |
| el19tb | 8:52794de935b2 | 203 | attach = false; |
| el19tb | 3:648c9d5001be | 204 | } else if(gamepad.X_pressed()){ |
| el19tb | 3:648c9d5001be | 205 | moveChicken(0,-1); |
| el19tb | 8:52794de935b2 | 206 | attach = false; |
| el19tb | 3:648c9d5001be | 207 | } else if(gamepad.B_pressed()){ |
| el19tb | 3:648c9d5001be | 208 | moveChicken(0,1); |
| el19tb | 8:52794de935b2 | 209 | attach = false; |
| el19tb | 3:648c9d5001be | 210 | } else if(gamepad.Y_pressed()){ |
| el19tb | 3:648c9d5001be | 211 | moveChicken(-1,0); |
| el19tb | 8:52794de935b2 | 212 | attach = false; |
| el19tb | 8:52794de935b2 | 213 | } |
| el19tb | 8:52794de935b2 | 214 | } |
| el19tb | 8:52794de935b2 | 215 | |
| el19tb | 19:6d9721ffc078 | 216 | void CrossyChicken::createMultipleSafetyLane() |
| el19tb | 19:6d9721ffc078 | 217 | { |
| el19tb | 19:6d9721ffc078 | 218 | int min = 0; // start from the top, and draw till 0 |
| el19tb | 19:6d9721ffc078 | 219 | int max = grid_width; // draw 21 objects to the first lane |
| el19tb | 19:6d9721ffc078 | 220 | int row = 1; |
| el19tb | 19:6d9721ffc078 | 221 | int x_fac = 1; |
| el19tb | 19:6d9721ffc078 | 222 | |
| el19tb | 19:6d9721ffc078 | 223 | for(int x = 0; x < 2; x++){ |
| el19tb | 19:6d9721ffc078 | 224 | creatSafetyObject(min, max, row, x_fac); |
| el19tb | 19:6d9721ffc078 | 225 | row += 6; // increment the rows to the next lane |
| el19tb | 19:6d9721ffc078 | 226 | min += 22; // fill in the vector with 22 grid units |
| el19tb | 19:6d9721ffc078 | 227 | max += 22; |
| el19tb | 19:6d9721ffc078 | 228 | } |
| el19tb | 19:6d9721ffc078 | 229 | } |
| el19tb | 18:6be4c3c94a3d | 230 | |
| el19tb | 19:6d9721ffc078 | 231 | void CrossyChicken::createSafetyObject(int min, int max, int row, int x_fac) |
| el19tb | 19:6d9721ffc078 | 232 | { |
| el19tb | 19:6d9721ffc078 | 233 | int state = 1; |
| el19tb | 19:6d9721ffc078 | 234 | |
| el19tb | 19:6d9721ffc078 | 235 | for(int z = 0; z < 22; z++){ |
| el19tb | 19:6d9721ffc078 | 236 | switch(state) |
| el19tb | 19:6d9721ffc078 | 237 | { |
| el19tb | 19:6d9721ffc078 | 238 | case 1: |
| el19tb | 19:6d9721ffc078 | 239 | safety_lane.push_back(z/x_fac, row, 'K'); // safety lane so row 0 |
| el19tb | 19:6d9721ffc078 | 240 | state++; |
| el19tb | 19:6d9721ffc078 | 241 | break; |
| el19tb | 19:6d9721ffc078 | 242 | case 2: |
| el19tb | 19:6d9721ffc078 | 243 | safety_lane.push_back(z/x_fac, row, 'P'); // safety lane so row 0 |
| el19tb | 19:6d9721ffc078 | 244 | state--; // back to inital state |
| el19tb | 19:6d9721ffc078 | 245 | break; |
| el19tb | 19:6d9721ffc078 | 246 | } |
| el19tb | 19:6d9721ffc078 | 247 | } |
| el19tb | 19:6d9721ffc078 | 248 | } |
| el19tb | 18:6be4c3c94a3d | 249 | |
| el19tb | 19:6d9721ffc078 | 250 | void CrossyChicken::drawSafety() |
| el19tb | 19:6d9721ffc078 | 251 | { |
| el19tb | 19:6d9721ffc078 | 252 | graphics.drawSafetyLanes(safety_lane); |
| el19tb | 19:6d9721ffc078 | 253 | } |
| el19tb | 18:6be4c3c94a3d | 254 | |
| el19tb | 18:6be4c3c94a3d | 255 | |
| el19tb | 18:6be4c3c94a3d | 256 | void CrossyChicken::createMultipleRoadLane() |
| el19tb | 15:b15bf9357cd9 | 257 | { |
| el19tb | 18:6be4c3c94a3d | 258 | int min = 0; // start from the top, and draw till 0 |
| el19tb | 18:6be4c3c94a3d | 259 | int max = grid_width; // draw 21 objects to the first lane |
| el19tb | 18:6be4c3c94a3d | 260 | int row = 1; |
| el19tb | 15:b15bf9357cd9 | 261 | |
| el19tb | 18:6be4c3c94a3d | 262 | for(int x = 0; x < 4; x++){ |
| el19tb | 18:6be4c3c94a3d | 263 | createRoadLane(min, max, row); |
| el19tb | 18:6be4c3c94a3d | 264 | row++; // increment the rows to the next lane |
| el19tb | 18:6be4c3c94a3d | 265 | min += 22; // fill in the vector with 22 grid units |
| el19tb | 18:6be4c3c94a3d | 266 | max += 22; |
| el19tb | 16:8a65cccd9685 | 267 | } |
| el19tb | 16:8a65cccd9685 | 268 | } |
| el19tb | 18:6be4c3c94a3d | 269 | |
| el19tb | 18:6be4c3c94a3d | 270 | // every level is going to have the same amount of blocks |
| el19tb | 18:6be4c3c94a3d | 271 | void CrossyChicken::createRoadLane(int min, int max, int row) |
| el19tb | 18:6be4c3c94a3d | 272 | { |
| el19tb | 18:6be4c3c94a3d | 273 | std::vector<Background>::size_type it; |
| el19tb | 18:6be4c3c94a3d | 274 | |
| el19tb | 18:6be4c3c94a3d | 275 | // fill the road objects |
| el19tb | 18:6be4c3c94a3d | 276 | for(int it = min; it != max; it++){ |
| el19tb | 18:6be4c3c94a3d | 277 | roads.push_back(it/row, row); // it is the x pos of the obj, row is the y pos |
| el19tb | 18:6be4c3c94a3d | 278 | } |
| el19tb | 18:6be4c3c94a3d | 279 | } |
| el19tb | 18:6be4c3c94a3d | 280 | |
| el19tb | 18:6be4c3c94a3d | 281 | void CrossyChicken::drawRoadObjects() |
| el19tb | 18:6be4c3c94a3d | 282 | { |
| el19tb | 18:6be4c3c94a3d | 283 | graphics.getRoadObjects(roads); |
| el19tb | 18:6be4c3c94a3d | 284 | } |
| el19tb | 18:6be4c3c94a3d | 285 | |
| el19tb | 19:6d9721ffc078 | 286 | void CrossyChicken::createMultipleLanesWater() |
| el19tb | 19:6d9721ffc078 | 287 | { |
| el19tb | 19:6d9721ffc078 | 288 | int min = 0; // start from the top, and draw till 0 |
| el19tb | 19:6d9721ffc078 | 289 | int max = grid_width; // draw 21 objects to the first lane |
| el19tb | 19:6d9721ffc078 | 290 | int row = 7; |
| el19tb | 19:6d9721ffc078 | 291 | int x_fac = 1; |
| el19tb | 19:6d9721ffc078 | 292 | |
| el19tb | 19:6d9721ffc078 | 293 | for(int x = 0; x < 3; x++){ |
| el19tb | 19:6d9721ffc078 | 294 | if(x == 1){ |
| el19tb | 19:6d9721ffc078 | 295 | createWaterLane(min, max, row, -0.6); // second row goes left |
| el19tb | 19:6d9721ffc078 | 296 | } else { |
| el19tb | 19:6d9721ffc078 | 297 | createWaterLane(min, max, row, 0.4); // rest of the rows go right |
| el19tb | 19:6d9721ffc078 | 298 | } |
| el19tb | 19:6d9721ffc078 | 299 | |
| el19tb | 19:6d9721ffc078 | 300 | row++; // increment the rows to the next lane |
| el19tb | 19:6d9721ffc078 | 301 | min += grid_width; // fill in the vector with 22 grid units |
| el19tb | 19:6d9721ffc078 | 302 | max += grid_width; |
| el19tb | 19:6d9721ffc078 | 303 | } |
| el19tb | 19:6d9721ffc078 | 304 | } |
| el19tb | 19:6d9721ffc078 | 305 | |
| el19tb | 19:6d9721ffc078 | 306 | void CrossyChicken::createWaterLane(int min, int max, int row, float speed, int x_fac){ |
| el19tb | 19:6d9721ffc078 | 307 | int state = 1; |
| el19tb | 19:6d9721ffc078 | 308 | for(it = min; it < max; it++) |
| el19tb | 19:6d9721ffc078 | 309 | { |
| el19tb | 19:6d9721ffc078 | 310 | switch(state) |
| el19tb | 19:6d9721ffc078 | 311 | { |
| el19tb | 19:6d9721ffc078 | 312 | case 1: // initial state |
| el19tb | 19:6d9721ffc078 | 313 | water[it].push_back(it/x_fac, row, speed, 'F'); // first type of water |
| el19tb | 19:6d9721ffc078 | 314 | state++; // transition to next state |
| el19tb | 19:6d9721ffc078 | 315 | break; |
| el19tb | 19:6d9721ffc078 | 316 | case 2: |
| el19tb | 19:6d9721ffc078 | 317 | water[it].push_back(it/x_fac, row, speed, 'S'); // first type of water |
| el19tb | 19:6d9721ffc078 | 318 | state++; // transition to state 3 |
| el19tb | 19:6d9721ffc078 | 319 | break; |
| el19tb | 19:6d9721ffc078 | 320 | case 3: |
| el19tb | 19:6d9721ffc078 | 321 | water[it].push_back(it/x_fac, row, speed, 'T'); // first type of water |
| el19tb | 19:6d9721ffc078 | 322 | state = 1; // back to start state |
| el19tb | 19:6d9721ffc078 | 323 | break; |
| el19tb | 19:6d9721ffc078 | 324 | } |
| el19tb | 19:6d9721ffc078 | 325 | } |
| el19tb | 19:6d9721ffc078 | 326 | } |
| el19tb | 19:6d9721ffc078 | 327 | |
| el19tb | 19:6d9721ffc078 | 328 | // moves the water based on the velocity |
| el19tb | 19:6d9721ffc078 | 329 | void CrossyChicken::moveWater(std::vector<Water>& water_lanes) |
| el19tb | 18:6be4c3c94a3d | 330 | { |
| el19tb | 19:6d9721ffc078 | 331 | for(unsigned int i = 0; i < water_lanes.size(); i++) |
| el19tb | 19:6d9721ffc078 | 332 | { |
| el19tb | 19:6d9721ffc078 | 333 | if(water_lanes(i).speed > 0){ |
| el19tb | 19:6d9721ffc078 | 334 | water_lanes(i).x += speed; |
| el19tb | 19:6d9721ffc078 | 335 | } else { |
| el19tb | 19:6d9721ffc078 | 336 | water_lanes(i).x -= speed; |
| el19tb | 19:6d9721ffc078 | 337 | } |
| el19tb | 19:6d9721ffc078 | 338 | } |
| el19tb | 18:6be4c3c94a3d | 339 | |
| el19tb | 19:6d9721ffc078 | 340 | loopWater(water_lanes); |
| el19tb | 19:6d9721ffc078 | 341 | } |
| el19tb | 19:6d9721ffc078 | 342 | |
| el19tb | 19:6d9721ffc078 | 343 | |
| el19tb | 19:6d9721ffc078 | 344 | void CrossyChicken::loopWater(std::vector<Water>& water_lanes) |
| el19tb | 19:6d9721ffc078 | 345 | { |
| el19tb | 19:6d9721ffc078 | 346 | for(unsigned int i = 0; i < water_lanes.size(); i++) |
| el19tb | 19:6d9721ffc078 | 347 | { |
| el19tb | 19:6d9721ffc078 | 348 | if(water_lanes(i).x > 84+grid){ |
| el19tb | 19:6d9721ffc078 | 349 | water_lanes(i).x = -4; |
| el19tb | 19:6d9721ffc078 | 350 | } else if(water_lanes[i].x < -4){ |
| el19tb | 19:6d9721ffc078 | 351 | water_lanes(i).x = 84 + grid; |
| el19tb | 19:6d9721ffc078 | 352 | } |
| el19tb | 18:6be4c3c94a3d | 353 | } |
| el19tb | 18:6be4c3c94a3d | 354 | } |
| el19tb | 18:6be4c3c94a3d | 355 | |
| el19tb | 19:6d9721ffc078 | 356 | void CrossyChicken::drawWater() |
| el19tb | 16:8a65cccd9685 | 357 | { |
| el19tb | 19:6d9721ffc078 | 358 | graphics.drawWater(water); |
| el19tb | 15:b15bf9357cd9 | 359 | } |
| el19tb | 15:b15bf9357cd9 | 360 | |
| el19tb | 8:52794de935b2 | 361 | // make the frog move same speed as log |
| el19tb | 8:52794de935b2 | 362 | // if the frog moves then detach |
| el19tb | 8:52794de935b2 | 363 | void CrossyChicken::frogOnLog(Car *log) { |
| el19tb | 9:d210eede4a1b | 364 | if(log->seperation != 0){ |
| el19tb | 9:d210eede4a1b | 365 | chicken.x += 1.0; |
| el19tb | 9:d210eede4a1b | 366 | } |
| el19tb | 2:86cef2afa648 | 367 | } |
| el19tb | 2:86cef2afa648 | 368 | |
| el19tb | 3:648c9d5001be | 369 | //moves the chicken around the grid |
| el19tb | 3:648c9d5001be | 370 | void CrossyChicken::moveChicken(int xWay, int yWay){ |
| el19tb | 3:648c9d5001be | 371 | //increment the left side of the chicken by a value of the grid size |
| el19tb | 4:aae7f8d4ab78 | 372 | chicken.x += xWay * 4; |
| el19tb | 3:648c9d5001be | 373 | |
| el19tb | 3:648c9d5001be | 374 | //increment the top side by a value of grid sizw |
| el19tb | 4:aae7f8d4ab78 | 375 | chicken.y += yWay * 4; |
| el19tb | 3:648c9d5001be | 376 | |
| el19tb | 6:e285eaf8bdcd | 377 | chicken.left_side = chicken.x; |
| el19tb | 6:e285eaf8bdcd | 378 | chicken.right_side = grid + chicken.x; |
| el19tb | 6:e285eaf8bdcd | 379 | chicken.up = chicken.y; |
| el19tb | 6:e285eaf8bdcd | 380 | chicken.down = grid + chicken.y; |
| el19tb | 8:52794de935b2 | 381 | |
| el19tb | 3:648c9d5001be | 382 | //display the new state of the chicken |
| el19tb | 9:d210eede4a1b | 383 | //graphics.showChicken(); |
| el19tb | 3:648c9d5001be | 384 | |
| el19tb | 8:52794de935b2 | 385 | //wait_ms(30); |
| el19tb | 2:86cef2afa648 | 386 | } |
| el19tb | 2:86cef2afa648 | 387 | |
| el19tb | 5:6e3afee7eac3 | 388 | void CrossyChicken::moveCar(Car *car, int dir, int x) { |
| el19tb | 5:6e3afee7eac3 | 389 | car->speedMedium(dir, x); |
| el19tb | 4:aae7f8d4ab78 | 390 | |
| el19tb | 8:52794de935b2 | 391 | // check if car goes out of bounds |
| el19tb | 5:6e3afee7eac3 | 392 | if(car->vehicle.x > 84+grid){ |
| el19tb | 5:6e3afee7eac3 | 393 | car->vehicle.x = -4; |
| el19tb | 3:648c9d5001be | 394 | |
| el19tb | 5:6e3afee7eac3 | 395 | } else if(car->vehicle.x < -4){ |
| el19tb | 5:6e3afee7eac3 | 396 | car->vehicle.x = 84 + grid; |
| el19tb | 5:6e3afee7eac3 | 397 | } |
| el19tb | 5:6e3afee7eac3 | 398 | } |
| el19tb | 5:6e3afee7eac3 | 399 | |
| el19tb | 5:6e3afee7eac3 | 400 | // debug |
| el19tb | 8:52794de935b2 | 401 | void CrossyChicken::setCollision(Car *car){ |
| el19tb | 4:aae7f8d4ab78 | 402 | |
| el19tb | 5:6e3afee7eac3 | 403 | float other_bottom = car->vehicle.height + car->vehicle.y; |
| el19tb | 6:e285eaf8bdcd | 404 | |
| el19tb | 6:e285eaf8bdcd | 405 | |
| el19tb | 5:6e3afee7eac3 | 406 | if(!(chicken.up >= other_bottom || |
| el19tb | 5:6e3afee7eac3 | 407 | (chicken.right_side <= car->vehicle.x) || |
| el19tb | 5:6e3afee7eac3 | 408 | (chicken.down <= car->vehicle.y) || |
| el19tb | 5:6e3afee7eac3 | 409 | chicken.left_side >= (car->vehicle.width + car->vehicle.x))){ |
| el19tb | 5:6e3afee7eac3 | 410 | graphics.printTest(); |
| el19tb | 8:52794de935b2 | 411 | |
| el19tb | 8:52794de935b2 | 412 | if(chicken.y < screenH - grid*6){ |
| el19tb | 8:52794de935b2 | 413 | attach = true; |
| el19tb | 8:52794de935b2 | 414 | } else { |
| el19tb | 8:52794de935b2 | 415 | attach = false; |
| el19tb | 8:52794de935b2 | 416 | } |
| el19tb | 8:52794de935b2 | 417 | } |
| el19tb | 8:52794de935b2 | 418 | } |
| el19tb | 8:52794de935b2 | 419 | |
| el19tb | 8:52794de935b2 | 420 | bool CrossyChicken::returnCollision(Car* log){ |
| el19tb | 8:52794de935b2 | 421 | |
| el19tb | 8:52794de935b2 | 422 | float ob= log->vehicle.height + log->vehicle.y; |
| el19tb | 8:52794de935b2 | 423 | |
| el19tb | 8:52794de935b2 | 424 | if(!(chicken.up >= ob|| |
| el19tb | 8:52794de935b2 | 425 | (chicken.right_side <= log->vehicle.x) || |
| el19tb | 8:52794de935b2 | 426 | (chicken.down <= log->vehicle.y) || |
| el19tb | 8:52794de935b2 | 427 | chicken.left_side >= (log->vehicle.width + log->vehicle.x))){ |
| el19tb | 8:52794de935b2 | 428 | return true; |
| el19tb | 5:6e3afee7eac3 | 429 | } |
| el19tb | 5:6e3afee7eac3 | 430 | } |