ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19tb

Dependencies:   mbed

Committer:
el19tb
Date:
Thu May 07 05:35:50 2020 +0000
Revision:
5:6e3afee7eac3
Parent:
4:aae7f8d4ab78
Child:
6:e285eaf8bdcd
added car direction, and reappearence of cars, made the code more structured

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 4:aae7f8d4ab78 20 Chicken chicken((84/2)-4/2, 48-4, 4);
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 5:6e3afee7eac3 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 5:6e3afee7eac3 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 5:6e3afee7eac3 81
el19tb 3:648c9d5001be 82 graphics.refresh();
el19tb 3:648c9d5001be 83 wait_ms(100);
el19tb 2:86cef2afa648 84 }
el19tb 2:86cef2afa648 85 }
el19tb 2:86cef2afa648 86
el19tb 2:86cef2afa648 87 //A moves right
el19tb 2:86cef2afa648 88 //X moves upward
el19tb 2:86cef2afa648 89 //B moves downward
el19tb 2:86cef2afa648 90 //Y moves left
el19tb 2:86cef2afa648 91 void CrossyChicken::process_input() {
el19tb 3:648c9d5001be 92 //determine the input
el19tb 3:648c9d5001be 93 if(gamepad.A_pressed()){
el19tb 3:648c9d5001be 94 moveChicken(1,0);
el19tb 3:648c9d5001be 95 } else if(gamepad.X_pressed()){
el19tb 3:648c9d5001be 96 moveChicken(0,-1);
el19tb 3:648c9d5001be 97 } else if(gamepad.B_pressed()){
el19tb 3:648c9d5001be 98 moveChicken(0,1);
el19tb 3:648c9d5001be 99 } else if(gamepad.Y_pressed()){
el19tb 3:648c9d5001be 100 moveChicken(-1,0);
el19tb 3:648c9d5001be 101 }
el19tb 2:86cef2afa648 102 }
el19tb 2:86cef2afa648 103
el19tb 3:648c9d5001be 104 //moves the chicken around the grid
el19tb 3:648c9d5001be 105 void CrossyChicken::moveChicken(int xWay, int yWay){
el19tb 3:648c9d5001be 106 //increment the left side of the chicken by a value of the grid size
el19tb 4:aae7f8d4ab78 107 chicken.x += xWay * 4;
el19tb 3:648c9d5001be 108
el19tb 3:648c9d5001be 109 //increment the top side by a value of grid sizw
el19tb 4:aae7f8d4ab78 110 chicken.y += yWay * 4;
el19tb 3:648c9d5001be 111
el19tb 3:648c9d5001be 112 //display the new state of the chicken
el19tb 3:648c9d5001be 113 graphics.showChicken();
el19tb 3:648c9d5001be 114
el19tb 3:648c9d5001be 115 wait_ms(30);
el19tb 2:86cef2afa648 116 }
el19tb 2:86cef2afa648 117
el19tb 5:6e3afee7eac3 118 void CrossyChicken::moveCar(Car *car, int dir, int x) {
el19tb 5:6e3afee7eac3 119 car->speedMedium(dir, x);
el19tb 4:aae7f8d4ab78 120
el19tb 5:6e3afee7eac3 121 // check if car goes out of bounds
el19tb 5:6e3afee7eac3 122 if(car->vehicle.x > 84+grid){
el19tb 5:6e3afee7eac3 123 car->vehicle.x = -4;
el19tb 3:648c9d5001be 124
el19tb 5:6e3afee7eac3 125 } else if(car->vehicle.x < -4){
el19tb 5:6e3afee7eac3 126 car->vehicle.x = 84 + grid;
el19tb 5:6e3afee7eac3 127 }
el19tb 5:6e3afee7eac3 128 }
el19tb 5:6e3afee7eac3 129
el19tb 5:6e3afee7eac3 130 // debug
el19tb 5:6e3afee7eac3 131 void CrossyChicken::collision(Car *car){
el19tb 4:aae7f8d4ab78 132
el19tb 5:6e3afee7eac3 133 float other_bottom = car->vehicle.height + car->vehicle.y;
el19tb 5:6e3afee7eac3 134 if(!(chicken.up >= other_bottom ||
el19tb 5:6e3afee7eac3 135 (chicken.right_side <= car->vehicle.x) ||
el19tb 5:6e3afee7eac3 136 (chicken.down <= car->vehicle.y) ||
el19tb 5:6e3afee7eac3 137 chicken.left_side >= (car->vehicle.width + car->vehicle.x))){
el19tb 5:6e3afee7eac3 138 graphics.printTest();
el19tb 5:6e3afee7eac3 139 }
el19tb 5:6e3afee7eac3 140 }
el19tb 5:6e3afee7eac3 141