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:
- 42:04e326dcf09b
- Parent:
- 41:6267c66953ca
- Child:
- 43:2cd1cfe07770
--- a/Frogger/Frogger.cpp Mon May 18 03:12:30 2020 +0000 +++ b/Frogger/Frogger.cpp Mon May 18 04:39:06 2020 +0000 @@ -16,6 +16,7 @@ this->level = levelptr; // one level object per engine this->frog = frog; // one frog object per run + current_level = 1; initializeParams(w, h, grid); // initialize the software parameters initializeEmbeddedSystem(); // initialize the hardware paramaters } @@ -39,15 +40,37 @@ void Frogger::initializeEmbeddedSystem() { //game setup - graphics.init(); // initialize the LCD object - graphics.contrast(); // set the contrast to 0.4 - graphics.backLightOn(); // turn on the backlight - gamepad.init(); // initialize the actual game pad + graphics->init(); // initialize the LCD object + graphics->contrast(); // set the contrast to 0.4 + graphics->backLightOn(); // turn on the backlight + + gamepad.init(); // initialize the actual embedded system +} + +//main function that starts the game +void Frogger::start() +{ + //keep reading and processing user input + while(1) { + graphics->clear(); // clear the lcd screen + + runCurrentLevel(); // add velocity to level vehicles and logs + + graphics->showFrog(); // display current position of frog + process_input(); // user controls the frog object + + checkCurrentLevelCollision(); // check if the frog is alive + checkFrogInWater(); // check if the frog is in the water level + + graphics->refresh(); // refresh the lcd screen + + wait_ms(75); // fpms + } } void Frogger::runCurrentLevel() { - switch (current_level) + switch (current_level) { case 1: level->levelOne(); @@ -58,26 +81,6 @@ } } -//main function that starts the game -void Frogger::start() -{ - //keep reading and processing user input - while(1) { - graphics.clear(); // clear the lcd screen - runCurrentLevel(); // add velocity to level vehicles and logs - - graphics.showFrog(); // display current position of frog - process_input(); // user controls the frog object - - checkCurrentLevelCollision(); // check if the frog is alive - checkFrogInWater(); // check if the frog is in the water level - - graphics.refresh(); // refresh the lcd screen - - wait_ms(70); // fpms - } -} - void Frogger::checkCurrentLevelCollision() { switch (current_level) @@ -91,265 +94,137 @@ } } -void Frogger::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 -// if the frog is back to the safe zone detatch also -// whenever the frog moves detach -//A moves right -//X moves upward -//B moves downward -//Y moves left void Frogger::process_input() { //determine the input /* make this a switch statement */ if(gamepad.A_pressed()){ - moveChicken(1,0); + moveFrog(1,0); level->frogOnLog = false; } else if(gamepad.X_pressed()){ - moveChicken(0,-1); + moveFrog(0,-1); level->frogOnLog = false; } else if(gamepad.B_pressed()){ - moveChicken(0,1); + moveFrog(0,1); level->frogOnLog = false; } else if(gamepad.Y_pressed()){ - moveChicken(-1,0); + moveFrog(-1,0); level->frogOnLog = false; } } -void Frogger::createMultipleSafetyLane() +//moves the frog around the grid +void Frogger::moveFrog(int xWay, int yWay) { - int min = 0; // start from the top, and draw till 0 - int max = grid_width; // draw 21 objects to the first lane - int row = 1; - int x_fac = 1; + //increment the left side of the chicken by a value of the grid size + frog->x += xWay * 4; + + //increment the top side by a value of grid sizw + frog->y += yWay * 4; - for(int x = 0; x < 2; x++){ - creatSafetyObject(min, max, row, x_fac); - row += 6; // increment the rows to the next lane - min += 22; // fill in the vector with 22 grid units - max += 22; - } + // change rect parameters + frog->left_side = frog->x; + frog->right_side = grid + frog->x; + frog->up = frog->y; + frog->down = grid + frog->y; +} + +void Frogger::createGoalPost() +{ + //graphics->drawGoalPost(); } -void Frogger::createSafetyObject(int min, int max, int row, int x_fac) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +void Frogger::createMultipleSafetyLane() { - int state = 1; - - for(int z = 0; z < 22; z++){ - switch(state) - { - case 1: - safety_lane.push_back(z/x_fac, row, 'K'); // safety lane so row 0 - state++; - break; - case 2: - safety_lane.push_back(z/x_fac, row, 'P'); // safety lane so row 0 - state--; // back to inital state - break; - } - } + +} + +void Frogger::createSafetyObject() +{ + } void Frogger::drawSafety() { - graphics.drawSafetyLanes(safety_lane); + //graphics.drawSafetyLanes(safety_lane); } 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 - int row = 1; - - for(int x = 0; x < 4; x++){ - createRoadLane(min, max, row); - row++; // increment the rows to the next lane - min += 22; // fill in the vector with 22 grid units - max += 22; - } + } // every level is going to have the same amount of blocks -void Frogger::createRoadLane(int min, int max, int row) +void Frogger::createRoadLane() { - std::vector<Background>::size_type it; - // fill the road objects - for(int it = min; it != max; it++){ - roads.push_back(it/row, row); // it is the x pos of the obj, row is the y pos - } } void Frogger::drawRoadObjects() { - graphics.getRoadObjects(roads); + //graphics.getRoadObjects(roads); } 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 - int row = 7; - int x_fac = 1; - - for(int x = 0; x < 3; x++){ - if(x == 1){ - createWaterLane(min, max, row, -0.6); // second row goes left - } else { - createWaterLane(min, max, row, 0.4); // rest of the rows go right - } - - row++; // increment the rows to the next lane - min += grid_width; // fill in the vector with 22 grid units - max += grid_width; - } + + } -void Frogger::createWaterLane(int min, int max, int row, float speed, int x_fac){ - int state = 1; - for(it = min; it < max; it++) - { - switch(state) - { - case 1: // initial state - water[it].push_back(it/x_fac, row, speed, 'F'); // first type of water - state++; // transition to next state - break; - case 2: - water[it].push_back(it/x_fac, row, speed, 'S'); // first type of water - state++; // transition to state 3 - break; - case 3: - water[it].push_back(it/x_fac, row, speed, 'T'); // first type of water - state = 1; // back to start state - break; - } - } +void Frogger::createWaterLane(){ + } // moves the water based on the velocity -void Frogger::moveWater(std::vector<Water>& water_lanes) +void Frogger::moveWater() { - for(unsigned int i = 0; i < water_lanes.size(); i++) - { - if(water_lanes(i).speed > 0){ - water_lanes(i).x += speed; - } else { - water_lanes(i).x -= speed; - } - } - - loopWater(water_lanes); + + //loopWater(water_lanes); } -void Frogger::loopWater(std::vector<Water>& water_lanes) +void Frogger::loopWater() { - for(unsigned int i = 0; i < water_lanes.size(); i++) - { - if(water_lanes(i).x > 84+grid){ - water_lanes(i).x = -4; - } else if(water_lanes[i].x < -4){ - water_lanes(i).x = 84 + grid; - } - } + } void Frogger::drawWater() { - graphics.drawWater(water); + // graphics.drawWater(water); } 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 Frogger::frogOnLog(Car *log) { - if(log->seperation != 0){ - chicken.x += 1.0; - } -} - -//moves the chicken around the grid -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; - - //increment the top side by a value of grid sizw - chicken.y += yWay * 4; - - chicken.left_side = chicken.x; - chicken.right_side = grid + chicken.x; - chicken.up = chicken.y; - chicken.down = grid + chicken.y; - + //graphics.drawGoal(84/2)-grid/2, 48-grid*11); } -void Frogger::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 Frogger::setCollision(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(); - - if(chicken.y < screenH - grid*6){ - attach = true; - } else { - attach = false; - } - } -} - -bool Frogger::returnCollision(Car* log){ - - float ob = log->vehicle.height + log->vehicle.y; - - if(!(chicken.up >= ob|| - (chicken.right_side <= log->vehicle.x) || - (chicken.down <= log->vehicle.y) || - chicken.left_side >= (log->vehicle.width + log->vehicle.x))){ - return true; - } -}