ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19tb

Dependencies:   mbed

Committer:
el19tb
Date:
Mon May 11 22:57:53 2020 +0000
Revision:
7:1dce07fd0867
Parent:
6:e285eaf8bdcd
Child:
8:52794de935b2
added log lanes and log functionality

Who changed what in which revision?

UserRevisionLine numberNew 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 3:648c9d5001be 32 //class that whill show objects
el19tb 3:648c9d5001be 33 GraphicEngine graphics(chickenptr);
el19tb 2:86cef2afa648 34
el19tb 2:86cef2afa648 35 //main function that starts the game
el19tb 3:648c9d5001be 36 void CrossyChicken::start(){
el19tb 2:86cef2afa648 37 //game setup
el19tb 3:648c9d5001be 38 graphics.init();
el19tb 3:648c9d5001be 39 graphics.contrast();
el19tb 3:648c9d5001be 40 graphics.backLightOn();
el19tb 2:86cef2afa648 41 gamepad.init();
el19tb 4:aae7f8d4ab78 42
el19tb 5:6e3afee7eac3 43 //first lane of left racers
el19tb 5:6e3afee7eac3 44 firstLane[0].setSeperation(0);
el19tb 7:1dce07fd0867 45 firstLane[1].setSeperation(20);
el19tb 5:6e3afee7eac3 46 firstLane[0].setRow(2);
el19tb 5:6e3afee7eac3 47 firstLane[1].setRow(2);
el19tb 4:aae7f8d4ab78 48
el19tb 5:6e3afee7eac3 49 secondLane[0].setSeperation(0);
el19tb 7:1dce07fd0867 50 secondLane[1].setSeperation(20);
el19tb 5:6e3afee7eac3 51 secondLane[0].setRow(3);
el19tb 5:6e3afee7eac3 52 secondLane[1].setRow(3);
el19tb 5:6e3afee7eac3 53
el19tb 7:1dce07fd0867 54 thirdLane[0].setSeperation(0);
el19tb 7:1dce07fd0867 55 thirdLane[1].setSeperation(20);
el19tb 7:1dce07fd0867 56 thirdLane[2].setSeperation(40);
el19tb 5:6e3afee7eac3 57 thirdLane[0].setRow(4);
el19tb 5:6e3afee7eac3 58 thirdLane[1].setRow(4);
el19tb 5:6e3afee7eac3 59 thirdLane[2].setRow(4);
el19tb 7:1dce07fd0867 60
el19tb 7:1dce07fd0867 61 fourthLane[0].setSeperation(10);
el19tb 7:1dce07fd0867 62 fourthLane[1].setSeperation(30);
el19tb 7:1dce07fd0867 63 fourthLane[2].setSeperation(50);
el19tb 7:1dce07fd0867 64 fourthLane[0].setRow(5);
el19tb 7:1dce07fd0867 65 fourthLane[1].setRow(5);
el19tb 7:1dce07fd0867 66 fourthLane[2].setRow(5);
el19tb 7:1dce07fd0867 67
el19tb 7:1dce07fd0867 68 // log lane 1
el19tb 7:1dce07fd0867 69 log1Lane[0].setSeperation(0);
el19tb 7:1dce07fd0867 70 log1Lane[1].setSeperation(20);
el19tb 7:1dce07fd0867 71 log1Lane[0].setRow(7);
el19tb 7:1dce07fd0867 72 log1Lane[1].setRow(7);
el19tb 7:1dce07fd0867 73
el19tb 7:1dce07fd0867 74 // log lane 2
el19tb 7:1dce07fd0867 75 lo[0].setSeperation(10);
el19tb 7:1dce07fd0867 76 lo[1].setSeperation(30);
el19tb 7:1dce07fd0867 77 lo[0].setRow(8);
el19tb 7:1dce07fd0867 78 lo[1].setRow(8);
el19tb 7:1dce07fd0867 79
el19tb 7:1dce07fd0867 80 // log lane 3
el19tb 7:1dce07fd0867 81 logger[0].setSeperation(0);
el19tb 7:1dce07fd0867 82 logger[1].setSeperation(20);
el19tb 7:1dce07fd0867 83 logger[0].setRow(9);
el19tb 7:1dce07fd0867 84 logger[1].setRow(9);
el19tb 7:1dce07fd0867 85
el19tb 5:6e3afee7eac3 86
el19tb 2:86cef2afa648 87 //keep reading and processing user input
el19tb 3:648c9d5001be 88 while(1) {
el19tb 3:648c9d5001be 89 graphics.clear();
el19tb 3:648c9d5001be 90 graphics.showChicken();
el19tb 4:aae7f8d4ab78 91 process_input();
el19tb 5:6e3afee7eac3 92
el19tb 7:1dce07fd0867 93 for(int i = 0; i < 2; i++){
el19tb 5:6e3afee7eac3 94 moveCar(&firstLane[i], 2, (-2)); // change x position (moving)
el19tb 5:6e3afee7eac3 95 }
el19tb 5:6e3afee7eac3 96
el19tb 5:6e3afee7eac3 97 for(int x = 0; x < 2; x++){
el19tb 5:6e3afee7eac3 98 moveCar(&secondLane[x], 1, 1);
el19tb 5:6e3afee7eac3 99 }
el19tb 5:6e3afee7eac3 100
el19tb 5:6e3afee7eac3 101 for(int t = 0; t < 3; t++){
el19tb 5:6e3afee7eac3 102 moveCar(&thirdLane[t], 2, 1);
el19tb 7:1dce07fd0867 103 }
el19tb 7:1dce07fd0867 104
el19tb 7:1dce07fd0867 105 for(int c = 0; c < 3; c++){
el19tb 7:1dce07fd0867 106 moveCar(&fourthLane[c], 2, 1);
el19tb 7:1dce07fd0867 107 }
el19tb 7:1dce07fd0867 108
el19tb 7:1dce07fd0867 109 for(int v = 0; v < 2; v++){
el19tb 7:1dce07fd0867 110 moveCar(&log1Lane[v], 2, 1);
el19tb 7:1dce07fd0867 111 }
el19tb 7:1dce07fd0867 112
el19tb 7:1dce07fd0867 113 for(int b = 0; b < 2; b++){
el19tb 7:1dce07fd0867 114 moveCar(&lo[b], 2, 1);
el19tb 7:1dce07fd0867 115 moveCar(&logger[b], 2, 2);
el19tb 5:6e3afee7eac3 116
el19tb 5:6e3afee7eac3 117 }
el19tb 5:6e3afee7eac3 118
el19tb 5:6e3afee7eac3 119 graphics.showCar(firstLane);
el19tb 5:6e3afee7eac3 120 graphics.showCar(secondLane);
el19tb 5:6e3afee7eac3 121 graphics.showCar(thirdLane);
el19tb 7:1dce07fd0867 122 graphics.showCar(fourthLane);
el19tb 7:1dce07fd0867 123 graphics.showCar(log1Lane);
el19tb 7:1dce07fd0867 124 graphics.showCar(lo);
el19tb 7:1dce07fd0867 125 //graphics.showCar(logger);
el19tb 7:1dce07fd0867 126
el19tb 6:e285eaf8bdcd 127 for(int t = 0; t < 3; t++){
el19tb 6:e285eaf8bdcd 128 collision(&thirdLane[t]);
el19tb 6:e285eaf8bdcd 129 }
el19tb 6:e285eaf8bdcd 130
el19tb 3:648c9d5001be 131 graphics.refresh();
el19tb 7:1dce07fd0867 132 wait_ms(50);
el19tb 2:86cef2afa648 133 }
el19tb 2:86cef2afa648 134 }
el19tb 2:86cef2afa648 135
el19tb 7:1dce07fd0867 136 // log intersects then frog.x = log[i].speed
el19tb 7:1dce07fd0867 137 // frog is attached function
el19tb 7:1dce07fd0867 138 // detach the frog when user goes up or down
el19tb 7:1dce07fd0867 139 // if the frog is back to the safe zone detatch also
el19tb 7:1dce07fd0867 140 // whenever the frog moves detach
el19tb 2:86cef2afa648 141 //A moves right
el19tb 2:86cef2afa648 142 //X moves upward
el19tb 2:86cef2afa648 143 //B moves downward
el19tb 2:86cef2afa648 144 //Y moves left
el19tb 2:86cef2afa648 145 void CrossyChicken::process_input() {
el19tb 3:648c9d5001be 146 //determine the input
el19tb 3:648c9d5001be 147 if(gamepad.A_pressed()){
el19tb 3:648c9d5001be 148 moveChicken(1,0);
el19tb 3:648c9d5001be 149 } else if(gamepad.X_pressed()){
el19tb 3:648c9d5001be 150 moveChicken(0,-1);
el19tb 3:648c9d5001be 151 } else if(gamepad.B_pressed()){
el19tb 3:648c9d5001be 152 moveChicken(0,1);
el19tb 3:648c9d5001be 153 } else if(gamepad.Y_pressed()){
el19tb 3:648c9d5001be 154 moveChicken(-1,0);
el19tb 3:648c9d5001be 155 }
el19tb 2:86cef2afa648 156 }
el19tb 2:86cef2afa648 157
el19tb 3:648c9d5001be 158 //moves the chicken around the grid
el19tb 3:648c9d5001be 159 void CrossyChicken::moveChicken(int xWay, int yWay){
el19tb 3:648c9d5001be 160 //increment the left side of the chicken by a value of the grid size
el19tb 4:aae7f8d4ab78 161 chicken.x += xWay * 4;
el19tb 3:648c9d5001be 162
el19tb 3:648c9d5001be 163 //increment the top side by a value of grid sizw
el19tb 4:aae7f8d4ab78 164 chicken.y += yWay * 4;
el19tb 3:648c9d5001be 165
el19tb 6:e285eaf8bdcd 166 chicken.left_side = chicken.x;
el19tb 6:e285eaf8bdcd 167 chicken.right_side = grid + chicken.x;
el19tb 6:e285eaf8bdcd 168 chicken.up = chicken.y;
el19tb 6:e285eaf8bdcd 169 chicken.down = grid + chicken.y;
el19tb 6:e285eaf8bdcd 170
el19tb 3:648c9d5001be 171 //display the new state of the chicken
el19tb 3:648c9d5001be 172 graphics.showChicken();
el19tb 3:648c9d5001be 173
el19tb 3:648c9d5001be 174 wait_ms(30);
el19tb 2:86cef2afa648 175 }
el19tb 2:86cef2afa648 176
el19tb 5:6e3afee7eac3 177 void CrossyChicken::moveCar(Car *car, int dir, int x) {
el19tb 5:6e3afee7eac3 178 car->speedMedium(dir, x);
el19tb 4:aae7f8d4ab78 179
el19tb 5:6e3afee7eac3 180 // check if car goes out of bounds
el19tb 5:6e3afee7eac3 181 if(car->vehicle.x > 84+grid){
el19tb 5:6e3afee7eac3 182 car->vehicle.x = -4;
el19tb 3:648c9d5001be 183
el19tb 5:6e3afee7eac3 184 } else if(car->vehicle.x < -4){
el19tb 5:6e3afee7eac3 185 car->vehicle.x = 84 + grid;
el19tb 5:6e3afee7eac3 186 }
el19tb 5:6e3afee7eac3 187 }
el19tb 5:6e3afee7eac3 188
el19tb 5:6e3afee7eac3 189 // debug
el19tb 5:6e3afee7eac3 190 void CrossyChicken::collision(Car *car){
el19tb 4:aae7f8d4ab78 191
el19tb 5:6e3afee7eac3 192 float other_bottom = car->vehicle.height + car->vehicle.y;
el19tb 6:e285eaf8bdcd 193
el19tb 6:e285eaf8bdcd 194
el19tb 5:6e3afee7eac3 195 if(!(chicken.up >= other_bottom ||
el19tb 5:6e3afee7eac3 196 (chicken.right_side <= car->vehicle.x) ||
el19tb 5:6e3afee7eac3 197 (chicken.down <= car->vehicle.y) ||
el19tb 5:6e3afee7eac3 198 chicken.left_side >= (car->vehicle.width + car->vehicle.x))){
el19tb 5:6e3afee7eac3 199 graphics.printTest();
el19tb 5:6e3afee7eac3 200 }
el19tb 5:6e3afee7eac3 201 }
el19tb 5:6e3afee7eac3 202