ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19tb

Dependencies:   mbed

Committer:
el19tb
Date:
Mon May 11 21:16:12 2020 +0000
Revision:
6:e285eaf8bdcd
Parent:
5:6e3afee7eac3
Child:
7:1dce07fd0867
fixed bugs that occurred with frog

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 3:648c9d5001be 27
el19tb 3:648c9d5001be 28 //class that whill show objects
el19tb 3:648c9d5001be 29 GraphicEngine graphics(chickenptr);
el19tb 2:86cef2afa648 30
el19tb 2:86cef2afa648 31 //main function that starts the game
el19tb 3:648c9d5001be 32 void CrossyChicken::start(){
el19tb 2:86cef2afa648 33 //game setup
el19tb 3:648c9d5001be 34 graphics.init();
el19tb 3:648c9d5001be 35 graphics.contrast();
el19tb 3:648c9d5001be 36 graphics.backLightOn();
el19tb 2:86cef2afa648 37 gamepad.init();
el19tb 4:aae7f8d4ab78 38
el19tb 5:6e3afee7eac3 39 //first lane of left racers
el19tb 5:6e3afee7eac3 40 firstLane[0].setSeperation(0);
el19tb 5:6e3afee7eac3 41 firstLane[1].setSeperation(40);
el19tb 5:6e3afee7eac3 42 firstLane[0].setRow(2);
el19tb 5:6e3afee7eac3 43 firstLane[1].setRow(2);
el19tb 4:aae7f8d4ab78 44
el19tb 5:6e3afee7eac3 45 secondLane[0].setSeperation(0);
el19tb 5:6e3afee7eac3 46 secondLane[1].setSeperation(30);
el19tb 5:6e3afee7eac3 47 secondLane[0].setRow(3);
el19tb 5:6e3afee7eac3 48 secondLane[1].setRow(3);
el19tb 5:6e3afee7eac3 49
el19tb 5:6e3afee7eac3 50 thirdLane[0].setSeperation(30);
el19tb 5:6e3afee7eac3 51 thirdLane[1].setSeperation(0);
el19tb 5:6e3afee7eac3 52 thirdLane[2].setSeperation(50);
el19tb 5:6e3afee7eac3 53 thirdLane[0].setRow(4);
el19tb 5:6e3afee7eac3 54 thirdLane[1].setRow(4);
el19tb 5:6e3afee7eac3 55 thirdLane[2].setRow(4);
el19tb 5:6e3afee7eac3 56
el19tb 2:86cef2afa648 57 //keep reading and processing user input
el19tb 3:648c9d5001be 58 while(1) {
el19tb 3:648c9d5001be 59 graphics.clear();
el19tb 3:648c9d5001be 60 graphics.showChicken();
el19tb 4:aae7f8d4ab78 61 process_input();
el19tb 5:6e3afee7eac3 62
el19tb 5:6e3afee7eac3 63 for(int i = 0; i < 2; i++){
el19tb 5:6e3afee7eac3 64 moveCar(&firstLane[i], 2, (-2)); // change x position (moving)
el19tb 6:e285eaf8bdcd 65 // collision(&firstLane[i]);
el19tb 5:6e3afee7eac3 66 }
el19tb 5:6e3afee7eac3 67
el19tb 5:6e3afee7eac3 68 for(int x = 0; x < 2; x++){
el19tb 5:6e3afee7eac3 69 moveCar(&secondLane[x], 1, 1);
el19tb 5:6e3afee7eac3 70 }
el19tb 5:6e3afee7eac3 71
el19tb 5:6e3afee7eac3 72 for(int t = 0; t < 3; t++){
el19tb 5:6e3afee7eac3 73 moveCar(&thirdLane[t], 2, 1);
el19tb 6:e285eaf8bdcd 74 //collision(&thirdLane[t]);
el19tb 5:6e3afee7eac3 75
el19tb 5:6e3afee7eac3 76 }
el19tb 5:6e3afee7eac3 77
el19tb 5:6e3afee7eac3 78 graphics.showCar(firstLane);
el19tb 5:6e3afee7eac3 79 graphics.showCar(secondLane);
el19tb 5:6e3afee7eac3 80 graphics.showCar(thirdLane);
el19tb 6:e285eaf8bdcd 81
el19tb 6:e285eaf8bdcd 82 for(int t = 0; t < 3; t++){
el19tb 6:e285eaf8bdcd 83 collision(&thirdLane[t]);
el19tb 6:e285eaf8bdcd 84 }
el19tb 6:e285eaf8bdcd 85
el19tb 3:648c9d5001be 86 graphics.refresh();
el19tb 3:648c9d5001be 87 wait_ms(100);
el19tb 2:86cef2afa648 88 }
el19tb 2:86cef2afa648 89 }
el19tb 2:86cef2afa648 90
el19tb 2:86cef2afa648 91 //A moves right
el19tb 2:86cef2afa648 92 //X moves upward
el19tb 2:86cef2afa648 93 //B moves downward
el19tb 2:86cef2afa648 94 //Y moves left
el19tb 2:86cef2afa648 95 void CrossyChicken::process_input() {
el19tb 3:648c9d5001be 96 //determine the input
el19tb 3:648c9d5001be 97 if(gamepad.A_pressed()){
el19tb 3:648c9d5001be 98 moveChicken(1,0);
el19tb 3:648c9d5001be 99 } else if(gamepad.X_pressed()){
el19tb 3:648c9d5001be 100 moveChicken(0,-1);
el19tb 3:648c9d5001be 101 } else if(gamepad.B_pressed()){
el19tb 3:648c9d5001be 102 moveChicken(0,1);
el19tb 3:648c9d5001be 103 } else if(gamepad.Y_pressed()){
el19tb 3:648c9d5001be 104 moveChicken(-1,0);
el19tb 3:648c9d5001be 105 }
el19tb 2:86cef2afa648 106 }
el19tb 2:86cef2afa648 107
el19tb 3:648c9d5001be 108 //moves the chicken around the grid
el19tb 3:648c9d5001be 109 void CrossyChicken::moveChicken(int xWay, int yWay){
el19tb 3:648c9d5001be 110 //increment the left side of the chicken by a value of the grid size
el19tb 4:aae7f8d4ab78 111 chicken.x += xWay * 4;
el19tb 3:648c9d5001be 112
el19tb 3:648c9d5001be 113 //increment the top side by a value of grid sizw
el19tb 4:aae7f8d4ab78 114 chicken.y += yWay * 4;
el19tb 3:648c9d5001be 115
el19tb 6:e285eaf8bdcd 116 chicken.left_side = chicken.x;
el19tb 6:e285eaf8bdcd 117 chicken.right_side = grid + chicken.x;
el19tb 6:e285eaf8bdcd 118 chicken.up = chicken.y;
el19tb 6:e285eaf8bdcd 119 chicken.down = grid + chicken.y;
el19tb 6:e285eaf8bdcd 120
el19tb 3:648c9d5001be 121 //display the new state of the chicken
el19tb 3:648c9d5001be 122 graphics.showChicken();
el19tb 3:648c9d5001be 123
el19tb 3:648c9d5001be 124 wait_ms(30);
el19tb 2:86cef2afa648 125 }
el19tb 2:86cef2afa648 126
el19tb 5:6e3afee7eac3 127 void CrossyChicken::moveCar(Car *car, int dir, int x) {
el19tb 5:6e3afee7eac3 128 car->speedMedium(dir, x);
el19tb 4:aae7f8d4ab78 129
el19tb 5:6e3afee7eac3 130 // check if car goes out of bounds
el19tb 5:6e3afee7eac3 131 if(car->vehicle.x > 84+grid){
el19tb 5:6e3afee7eac3 132 car->vehicle.x = -4;
el19tb 3:648c9d5001be 133
el19tb 5:6e3afee7eac3 134 } else if(car->vehicle.x < -4){
el19tb 5:6e3afee7eac3 135 car->vehicle.x = 84 + grid;
el19tb 5:6e3afee7eac3 136 }
el19tb 5:6e3afee7eac3 137 }
el19tb 5:6e3afee7eac3 138
el19tb 5:6e3afee7eac3 139 // debug
el19tb 5:6e3afee7eac3 140 void CrossyChicken::collision(Car *car){
el19tb 4:aae7f8d4ab78 141
el19tb 5:6e3afee7eac3 142 float other_bottom = car->vehicle.height + car->vehicle.y;
el19tb 6:e285eaf8bdcd 143
el19tb 6:e285eaf8bdcd 144
el19tb 5:6e3afee7eac3 145 if(!(chicken.up >= other_bottom ||
el19tb 5:6e3afee7eac3 146 (chicken.right_side <= car->vehicle.x) ||
el19tb 5:6e3afee7eac3 147 (chicken.down <= car->vehicle.y) ||
el19tb 5:6e3afee7eac3 148 chicken.left_side >= (car->vehicle.width + car->vehicle.x))){
el19tb 5:6e3afee7eac3 149 graphics.printTest();
el19tb 5:6e3afee7eac3 150 }
el19tb 5:6e3afee7eac3 151 }
el19tb 5:6e3afee7eac3 152