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@8:52794de935b2, 2020-05-12 (annotated)
- Committer:
- el19tb
- Date:
- Tue May 12 05:14:17 2020 +0000
- Revision:
- 8:52794de935b2
- Parent:
- 7:1dce07fd0867
- Child:
- 9:d210eede4a1b
implemented the attach function so the frog can jump on a log
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 | 2:86cef2afa648 | 7 | |
| el19tb | 5:6e3afee7eac3 | 8 | // start from the top (height) |
| el19tb | 2:86cef2afa648 | 9 | Gamepad gamepad; |
| el19tb | 2:86cef2afa648 | 10 | |
| el19tb | 3:648c9d5001be | 11 | //create three class: CAR LANE, SAFETY LANE, OBSTACLE LANE(water) |
| el19tb | 3:648c9d5001be | 12 | //random |
| el19tb | 3:648c9d5001be | 13 | //algoritmic way of generating lanes |
| el19tb | 3:648c9d5001be | 14 | |
| el19tb | 2:86cef2afa648 | 15 | //size of each each tile in the game |
| el19tb | 4:aae7f8d4ab78 | 16 | int grid = 4; |
| el19tb | 2:86cef2afa648 | 17 | |
| el19tb | 3:648c9d5001be | 18 | //make one object of chicken |
| el19tb | 3:648c9d5001be | 19 | //use this to move around the lcd |
| el19tb | 6:e285eaf8bdcd | 20 | Chicken chicken((84/2)-grid/2, 48-grid, grid); |
| el19tb | 3:648c9d5001be | 21 | Chicken *chickenptr= &chicken; |
| el19tb | 3:648c9d5001be | 22 | |
| el19tb | 3:648c9d5001be | 23 | //there will be multiple cars |
| el19tb | 5:6e3afee7eac3 | 24 | Car firstLane[2]; |
| el19tb | 5:6e3afee7eac3 | 25 | Car secondLane[2]; |
| el19tb | 5:6e3afee7eac3 | 26 | Car thirdLane[3]; |
| el19tb | 7:1dce07fd0867 | 27 | Car fourthLane[3]; |
| el19tb | 7:1dce07fd0867 | 28 | Car log1Lane[2]; |
| el19tb | 7:1dce07fd0867 | 29 | Car lo[2]; |
| el19tb | 7:1dce07fd0867 | 30 | Car logger[2]; |
| el19tb | 3:648c9d5001be | 31 | |
| el19tb | 8:52794de935b2 | 32 | int screenW = 84; |
| el19tb | 8:52794de935b2 | 33 | int screenH = 48; |
| el19tb | 8:52794de935b2 | 34 | |
| el19tb | 3:648c9d5001be | 35 | //class that whill show objects |
| el19tb | 3:648c9d5001be | 36 | GraphicEngine graphics(chickenptr); |
| el19tb | 2:86cef2afa648 | 37 | |
| el19tb | 8:52794de935b2 | 38 | bool frogLog = false; |
| el19tb | 8:52794de935b2 | 39 | bool frogDie = false; // whenever the frog is above safety lane in the middle |
| el19tb | 8:52794de935b2 | 40 | bool attach = false; |
| el19tb | 8:52794de935b2 | 41 | |
| el19tb | 2:86cef2afa648 | 42 | //main function that starts the game |
| el19tb | 3:648c9d5001be | 43 | void CrossyChicken::start(){ |
| el19tb | 2:86cef2afa648 | 44 | //game setup |
| el19tb | 3:648c9d5001be | 45 | graphics.init(); |
| el19tb | 3:648c9d5001be | 46 | graphics.contrast(); |
| el19tb | 3:648c9d5001be | 47 | graphics.backLightOn(); |
| el19tb | 2:86cef2afa648 | 48 | gamepad.init(); |
| el19tb | 4:aae7f8d4ab78 | 49 | |
| el19tb | 5:6e3afee7eac3 | 50 | //first lane of left racers |
| el19tb | 5:6e3afee7eac3 | 51 | firstLane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 52 | firstLane[1].setSeperation(20); |
| el19tb | 5:6e3afee7eac3 | 53 | firstLane[0].setRow(2); |
| el19tb | 5:6e3afee7eac3 | 54 | firstLane[1].setRow(2); |
| el19tb | 4:aae7f8d4ab78 | 55 | |
| el19tb | 5:6e3afee7eac3 | 56 | secondLane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 57 | secondLane[1].setSeperation(20); |
| el19tb | 5:6e3afee7eac3 | 58 | secondLane[0].setRow(3); |
| el19tb | 5:6e3afee7eac3 | 59 | secondLane[1].setRow(3); |
| el19tb | 5:6e3afee7eac3 | 60 | |
| el19tb | 7:1dce07fd0867 | 61 | thirdLane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 62 | thirdLane[1].setSeperation(20); |
| el19tb | 7:1dce07fd0867 | 63 | thirdLane[2].setSeperation(40); |
| el19tb | 5:6e3afee7eac3 | 64 | thirdLane[0].setRow(4); |
| el19tb | 5:6e3afee7eac3 | 65 | thirdLane[1].setRow(4); |
| el19tb | 5:6e3afee7eac3 | 66 | thirdLane[2].setRow(4); |
| el19tb | 7:1dce07fd0867 | 67 | |
| el19tb | 7:1dce07fd0867 | 68 | fourthLane[0].setSeperation(10); |
| el19tb | 7:1dce07fd0867 | 69 | fourthLane[1].setSeperation(30); |
| el19tb | 7:1dce07fd0867 | 70 | fourthLane[2].setSeperation(50); |
| el19tb | 7:1dce07fd0867 | 71 | fourthLane[0].setRow(5); |
| el19tb | 7:1dce07fd0867 | 72 | fourthLane[1].setRow(5); |
| el19tb | 7:1dce07fd0867 | 73 | fourthLane[2].setRow(5); |
| el19tb | 7:1dce07fd0867 | 74 | |
| el19tb | 7:1dce07fd0867 | 75 | // log lane 1 |
| el19tb | 7:1dce07fd0867 | 76 | log1Lane[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 77 | log1Lane[1].setSeperation(20); |
| el19tb | 7:1dce07fd0867 | 78 | log1Lane[0].setRow(7); |
| el19tb | 7:1dce07fd0867 | 79 | log1Lane[1].setRow(7); |
| el19tb | 7:1dce07fd0867 | 80 | |
| el19tb | 7:1dce07fd0867 | 81 | // log lane 2 |
| el19tb | 7:1dce07fd0867 | 82 | lo[0].setSeperation(10); |
| el19tb | 7:1dce07fd0867 | 83 | lo[1].setSeperation(30); |
| el19tb | 7:1dce07fd0867 | 84 | lo[0].setRow(8); |
| el19tb | 7:1dce07fd0867 | 85 | lo[1].setRow(8); |
| el19tb | 7:1dce07fd0867 | 86 | |
| el19tb | 7:1dce07fd0867 | 87 | // log lane 3 |
| el19tb | 7:1dce07fd0867 | 88 | logger[0].setSeperation(0); |
| el19tb | 7:1dce07fd0867 | 89 | logger[1].setSeperation(20); |
| el19tb | 7:1dce07fd0867 | 90 | logger[0].setRow(9); |
| el19tb | 7:1dce07fd0867 | 91 | logger[1].setRow(9); |
| el19tb | 7:1dce07fd0867 | 92 | |
| el19tb | 5:6e3afee7eac3 | 93 | |
| el19tb | 2:86cef2afa648 | 94 | //keep reading and processing user input |
| el19tb | 3:648c9d5001be | 95 | while(1) { |
| el19tb | 3:648c9d5001be | 96 | graphics.clear(); |
| el19tb | 8:52794de935b2 | 97 | |
| el19tb | 8:52794de935b2 | 98 | for(int i = 0; i < 2; i++){ |
| el19tb | 5:6e3afee7eac3 | 99 | moveCar(&firstLane[i], 2, (-2)); // change x position (moving) |
| el19tb | 5:6e3afee7eac3 | 100 | } |
| el19tb | 5:6e3afee7eac3 | 101 | |
| el19tb | 5:6e3afee7eac3 | 102 | for(int x = 0; x < 2; x++){ |
| el19tb | 5:6e3afee7eac3 | 103 | moveCar(&secondLane[x], 1, 1); |
| el19tb | 5:6e3afee7eac3 | 104 | } |
| el19tb | 5:6e3afee7eac3 | 105 | |
| el19tb | 5:6e3afee7eac3 | 106 | for(int t = 0; t < 3; t++){ |
| el19tb | 5:6e3afee7eac3 | 107 | moveCar(&thirdLane[t], 2, 1); |
| el19tb | 7:1dce07fd0867 | 108 | } |
| el19tb | 7:1dce07fd0867 | 109 | |
| el19tb | 7:1dce07fd0867 | 110 | for(int c = 0; c < 3; c++){ |
| el19tb | 7:1dce07fd0867 | 111 | moveCar(&fourthLane[c], 2, 1); |
| el19tb | 7:1dce07fd0867 | 112 | } |
| el19tb | 7:1dce07fd0867 | 113 | |
| el19tb | 7:1dce07fd0867 | 114 | for(int v = 0; v < 2; v++){ |
| el19tb | 8:52794de935b2 | 115 | moveCar(&log1Lane[v], 1, 1); |
| el19tb | 7:1dce07fd0867 | 116 | } |
| el19tb | 7:1dce07fd0867 | 117 | |
| el19tb | 7:1dce07fd0867 | 118 | for(int b = 0; b < 2; b++){ |
| el19tb | 7:1dce07fd0867 | 119 | moveCar(&lo[b], 2, 1); |
| el19tb | 7:1dce07fd0867 | 120 | moveCar(&logger[b], 2, 2); |
| el19tb | 5:6e3afee7eac3 | 121 | |
| el19tb | 5:6e3afee7eac3 | 122 | } |
| el19tb | 5:6e3afee7eac3 | 123 | |
| el19tb | 5:6e3afee7eac3 | 124 | graphics.showCar(firstLane); |
| el19tb | 5:6e3afee7eac3 | 125 | graphics.showCar(secondLane); |
| el19tb | 5:6e3afee7eac3 | 126 | graphics.showCar(thirdLane); |
| el19tb | 7:1dce07fd0867 | 127 | graphics.showCar(fourthLane); |
| el19tb | 7:1dce07fd0867 | 128 | graphics.showCar(log1Lane); |
| el19tb | 7:1dce07fd0867 | 129 | graphics.showCar(lo); |
| el19tb | 7:1dce07fd0867 | 130 | //graphics.showCar(logger); |
| el19tb | 8:52794de935b2 | 131 | |
| el19tb | 8:52794de935b2 | 132 | graphics.showChicken(); |
| el19tb | 8:52794de935b2 | 133 | process_input(); |
| el19tb | 8:52794de935b2 | 134 | |
| el19tb | 8:52794de935b2 | 135 | // now when the chicken is above the safety |
| el19tb | 8:52794de935b2 | 136 | // lane we need to ensure that it only can go on logs |
| el19tb | 8:52794de935b2 | 137 | if(chicken.y < screenH - grid*6){ |
| el19tb | 8:52794de935b2 | 138 | frogDie = true; // frog can die if it is in water |
| el19tb | 7:1dce07fd0867 | 139 | |
| el19tb | 8:52794de935b2 | 140 | //if it is not in a log |
| el19tb | 8:52794de935b2 | 141 | for(int x = 0; x < 2; x++){ |
| el19tb | 8:52794de935b2 | 142 | setCollision(&log1Lane[x]); |
| el19tb | 8:52794de935b2 | 143 | |
| el19tb | 8:52794de935b2 | 144 | if(attach){ |
| el19tb | 8:52794de935b2 | 145 | frogOnLog(&log1Lane[x]); |
| el19tb | 8:52794de935b2 | 146 | } |
| el19tb | 8:52794de935b2 | 147 | } |
| el19tb | 8:52794de935b2 | 148 | |
| el19tb | 8:52794de935b2 | 149 | if(!attach){ |
| el19tb | 8:52794de935b2 | 150 | graphics.print(); |
| el19tb | 8:52794de935b2 | 151 | } |
| el19tb | 8:52794de935b2 | 152 | |
| el19tb | 6:e285eaf8bdcd | 153 | } |
| el19tb | 8:52794de935b2 | 154 | |
| el19tb | 8:52794de935b2 | 155 | |
| el19tb | 3:648c9d5001be | 156 | graphics.refresh(); |
| el19tb | 8:52794de935b2 | 157 | wait_ms(200); |
| el19tb | 2:86cef2afa648 | 158 | } |
| el19tb | 2:86cef2afa648 | 159 | } |
| el19tb | 2:86cef2afa648 | 160 | |
| el19tb | 7:1dce07fd0867 | 161 | // log intersects then frog.x = log[i].speed |
| el19tb | 7:1dce07fd0867 | 162 | // frog is attached function |
| el19tb | 7:1dce07fd0867 | 163 | // detach the frog when user goes up or down |
| el19tb | 7:1dce07fd0867 | 164 | // if the frog is back to the safe zone detatch also |
| el19tb | 7:1dce07fd0867 | 165 | // whenever the frog moves detach |
| el19tb | 2:86cef2afa648 | 166 | //A moves right |
| el19tb | 2:86cef2afa648 | 167 | //X moves upward |
| el19tb | 2:86cef2afa648 | 168 | //B moves downward |
| el19tb | 2:86cef2afa648 | 169 | //Y moves left |
| el19tb | 2:86cef2afa648 | 170 | void CrossyChicken::process_input() { |
| el19tb | 3:648c9d5001be | 171 | //determine the input |
| el19tb | 8:52794de935b2 | 172 | /* make this a switch statement */ |
| el19tb | 3:648c9d5001be | 173 | if(gamepad.A_pressed()){ |
| el19tb | 3:648c9d5001be | 174 | moveChicken(1,0); |
| el19tb | 8:52794de935b2 | 175 | attach = false; |
| el19tb | 3:648c9d5001be | 176 | } else if(gamepad.X_pressed()){ |
| el19tb | 3:648c9d5001be | 177 | moveChicken(0,-1); |
| el19tb | 8:52794de935b2 | 178 | attach = false; |
| el19tb | 3:648c9d5001be | 179 | } else if(gamepad.B_pressed()){ |
| el19tb | 3:648c9d5001be | 180 | moveChicken(0,1); |
| el19tb | 8:52794de935b2 | 181 | attach = false; |
| el19tb | 3:648c9d5001be | 182 | } else if(gamepad.Y_pressed()){ |
| el19tb | 3:648c9d5001be | 183 | moveChicken(-1,0); |
| el19tb | 8:52794de935b2 | 184 | attach = false; |
| el19tb | 8:52794de935b2 | 185 | } |
| el19tb | 8:52794de935b2 | 186 | } |
| el19tb | 8:52794de935b2 | 187 | |
| el19tb | 8:52794de935b2 | 188 | // make the frog move same speed as log |
| el19tb | 8:52794de935b2 | 189 | // if the frog moves then detach |
| el19tb | 8:52794de935b2 | 190 | void CrossyChicken::frogOnLog(Car *log) { |
| el19tb | 8:52794de935b2 | 191 | // if(log->seperation != 0){ |
| el19tb | 8:52794de935b2 | 192 | chicken.x += 0.4; |
| el19tb | 8:52794de935b2 | 193 | // } |
| el19tb | 2:86cef2afa648 | 194 | } |
| el19tb | 2:86cef2afa648 | 195 | |
| el19tb | 3:648c9d5001be | 196 | //moves the chicken around the grid |
| el19tb | 3:648c9d5001be | 197 | void CrossyChicken::moveChicken(int xWay, int yWay){ |
| el19tb | 3:648c9d5001be | 198 | //increment the left side of the chicken by a value of the grid size |
| el19tb | 4:aae7f8d4ab78 | 199 | chicken.x += xWay * 4; |
| el19tb | 3:648c9d5001be | 200 | |
| el19tb | 3:648c9d5001be | 201 | //increment the top side by a value of grid sizw |
| el19tb | 4:aae7f8d4ab78 | 202 | chicken.y += yWay * 4; |
| el19tb | 3:648c9d5001be | 203 | |
| el19tb | 6:e285eaf8bdcd | 204 | chicken.left_side = chicken.x; |
| el19tb | 6:e285eaf8bdcd | 205 | chicken.right_side = grid + chicken.x; |
| el19tb | 6:e285eaf8bdcd | 206 | chicken.up = chicken.y; |
| el19tb | 6:e285eaf8bdcd | 207 | chicken.down = grid + chicken.y; |
| el19tb | 8:52794de935b2 | 208 | |
| el19tb | 3:648c9d5001be | 209 | //display the new state of the chicken |
| el19tb | 3:648c9d5001be | 210 | graphics.showChicken(); |
| el19tb | 3:648c9d5001be | 211 | |
| el19tb | 8:52794de935b2 | 212 | //wait_ms(30); |
| el19tb | 2:86cef2afa648 | 213 | } |
| el19tb | 2:86cef2afa648 | 214 | |
| el19tb | 5:6e3afee7eac3 | 215 | void CrossyChicken::moveCar(Car *car, int dir, int x) { |
| el19tb | 5:6e3afee7eac3 | 216 | car->speedMedium(dir, x); |
| el19tb | 4:aae7f8d4ab78 | 217 | |
| el19tb | 8:52794de935b2 | 218 | // check if car goes out of bounds |
| el19tb | 5:6e3afee7eac3 | 219 | if(car->vehicle.x > 84+grid){ |
| el19tb | 5:6e3afee7eac3 | 220 | car->vehicle.x = -4; |
| el19tb | 3:648c9d5001be | 221 | |
| el19tb | 5:6e3afee7eac3 | 222 | } else if(car->vehicle.x < -4){ |
| el19tb | 5:6e3afee7eac3 | 223 | car->vehicle.x = 84 + grid; |
| el19tb | 5:6e3afee7eac3 | 224 | } |
| el19tb | 5:6e3afee7eac3 | 225 | } |
| el19tb | 5:6e3afee7eac3 | 226 | |
| el19tb | 5:6e3afee7eac3 | 227 | // debug |
| el19tb | 8:52794de935b2 | 228 | void CrossyChicken::setCollision(Car *car){ |
| el19tb | 4:aae7f8d4ab78 | 229 | |
| el19tb | 5:6e3afee7eac3 | 230 | float other_bottom = car->vehicle.height + car->vehicle.y; |
| el19tb | 6:e285eaf8bdcd | 231 | |
| el19tb | 6:e285eaf8bdcd | 232 | |
| el19tb | 5:6e3afee7eac3 | 233 | if(!(chicken.up >= other_bottom || |
| el19tb | 5:6e3afee7eac3 | 234 | (chicken.right_side <= car->vehicle.x) || |
| el19tb | 5:6e3afee7eac3 | 235 | (chicken.down <= car->vehicle.y) || |
| el19tb | 5:6e3afee7eac3 | 236 | chicken.left_side >= (car->vehicle.width + car->vehicle.x))){ |
| el19tb | 5:6e3afee7eac3 | 237 | graphics.printTest(); |
| el19tb | 8:52794de935b2 | 238 | |
| el19tb | 8:52794de935b2 | 239 | if(chicken.y < screenH - grid*6){ |
| el19tb | 8:52794de935b2 | 240 | attach = true; |
| el19tb | 8:52794de935b2 | 241 | } else { |
| el19tb | 8:52794de935b2 | 242 | attach = false; |
| el19tb | 8:52794de935b2 | 243 | } |
| el19tb | 8:52794de935b2 | 244 | } |
| el19tb | 8:52794de935b2 | 245 | } |
| el19tb | 8:52794de935b2 | 246 | |
| el19tb | 8:52794de935b2 | 247 | bool CrossyChicken::returnCollision(Car* log){ |
| el19tb | 8:52794de935b2 | 248 | |
| el19tb | 8:52794de935b2 | 249 | float ob= log->vehicle.height + log->vehicle.y; |
| el19tb | 8:52794de935b2 | 250 | |
| el19tb | 8:52794de935b2 | 251 | if(!(chicken.up >= ob|| |
| el19tb | 8:52794de935b2 | 252 | (chicken.right_side <= log->vehicle.x) || |
| el19tb | 8:52794de935b2 | 253 | (chicken.down <= log->vehicle.y) || |
| el19tb | 8:52794de935b2 | 254 | chicken.left_side >= (log->vehicle.width + log->vehicle.x))){ |
| el19tb | 8:52794de935b2 | 255 | return true; |
| el19tb | 5:6e3afee7eac3 | 256 | } |
| el19tb | 5:6e3afee7eac3 | 257 | } |
| el19tb | 5:6e3afee7eac3 | 258 |