Dependencies: mbed
Diff: Game/Game.cpp
- Revision:
- 15:5bf3f951d337
- Parent:
- 14:652fd55aebcf
- Child:
- 16:1f196a0e12be
--- a/Game/Game.cpp Mon May 18 14:54:29 2020 +0000 +++ b/Game/Game.cpp Mon May 18 18:03:43 2020 +0000 @@ -19,23 +19,30 @@ _shot_y = 0; } void Game::play() { - updateLives(); // lights up leds initially // if all lives are lost or start is pressed game ends while(_lives > 0 && !_pad->start_pressed()) { - _level = random_level_gen(10); + _level = random_level_gen(10); //generate random level printf("Level (in game) = %d \n", _level); - _ball->set_level(_level); - readInput(); - _ball->playShot(_shot_x, _shot_y); - _ball->isGoal(_level, _shot_x, _shot_y); + _ball->set_level(_level); //set level + readInput(); //take input + _ball->isGoal(_level, _shot_x, _shot_y); //check if goal + //goal scored if(_ball->get_goal()) { + _ball->playShot(_shot_x, _shot_y); _score++; + print_goal_message(1); } - else { _lives--; } - updateSpeed(); - updateLives(); + //goal missed + else { + // ball should not go through obstacle hence y value limited to 21 + // this is done after calculations to ensure accurate results + if(_shot_y >= 6) { _shot_y = 21; } + _ball->playShot(_shot_x, _shot_y); + _lives--; + print_goal_message(0); + } + updateSpeed(); } - printf("Reached end of play loop"); } void Game::readInput() { @@ -58,17 +65,15 @@ } void Game::updateLives() { + _pad->leds(0.0); switch(_lives){ case 0: - _pad->leds(0.0); break; case 1: - _pad->leds(0.0); _pad->led(1,1.0); //red leds only _pad->led(4,1.0); break; case 2: - _pad->leds_off(); _pad->led(1,1.0); //red and yellow leds _pad->led(2,1.0); _pad->led(4,1.0); @@ -81,6 +86,8 @@ error("Invalid Number of Lives"); break; } + // +1 life if score hits a multiple of 5 provided less than 3 lives left + if(_score % 5 == 0 && _score != 0 && _lives != 3) { _lives++; } printf("Lives = %d \n", _lives); } void Game::updateScore() { @@ -100,6 +107,7 @@ void Game::pointer_input() { updateScore(); + updateLives(); _ball->displayBackground(); _ball->init(); //draw aim pointer @@ -142,13 +150,28 @@ } void Game::convert_to_shot_y() { _shot_y = ((((int)_y_val) * (24)) / (20)); - if(_shot_y <= 3) {_shot_y = -6; } //shot too high + if(_shot_y <= 2) {_shot_y = -6; } //shot too high else if (_shot_y >= 19) {_shot_y = 19; }//shot is low, but must enter goal } int Game::random_level_gen(int limit) { - int number = (rand() % limit) + 1; //random level between 1-10 - return number; + int number = (rand() % limit) + 1; //random level between 1-10 + return number; +} +void Game::print_goal_message(int n) { + _ball->displayBackground(); + _lcd->drawRect(0,14,84,11,FILL_WHITE);//white background + _lcd->drawRect(0,14,84,11,FILL_TRANSPARENT);//white background + + switch(n) { + case 0: + _lcd->printString("MISS!",30,2); + break; + case 1: + _lcd->printString("GOAL!",30,2); + break; + } + _lcd->refresh(); + wait(1.5); } -