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
Diff: Frogger/Frogger.cpp
- Revision:
- 37:65c9e5a65738
- Parent:
- 35:db257b7bdefc
- Child:
- 41:6267c66953ca
--- a/Frogger/Frogger.cpp Sun May 17 22:38:20 2020 +0000 +++ b/Frogger/Frogger.cpp Sun May 17 23:20:23 2020 +0000 @@ -1,46 +1,41 @@ -#include "Frogger +#include "Frogger.h" #include "Menu.h" #include "GraphicEngine.h" #include <vector> #include <stdio.h> #include <cstddef> -//there will be multiple cars -Car firstLane[2]; -Car secondLane[2]; -Car thirdLane[3]; -Car fourthLane[3]; -Car log1Lane[2]; -Car lo[2]; -Car logger[2]; -Car vop[2]; - - bool frogLog = false; bool frogDie = false; // whenever the frog is above safety lane in the middle bool attach = false; -Frogger::Frogger(GraphicEngine *engine, Level *level, Frog *frog) +// level one initiailization has already been done +Frogger::Frogger(GraphicEngine *engine, Level *levelptr, Frog *frog, int grid, int w, int h) { this->graphics = engine; // get the renderer in place - this->level = level; // one level object per engine + this->level = levelptr; // one level object per engine this->frog = frog; // one frog object per run - initializeParams(); // initialize the software parameters + initializeParams(w, h, grid); // initialize the software parameters initializeEmbeddedSystem(); // initialize the hardware paramaters } -void Frogger::initializeParams() +void Frogger::initializeParams(int w, int h, int grid) { // screen sizes - screenW = 84; // width - screenH = 48; // height + lcd_w = w; // width + lcd_h = h; // height // grid values grid = 4; // size of game grid system grid_width = 22; // size of screen width in grid units } +void Frogger::initializeLevelTwo() +{ + level->setupLevelTwo(); +} + void Frogger::initializeEmbeddedSystem() { //game setup @@ -50,85 +45,59 @@ gamepad.init(); // initialize the actual game pad } +void Frogger::runCurrentLevel() +{ + switch (current_level) + { + case 1: + level->levelOne(); + break; + case 2: + level->levelTwo(); + break; + } +} + //main function that starts the game -void CrossyChicken::start(){ - - row_number = 1; - +void Frogger::start() +{ //keep reading and processing user input while(1) { - graphics.clear(); - - for(int i = 0; i < 2; i++){ - moveCar(&firstLane[i], 2, (-2)); // change x position (moving) - } - - for(int x = 0; x < 2; x++){ - moveCar(&secondLane[x], 1, 1); - } - - for(int t = 0; t < 3; t++){ - moveCar(&thirdLane[t], 2, 1); - } - - for(int c = 0; c < 3; c++){ - moveCar(&fourthLane[c], 2, 1); - } - - for(int v = 0; v < 2; v++){ - moveCar(&log1Lane[v], 1, 1); - } - - for(int b = 0; b < 2; b++){ - moveCar(&lo[b], 2, 1); - // moveCar(&logger[b], 2, 2); - moveCar(&vop[b], 1, 2); - - } - - graphics.showCar(firstLane); - graphics.showCar(secondLane); - graphics.showCar(thirdLane); - graphics.showCar(fourthLane); - graphics.showCar(log1Lane); - graphics.showCar(lo); - graphics.showCar(logger); - graphics.showCar(vop); + graphics.clear(); // clear the lcd screen + runCurrentLevel(); graphics.showChicken(); - process_input(); + process_input(); // user controls the frog object // now when the chicken is above the safety // lane we need to ensure that it only can go on logs - if(chicken.y < screenH - grid*6){ - frogDie = true; // frog can die if it is in water - - //if it is not in a log - for(int x = 0; x < 2; x++){ - setCollision(&log1Lane[x]); - setCollision(&lo[x]); - setCollision(&logger[x]); - setCollision(&vop[x]); - - if(attach){ - frogOnLog(&log1Lane[x]); - frogOnLog(&lo[x]); - frogOnLog(&logger[x]); - frogOnLog(&vop[x]); - - } - } - - if(!attach){ - graphics.print(); - } - } + graphics.refresh(); wait_ms(70); } } +void checkIfFrogIsInWater() +{ + if(chicken.y < screenH - grid*6){ + frogDie = true; // frog can die if it is in water + + //if it is not in a log + for(int x = 0; x < 2; x++){ + + if(attach){ + + + } + } + + if(!attach){ + graphics.print(); + } + } +} + // log intersects then frog.x = log[i].speed // frog is attached function // detach the frog when user goes up or down @@ -138,7 +107,7 @@ //X moves upward //B moves downward //Y moves left -void CrossyChicken::process_input() { +void Frogger::process_input() { //determine the input /* make this a switch statement */ if(gamepad.A_pressed()){ @@ -156,7 +125,7 @@ } } -void CrossyChicken::createMultipleSafetyLane() +void Frogger::createMultipleSafetyLane() { int min = 0; // start from the top, and draw till 0 int max = grid_width; // draw 21 objects to the first lane @@ -171,7 +140,7 @@ } } -void CrossyChicken::createSafetyObject(int min, int max, int row, int x_fac) +void Frogger::createSafetyObject(int min, int max, int row, int x_fac) { int state = 1; @@ -190,13 +159,13 @@ } } -void CrossyChicken::drawSafety() +void Frogger::drawSafety() { graphics.drawSafetyLanes(safety_lane); } -void CrossyChicken::createMultipleRoadLane() +void Frogger::createMultipleRoadLane() { int min = 0; // start from the top, and draw till 0 int max = grid_width; // draw 21 objects to the first lane @@ -211,7 +180,7 @@ } // every level is going to have the same amount of blocks -void CrossyChicken::createRoadLane(int min, int max, int row) +void Frogger::createRoadLane(int min, int max, int row) { std::vector<Background>::size_type it; @@ -221,12 +190,12 @@ } } -void CrossyChicken::drawRoadObjects() +void Frogger::drawRoadObjects() { graphics.getRoadObjects(roads); } -void CrossyChicken::createMultipleLanesWater() +void Frogger::createMultipleLanesWater() { int min = 0; // start from the top, and draw till 0 int max = grid_width; // draw 21 objects to the first lane @@ -246,7 +215,7 @@ } } -void CrossyChicken::createWaterLane(int min, int max, int row, float speed, int x_fac){ +void Frogger::createWaterLane(int min, int max, int row, float speed, int x_fac){ int state = 1; for(it = min; it < max; it++) { @@ -269,7 +238,7 @@ } // moves the water based on the velocity -void CrossyChicken::moveWater(std::vector<Water>& water_lanes) +void Frogger::moveWater(std::vector<Water>& water_lanes) { for(unsigned int i = 0; i < water_lanes.size(); i++) { @@ -284,7 +253,7 @@ } -void CrossyChicken::loopWater(std::vector<Water>& water_lanes) +void Frogger::loopWater(std::vector<Water>& water_lanes) { for(unsigned int i = 0; i < water_lanes.size(); i++) { @@ -296,26 +265,26 @@ } } -void CrossyChicken::drawWater() +void Frogger::drawWater() { graphics.drawWater(water); } -void CrossyChicken::drawEndPost() +void Frogger::drawEndPost() { graphics.drawGoal(84/2)-grid/2, 48-grid*11); } // make the frog move same speed as log // if the frog moves then detach -void CrossyChicken::frogOnLog(Car *log) { +void Frogger::frogOnLog(Car *log) { if(log->seperation != 0){ chicken.x += 1.0; } } //moves the chicken around the grid -void CrossyChicken::moveChicken(int xWay, int yWay){ +void Frogger::moveChicken(int xWay, int yWay){ //increment the left side of the chicken by a value of the grid size chicken.x += xWay * 4; @@ -333,7 +302,7 @@ //wait_ms(30); } -void CrossyChicken::moveCar(Car *car, int dir, int x) { +void Frogger::moveCar(Car *car, int dir, int x) { car->speedMedium(dir, x); // check if car goes out of bounds @@ -346,7 +315,7 @@ } // debug -void CrossyChicken::setCollision(Car *car){ +void Frogger::setCollision(Car *car){ float other_bottom = car->vehicle.height + car->vehicle.y; @@ -365,7 +334,7 @@ } } -bool CrossyChicken::returnCollision(Car* log){ +bool Frogger::returnCollision(Car* log){ float ob = log->vehicle.height + log->vehicle.y;