James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Revision:
91:c01a736fb0d9
Parent:
89:806679ed11f2
Child:
111:4848c399fae0
--- a/Paddle/Paddle.cpp	Mon May 06 19:24:32 2019 +0000
+++ b/Paddle/Paddle.cpp	Mon May 06 22:57:04 2019 +0000
@@ -1,8 +1,7 @@
 #include "Paddle.h"
 
-FXOS8700CQ acc(I2C_SDA,I2C_SCL);
-
-// nothing doing in the constructor and destructor
+FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
+    
 Paddle::Paddle()
 {
 
@@ -15,13 +14,12 @@
 
 void Paddle::init(int y,int height,int width)
 {
-    _x = WIDTH/2 - width/2;  // x value on screen is fixed
-    _y = y;  // y depends on height of screen and height of paddle
+    _x = WIDTH/2 - width/2;  // initialised in the centre of the screen
+    _y = y;  // fixed at the bottom of the screen, initialised by _paddley in engine
     _height = height; // paddle height, defined at init
     _width = width; // paddle width, defined at init
     _sens = 0.5; // default sensitivity of tilt/joystick
     _speed = 4;  // default speed = _speed * _sens
-    _score = 0;  // start score from 0
     _lives = 6;  // number of lives = number of LEDs
     _tilt = false;  //used to choose between motion methods
 
@@ -29,45 +27,38 @@
 
 void Paddle::draw(N5110 &lcd)
 {
-    // draw paddle in screen buffer. 
-    lcd.drawRect(_x,_y,_width,_height,FILL_BLACK);
+    lcd.drawRect(_x,_y,_width,_height,FILL_BLACK);   // draw paddle at specified coordinates
 }
 
-void Paddle::update(Direction d,float mag)
+void Paddle::update(Direction d,float mag)   // update the paddle's properties
 {
-    acc.init();
-    
     if (_tilt == false) {
-        _speed = int(mag*10.0f);  // scale is arbitrary, could be changed in future
-    
-        // update y value depending on direction of movement
-        // North is decrement as origin is at the top-left so decreasing moves up
+        _speed = int(mag*10.0f);  // scale is arbitrary
+
+        // update x value depending on direction of movement
+        // West is decrement as origin is at the top-left so decreasing moves left
         if (d == W) {
-            _x-=_speed * _sens;
+            _x-=_speed * _sens;   // adjust the x coordinate accordingly. Sensitivity scales the speed of the paddle
         } else if (d == E) {
             _x+=_speed * _sens;
         }
-    
-        // check the y origin to ensure that the paddle doesn't go off screen
+
+        // censure that the paddle doesn't go off screen
         if (_x < 1) {
             _x = 1;
         }
         if (_x > WIDTH - _width - 1) {
             _x = WIDTH - _width - 1;
         }
-    }
-    else if (_tilt == true) {
-    
-        Data values = acc.get_values();
-        float roll_rad = atan2(values.ay,values.az);
-        _speed = int((roll_rad*(360/3.14159265))*0.3f);  // scale is arbitrary, could be changed in future
-    
-        // update y value depending on direction of movement
-        // North is decrement as origin is at the top-left so decreasing moves up
-        _x -= _speed * _sens;
+    } else if (_tilt == true) {
+        accelerometer.init();     // initialise the accelerometer
+        Data values = accelerometer.get_values();     // retrieve a struct of values
+        float roll_rad = atan2(values.ay,values.az);  // use values to calculate the roll angle in radians
+        _speed = int((roll_rad*(360/3.14159265))*0.3f);  // convert to degrees and scale
+        _x -= _speed * _sens;  // adjust the x coordinate accordingly
     }
 
-    // check the y origin to ensure that the paddle doesn't go off screen
+    // ensure that the paddle doesn't go off screen
     if (_x < 1) {
         _x = 1;
     }
@@ -76,46 +67,38 @@
     }
 }
 
-void Paddle::add_score()
-{
-    _score++;
-}
-int Paddle::get_score()
-{
-    return _score;
-}
-
-int Paddle::get_lives()
+int Paddle::get_lives()  // returns the integer _lives
 {
     return _lives;
 }
 
-void Paddle::lose_life()
+void Paddle::lose_life()  // decrements the member variable _lives
 {
     _lives--;
 }
 
-Vector2D Paddle::get_pos() {
+Vector2D Paddle::get_pos()  // returns the vector of the paddle's current position
+{
     Vector2D p = {_x,_y};
-    return p;    
+    return p;
 }
 
-void Paddle::set_tilt()
+void Paddle::set_tilt()    // sets the paddle motion option to tilt i.e. the member variable _tilt = true
 {
     _tilt = true;
 }
 
-void Paddle::set_joy()
+void Paddle::set_joy()     // sets the paddle motion option to joystick
 {
     _tilt = false;
 }
 
-void Paddle::recentre() 
+void Paddle::recentre()    // moves the paddle back to the centre of the screen (used after victory screen to restart)
 {
-    _x = WIDTH/2 - 15/2;  // x value on screen is fixed
+    _x = WIDTH/2 - PADDLE_WIDTH/2;  // centre of the screen
 }
 
-void Paddle::set_sens(float sens) 
+void Paddle::set_sens(float sens)  // sets the member variable _sens to an input
 {
     _sens = sens;
 }