Dependencies:   mbed

Revision:
9:e6566d09f087
Parent:
8:5ede90f99a27
Child:
12:ca7329a41fc5
--- a/Game/Game.cpp	Sun May 17 17:59:30 2020 +0000
+++ b/Game/Game.cpp	Sun May 17 23:00:04 2020 +0000
@@ -1,111 +1,134 @@
 #include "Game.h"
 
-Game::Game() {}
+Game::Game(N5110 &lcd, Gamepad &pad, Ball &ball) {
+    _lcd = &lcd;
+    _pad = &pad;
+    _ball = &ball;
+}
 Game::~Game() {}
 
 void Game::init() {
-    _level = 1;
     _is_goal = false;
     _lives = 3;
     _score = 0;
-    _speed = 1;
-    _x_val = WIDTH/2;
-    _y_val = 0;
+    _speed = 0.30f;
+    _x_val = 42.00f;
+    _y_val = 0.00f;
     _shot_x = 0;
     _shot_y = 0;
 }
-void Game::play(Gamepad &pad, N5110 &lcd, Ball &ball) {
-    init();
-    updateLives(pad);
-    readInput(pad, lcd, ball);
-    ball.playShot(_shot_x, _shot_y, lcd);   
+void Game::play() {
+    updateLives();
+    readInput();
+    _ball->playShot(_shot_x, _shot_y);
+    _score++;
+    updateSpeed();   
 }
 
-void Game::readInput(Gamepad &pad, N5110 &lcd, Ball &ball) {
-    while(!pad.A_pressed()) {
-        pointer_input( pad, lcd, ball);
+void Game::readInput() {
+    _x_val = 42; //reset x_val
+    _y_val = 0; //reset y_val
+    while(!_pad->A_pressed()) {
+        pointer_input();
     }
-    while(!pad.B_pressed()) {
-        power_meter_input( pad, lcd, ball);
+    wait(0.1);
+    while(!_pad->B_pressed()) {
+        power_meter_input();
     }
+    wait(0.1);
     convert_to_shot_x();
     convert_to_shot_y();
-    printf("x val = %d \n",_x_val);
+    printf("x val = %.2f \n",_x_val);
     printf("shot_x val = %d \n",_shot_x);
-    printf("y val = %d \n",_y_val);
+    printf("y val = %.2f \n",_y_val);
     printf("shot_y val = %d \n",_shot_y);
     
 }
-void Game::updateLives(Gamepad &pad) {
+void Game::updateLives() {
     switch(_lives){
         case 1:
-            pad.leds(0.0);
-            pad.led(1,1.0); //red leds only
-            pad.led(4,1.0);
+            _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);
-            pad.led(5,1.0);
+            _pad->leds_off();
+            _pad->led(1,1.0); //red and yellow leds
+            _pad->led(2,1.0);
+            _pad->led(4,1.0);
+            _pad->led(5,1.0);
             break; 
         case 3:
-            pad.leds(1.0);
+            _pad->leds(1.0);
             break; 
         default:
             error("Invalid Number of Lives");  
             break; 
     }
 }
-void Game::updateScore(N5110 &lcd) {
+void Game::updateScore() {
     char buffer[12];
     sprintf(buffer,"%d",_score);
-    lcd.printString(buffer,2,5);
+    _lcd->printString(buffer,2,5);
+}
+void Game::updateSpeed() {
+    if(abs(_speed) <= 2) { // max speed = 1
+        if(_score % 3 == 0) {
+            _speed = abs(_speed) + 0.20f;        
+        }
+    }
+    printf("speed = %.2f \n", _speed);
+    printf("score = %d \n", _score);
 }
 
-void Game::pointer_input(Gamepad &pad, N5110 &lcd, Ball &ball) {
-    updateScore(lcd);
-    ball.display_background(lcd);
-    ball.init(lcd);
+void Game::pointer_input() {
+    updateScore();
+    _ball->display_background();
+    _ball->init();
     //draw aim pointer
-    lcd.drawLine(WIDTH / 2 - 5, 41, _x_val, HEIGHT / 2 + 7, 1);//left side
-    lcd.drawLine(WIDTH / 2 + 5, 41, _x_val, HEIGHT / 2 + 7, 1);//right side
+    _lcd->drawLine(WIDTH / 2 - 5, 41, _x_val, HEIGHT / 2 + 7, 1);//left side
+    _lcd->drawLine(WIDTH / 2 + 5, 41, _x_val, HEIGHT / 2 + 7, 1);//right side
     _x_val += _speed;
-    //printf("speed = %d \n", _speed);
-    if(_x_val >= 72) { _speed = (-1 * _speed); }
-    //pointer points out of screen i.e. 72=84 therefore, switch direction 
+    //printf("dir = %d \n", _speed);
+    if((int)_x_val >= 70) { _speed = (-1 * _speed); }
+    //pointer points out of screen i.e. 70=84 therefore, switch direction 
     //value found through trial and error using print statement
-    else if(_x_val <= 8) { _speed = abs(_speed); }
-    lcd.refresh();
-    lcd.clear();
+    else if((int)_x_val <= 12) { _speed = abs(_speed); }
+    _lcd->refresh();
+    _lcd->clear();
     wait(0.01);
 }
-void Game::power_meter_input(Gamepad &pad, N5110 &lcd, Ball &ball) {
-    updateScore(lcd);
+void Game::power_meter_input() {
+    updateScore();
     //keep direction of pointer
-    ball.display_background(lcd);
-    ball.init(lcd);
-    lcd.drawLine(WIDTH / 2 - 5, 41, _x_val, HEIGHT / 2 + 7, 1);
-    lcd.drawLine(WIDTH / 2 + 5, 41, _x_val, HEIGHT / 2 + 7, 1);
+    _ball->display_background();
+    _ball->init();
+    _lcd->drawLine(WIDTH / 2 - 5, 41, _x_val, HEIGHT / 2 + 7, 1);
+    _lcd->drawLine(WIDTH / 2 + 5, 41, _x_val, HEIGHT / 2 + 7, 1);
     //fill and empty power meter
-    lcd.drawRect(77,27,6,_y_val,FILL_BLACK);
-    _y_val += _speed;
-    if(_y_val >= 20) { _speed = (-1 * _speed); } //power meter full
-    else if(_y_val <= 0) { _speed = abs(_speed); } // power meter empty
-    lcd.refresh();
-    lcd.clear();
+    _lcd->drawRect(77,27,6,_y_val,FILL_BLACK);
+    _y_val += _speed/2;
+    if((int)_y_val >= 20) { _speed = (-1 * _speed); } //power meter full
+    else if((int)_y_val <= 0) { _speed = abs(_speed); } // power meter empty
+    _lcd->refresh();
+    _lcd->clear();
     wait(0.01);
 }
 void Game::convert_to_shot_x() {
-    //convert from range 8-72 (range of pointer) to 0-84 (range of screen)
-    _shot_x = (((_x_val - 8) * (84)) / (72 - 8));
+    //convert from range 12-70 (range of pointer) to 0-84 (range of screen)
+    _shot_x = ((((int)_x_val - 12) * (84)) / (70 - 12));
     //ball misses goal completely
-    if(_shot_x <=7) { _shot_x = -30;}
-    if(_shot_x >=77) { _shot_x = 114;}
+    if(_shot_x <=7) { _shot_x = -20;}
+    else if(_shot_x >=77) { _shot_x = 104;}
 }
 void Game::convert_to_shot_y() {
-    _shot_y = (((_y_val) * (24)) / (20));
+    _shot_y = ((((int)_y_val) * (24)) / (20));
+    if(_shot_y <= 3) {_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;
 }