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-06
- Revision:
- 4:aae7f8d4ab78
- Parent:
- 3:648c9d5001be
- Child:
- 5:6e3afee7eac3
File content as of revision 4:aae7f8d4ab78:
#include "CrossyChicken.h"
#include "Menu.h"
#include "GraphicEngine.h"
#include <vector>
#include <stdio.h>
#include <cstddef>
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
std::vector<Car> cars;
//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();
for (int i = 0; i < 4; i++){
Car car;
cars.push_back(car);
}
//keep reading and processing user input
while(1) {
graphics.clear();
graphics.showChicken();
process_input();
moveCar();
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() {
std::vector<Car>::size_type t;
//for(int t = 0; t != cars.size(); t++){
//set the seperation of the first row
cars.at(0).setRow(4);
Car *carptr = &cars[0];
carptr->speedMedium(2);
//carptr->left_car.x += 0.2;
carptr->setSeperation(0);
graphics.showCar(carptr);
//}
}