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
- Committer:
- el19tb
- Date:
- 2020-05-07
- Revision:
- 5:6e3afee7eac3
- Parent:
- 4:aae7f8d4ab78
- Child:
- 6:e285eaf8bdcd
File content as of revision 5:6e3afee7eac3:
#include "CrossyChicken.h"
#include "Menu.h"
#include "GraphicEngine.h"
#include <vector>
#include <stdio.h>
#include <cstddef>
// start from the top (height)
Gamepad gamepad;
//create three class: CAR LANE, SAFETY LANE, OBSTACLE LANE(water)
//random
//algoritmic way of generating lanes
//size of each each tile in the game
int grid = 4;
//make one object of chicken
//use this to move around the lcd
Chicken chicken((84/2)-4/2, 48-4, 4);
Chicken *chickenptr= &chicken;
//there will be multiple cars
Car firstLane[2];
Car secondLane[2];
Car thirdLane[3];
//class that whill show objects
GraphicEngine graphics(chickenptr);
//main function that starts the game
void CrossyChicken::start(){
//game setup
graphics.init();
graphics.contrast();
graphics.backLightOn();
gamepad.init();
//first lane of left racers
firstLane[0].setSeperation(0);
firstLane[1].setSeperation(40);
firstLane[0].setRow(2);
firstLane[1].setRow(2);
secondLane[0].setSeperation(0);
secondLane[1].setSeperation(30);
secondLane[0].setRow(3);
secondLane[1].setRow(3);
thirdLane[0].setSeperation(30);
thirdLane[1].setSeperation(0);
thirdLane[2].setSeperation(50);
thirdLane[0].setRow(4);
thirdLane[1].setRow(4);
thirdLane[2].setRow(4);
//keep reading and processing user input
while(1) {
graphics.clear();
graphics.showChicken();
process_input();
for(int i = 0; i < 2; i++){
moveCar(&firstLane[i], 2, (-2)); // change x position (moving)
collision(&firstLane[i]);
}
for(int x = 0; x < 2; x++){
moveCar(&secondLane[x], 1, 1);
}
for(int t = 0; t < 3; t++){
moveCar(&thirdLane[t], 2, 1);
collision(&thirdLane[t]);
}
graphics.showCar(firstLane);
graphics.showCar(secondLane);
graphics.showCar(thirdLane);
graphics.refresh();
wait_ms(100);
}
}
//A moves right
//X moves upward
//B moves downward
//Y moves left
void CrossyChicken::process_input() {
//determine the input
if(gamepad.A_pressed()){
moveChicken(1,0);
} else if(gamepad.X_pressed()){
moveChicken(0,-1);
} else if(gamepad.B_pressed()){
moveChicken(0,1);
} else if(gamepad.Y_pressed()){
moveChicken(-1,0);
}
}
//moves the chicken around the grid
void CrossyChicken::moveChicken(int xWay, int yWay){
//increment the left side of the chicken by a value of the grid size
chicken.x += xWay * 4;
//increment the top side by a value of grid sizw
chicken.y += yWay * 4;
//display the new state of the chicken
graphics.showChicken();
wait_ms(30);
}
void CrossyChicken::moveCar(Car *car, int dir, int x) {
car->speedMedium(dir, x);
// check if car goes out of bounds
if(car->vehicle.x > 84+grid){
car->vehicle.x = -4;
} else if(car->vehicle.x < -4){
car->vehicle.x = 84 + grid;
}
}
// debug
void CrossyChicken::collision(Car *car){
float other_bottom = car->vehicle.height + car->vehicle.y;
if(!(chicken.up >= other_bottom ||
(chicken.right_side <= car->vehicle.x) ||
(chicken.down <= car->vehicle.y) ||
chicken.left_side >= (car->vehicle.width + car->vehicle.x))){
graphics.printTest();
}
}