project 2645

Dependencies:   Gamepad N5110 mbed

Fork of gravitygame_abdulrahmanalhinai by Abdul Rahman Alhinai

Files at this revision

API Documentation at this revision

Comitter:
aia
Date:
Fri May 05 12:43:45 2017 +0000
Parent:
4:d349e5d847cf
Commit message:
finel

Changed in this revision

Ball.lib Show diff for this revision Revisions of this file
Ball/Ball.cpp Show annotated file Show diff for this revision Revisions of this file
Ball/Ball.h Show annotated file Show diff for this revision Revisions of this file
FXOS8700CQ/FXOS8700CQ.cpp Show annotated file Show diff for this revision Revisions of this file
FXOS8700CQ/FXOS8700CQ.h Show annotated file Show diff for this revision Revisions of this file
Paddle.lib Show diff for this revision Revisions of this file
PongEngine.lib Show diff for this revision Revisions of this file
PongEngine/PongEngine.cpp Show annotated file Show diff for this revision Revisions of this file
PongEngine/PongEngine.h Show annotated file Show diff for this revision Revisions of this file
basket/Paddle.cpp Show annotated file Show diff for this revision Revisions of this file
basket/Paddle.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r d349e5d847cf -r 8d882354e387 Ball.lib
--- a/Ball.lib	Sun Mar 05 23:19:55 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/users/eencae/code/Ball/#d3f87c8e4027
diff -r d349e5d847cf -r 8d882354e387 Ball/Ball.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ball/Ball.cpp	Fri May 05 12:43:45 2017 +0000
@@ -0,0 +1,169 @@
+/*
+@author abdul rahman alhinai 200904758
+@brief the game ball class where the ball speed and movement made
+@date may 2017
+*/
+#include "Ball.h"
+
+Ball::Ball()
+{
+
+}
+
+Ball::~Ball()
+{
+
+}
+
+void Ball::init(int size,int speed, FXOS8700CQ &device)
+{
+    _size = size;
+     speed =2;
+    _x = WIDTH/2  ;
+    _y = HEIGHT/2 ;
+    
+     _lvl = 0;    // to increas the speed of the ball
+ Data values = device.get_values();
+
+
+    
+     // print each struct member over serial to test and debug
+        printf("ax = %f ay = %f az = %f | mx = %f my = %f mz = %f\n"
+               ,values.ax, values.ay, values.az
+               ,values.mx, values.my, values.mz);
+        printf( "pitch angle = %f\n",values.pitch);
+        printf( "roll angle = %f\n",values.roll);
+
+    // 9 possibilities. Get angel values for pitch and rolll and set velocities accordingly
+    if ((values.pitch >= 30) &&//bottom right
+       (values.roll >= 30)) { 
+        _velocity.x = speed+_lvl  ;
+        _velocity.y = -speed-_lvl;
+    } else if ((values.pitch >= 30) && //top right
+       (values.roll <= -30)) {
+        _velocity.x = speed+_lvl;
+        _velocity.y = speed+_lvl;
+    } else if ((values.pitch >= 30))  {//right
+        _velocity.x = speed+_lvl;
+        _velocity.y = 0;
+    } else if ((values.pitch <= -30) && //bottom left
+       (values.roll >= 30)) {
+        _velocity.x = -speed-_lvl;
+        _velocity.y = -speed-_lvl;
+    }
+    else if( (values.pitch <= -30) && //top left 
+       (values.roll <= -30)) {
+        _velocity.x = -speed-_lvl;
+        _velocity.y = speed+_lvl;
+    }else if (values.pitch <= -30) { //left
+        _velocity.x = -speed-_lvl;
+        _velocity.y = 0;
+    }
+    else if (values.roll >= 30) { //bottom
+        _velocity.x = 0;
+        _velocity.y = -speed-_lvl;
+    }
+    else if (values.roll <= -30) { //top
+        _velocity.x = 0;
+        _velocity.y = speed+_lvl;
+    }
+    else {                       //stop
+        _velocity.x = 0;
+        _velocity.y = 0;
+    }
+}
+
+void Ball::draw(N5110 &lcd)
+{
+    lcd.drawCircle(_x,_y,_size,FILL_BLACK);
+    
+}
+
+void Ball::update(int speed, FXOS8700CQ &device)
+{
+      
+      Data values = device.get_values();
+      if ((values.pitch >= 30) &&
+       (values.roll >= 30)) {
+        _velocity.x = -speed -_lvl ;
+        _velocity.y = speed+_lvl;
+    } else if ((values.pitch >= 30) &&
+       (values.roll <= -30)) {
+        _velocity.x = -speed-_lvl;
+        _velocity.y = -speed-_lvl;
+    } else if ((values.pitch >= 30))  {
+        _velocity.x = -speed-_lvl;
+        _velocity.y = 0;
+    } else if ((values.pitch <= -30) &&
+       (values.roll >= 30)) {
+        _velocity.x = speed+_lvl;
+        _velocity.y = speed+_lvl;
+    }
+    else if( (values.pitch <= -30) &&
+       (values.roll <= -30)) {
+        _velocity.x = speed+_lvl;
+        _velocity.y = -speed-_lvl;
+    }else if (values.pitch <= -30) {
+        _velocity.x = speed+_lvl;
+        _velocity.y = 0;
+    }
+    else if (values.roll >= 30) {
+        _velocity.x = 0;
+        _velocity.y = speed+_lvl;
+    }
+    else if (values.roll <= -30) {
+        _velocity.x = 0;
+        _velocity.y = -speed-_lvl;
+    }
+    else {
+        _velocity.x = 0;
+        _velocity.y = 0;
+    }
+     
+    _x += _velocity.x;
+    _y += _velocity.y;
+}
+
+void Ball::add_lvl()
+{
+    _lvl++;
+}
+int Ball::get_lvl()
+{
+    return _lvl;
+}
+void Ball::reset_lvl()
+{
+    _lvl=0;
+}
+
+
+void Ball::set_velocity(Vector2D v)
+{
+    _velocity.x = v.x;
+    _velocity.y = v.y;
+}
+
+Vector2D Ball::get_velocity()
+{
+    Vector2D v = {_velocity.x,_velocity.y};
+    return v;
+}
+
+Vector2D Ball::get_pos()
+{
+    Vector2D p = {_x,_y};
+    return p;
+}
+
+
+void Ball::set_pos(Vector2D p)
+{
+    _x = p.x;
+    _y = p.y;
+}
+void Ball::reset_pos()
+{
+    _x = WIDTH/2  ;
+    _y = HEIGHT/2 ;
+}
\ No newline at end of file
diff -r d349e5d847cf -r 8d882354e387 Ball/Ball.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ball/Ball.h	Fri May 05 12:43:45 2017 +0000
@@ -0,0 +1,37 @@
+#ifndef BALL_H
+#define BALL_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "FXOS8700CQ.h"
+#include "Gamepad.h"
+#include "Paddle.h"
+
+class Ball
+{
+
+public:
+    Ball();
+    ~Ball();
+    void init(int size,int speed,FXOS8700CQ &device );
+    void draw(N5110 &lcd);
+    void update(int speed,FXOS8700CQ &device);
+    /// accessors and mutators
+    void set_velocity(Vector2D v);
+    Vector2D get_velocity();
+    Vector2D get_pos();
+    void set_pos(Vector2D p);
+    void reset_pos();
+    void reset_lvl();
+    void add_lvl();
+    int get_lvl();
+     
+private:
+
+    Vector2D _velocity;
+    int _size;
+    int _x;
+    int _y;
+    int _lvl;
+};
+#endif
\ No newline at end of file
diff -r d349e5d847cf -r 8d882354e387 FXOS8700CQ/FXOS8700CQ.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXOS8700CQ/FXOS8700CQ.cpp	Fri May 05 12:43:45 2017 +0000
@@ -0,0 +1,166 @@
+/* FXOS8700CQ Library
+
+Sample code from ELEC2645 - demonstrates how to create a library
+for the K64F on-board accelerometer and magnetometer
+
+(c) Craig A. Evans, University of Leeds, Jan 2017
+##reused form lab taskes
+
+*/
+
+#include "FXOS8700CQ.h"
+
+// constructor is called when the object is created - use it to set pins and frequency
+FXOS8700CQ::FXOS8700CQ(PinName sda, PinName scl)
+{
+    i2c = new I2C(sda,scl);        // create new I2C instance and initialise
+}
+
+// destructor is called when the object goes out of scope
+FXOS8700CQ::~FXOS8700CQ()
+{
+    delete i2c;        // free memory
+}
+
+// based on 13.4 in datasheet - 200 Hz hybrid mode (both acc and mag)
+void FXOS8700CQ::init()
+{
+    // i2c fast-mode - 10.1.1 data sheet
+    i2c->frequency(400000);       // I2C Fast Mode - 400kHz
+
+    // the device has an ID number so we check the value to ensure the correct
+    // drive is on the i2c bus
+    char data = read_byte_from_reg(FXOS8700CQ_WHO_AM_I);
+    if (data != FXOS8700CQ_WHO_AM_I_VAL) { // if correct ID not found, hang and flash error message
+        error("Incorrect ID!");
+    }
+
+    // write 0000 0000 = 0x00 to accelerometer control register 1 to place
+    // FXOS8700CQ into standby
+    // [7-1] = 0000 000
+    // [0]: active=0
+    data = 0x00;
+    send_byte_to_reg(data,FXOS8700CQ_CTRL_REG1);
+
+    // write 0001 1111 = 0x1F to magnetometer control register 1
+    // [7]: m_acal=0: auto calibration disabled
+    // [6]: m_rst=0: no one-shot magnetic reset
+    // [5]: m_ost=0: no one-shot magnetic measurement
+    // [4-2]: m_os=111=7: 8x oversampling (for 200Hz) to reduce magnetometer noise
+    // [1-0]: m_hms=11=3: select hybrid mode with accel and magnetometer active
+    data = 0x1F;
+    send_byte_to_reg(data,FXOS8700CQ_M_CTRL_REG1);
+
+    // write 0010 0000 = 0x20 to magnetometer control register 2
+    // [7]: reserved
+    // [6]: reserved
+    // [5]: hyb_autoinc_mode=1 to map the magnetometer registers to follow
+    // the accelerometer registers
+    // [4]: m_maxmin_dis=0 to retain default min/max latching even though not used
+    // [3]: m_maxmin_dis_ths=0
+    // [2]: m_maxmin_rst=0
+    // [1-0]: m_rst_cnt=00 to enable magnetic reset each cycle
+    data = 0x20;
+    send_byte_to_reg(data,FXOS8700CQ_M_CTRL_REG2);
+
+    // write 0000 0001= 0x01 to XYZ_DATA_CFG register
+    // [7]: reserved
+    // [6]: reserved
+    // [5]: reserved
+    // [4]: hpf_out=0
+    // [3]: reserved
+    // [2]: reserved
+    // [1-0]: fs=01 for accelerometer range of +/-4g range with 0.488mg/LSB
+    data = 0x01;
+    send_byte_to_reg(data,FXOS8700CQ_XYZ_DATA_CFG);
+
+    // write 0000 1101 = 0x0D to accelerometer control register 1
+    // [7-6]: aslp_rate=00
+    // [5-3]: dr=001 for 200Hz data rate (when in hybrid mode)
+    // [2]: lnoise=1 for low noise mode
+    // [1]: f_read=0 for normal 16 bit reads
+    // [0]: active=1 to take the part out of standby and enable sampling
+    data = 0x0D;
+    send_byte_to_reg(data,FXOS8700CQ_CTRL_REG1);
+
+}
+
+Data FXOS8700CQ::get_values()
+{
+    // 13 bytes - status plus 6 channels (2 bytes each)
+    // x,y,z for accelerometer and magnetometer
+    char data[FXOS8700CQ_READ_LEN];
+    read_bytes_from_reg(FXOS8700CQ_STATUS,FXOS8700CQ_READ_LEN,data);
+
+    // copy the 14 bit accelerometer byte data into 16 bit words
+    int acc_x = (int16_t)(((data[1] << 8) | data[2]))>> 2;
+    int acc_y = (int16_t)(((data[3] << 8) | data[4]))>> 2;
+    int acc_z = (int16_t)(((data[5] << 8) | data[6]))>> 2;
+
+    // copy the magnetometer byte data into 16 bit words
+    int mag_x = (int16_t) (data[7] << 8) | data[8];
+    int mag_y = (int16_t) (data[9] << 8) | data[10];
+    int mag_z = (int16_t) (data[11] << 8) | data[12];
+
+    Data values;  // struct to hold values
+
+    // 0.488 mg/LSB in 4 g mode (8.1 data sheet)
+    values.ax = 0.488e-3*acc_x;
+    values.ay = 0.488e-3*acc_y;
+    values.az = 0.488e-3*acc_z;
+
+    // the magnetometer sensitivity is fixed at 0.1 μT/LSB
+    values.mx = 0.1e-6*mag_x;
+    values.my = 0.1e-6*mag_y;
+    values.mz = 0.1e-6*mag_z;
+    values.pitch = atan2(values.ay,values.az)*180/PI ;
+    values.roll = atan2(-values.ax,sqrt(pow(values.ay,2)+pow(values.az,2)))*180/PI ;
+    return values;
+}
+
+void FXOS8700CQ::send_byte_to_reg(char byte,char reg)
+{
+    char data[2];
+    data[0] = reg;
+    data[1] = byte;
+    // send the register address, followed by the data
+    int nack = i2c->write(FXOS8700CQ_ADDR,data,2);
+    if (nack)
+        error("No acknowledgement received!");  // if we don't receive acknowledgement, send error message
+
+}
+
+// reads a byte from a specific register
+char FXOS8700CQ::read_byte_from_reg(char reg)
+{
+    int nack = i2c->write(FXOS8700CQ_ADDR,&reg,1,true);  // send the register address to the slave
+    // true as need to send repeated start condition (5.10.1 datasheet)
+    // http://www.i2c-bus.org/repeated-start-condition/
+    if (nack)
+        error("No acknowledgement received!");  // if we don't receive acknowledgement, send error message
+
+    char rx;
+    nack = i2c->read(FXOS8700CQ_ADDR,&rx,1);  // read a byte from the register and store in buffer
+    if (nack)
+        error("No acknowledgement received!");  // if we don't receive acknowledgement, send error message
+
+    return rx;
+}
+
+// reads a series of bytes, starting from a specific register
+void FXOS8700CQ::read_bytes_from_reg(char reg,int number_of_bytes,char bytes[])
+{
+    int nack = i2c->write(FXOS8700CQ_ADDR,&reg,1,true);  // send the slave write address and the configuration register address
+    // true as need to send repeated start condition (5.10.1 datasheet)
+    // http://www.i2c-bus.org/repeated-start-condition/
+
+    if (nack)
+        error("No acknowledgement received!");  // if we don't receive acknowledgement, send error message
+
+    nack = i2c->read(FXOS8700CQ_ADDR,bytes,number_of_bytes);  // read bytes
+    if (nack)
+        error("No acknowledgement received!");  // if we don't receive acknowledgement, send error message
+
+}
+
+
diff -r d349e5d847cf -r 8d882354e387 FXOS8700CQ/FXOS8700CQ.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXOS8700CQ/FXOS8700CQ.h	Fri May 05 12:43:45 2017 +0000
@@ -0,0 +1,61 @@
+/* FXOS8700CQ Library
+
+Sample code from ELEC2645 - demonstrates how to create a library
+for the K64F on-board accelerometer and magnetometer
+
+(c) Craig A. Evans, University of Leeds, Jan 2017
+
+*/
+
+#ifndef FXOS8700CQ_H
+#define FXOS8700CQ_H
+
+#include "mbed.h"
+#include "math.h"
+// mbed API uses 8-bit addresses so need to left-shift 7-bit addresses by 1
+#define FXOS8700CQ_ADDR   (0x1D << 1)    // for K64F board
+// values from 13.2 datasheet
+#define FXOS8700CQ_STATUS 0x00
+#define FXOS8700CQ_WHO_AM_I 0x0D
+#define FXOS8700CQ_XYZ_DATA_CFG 0x0E
+#define FXOS8700CQ_CTRL_REG1 0x2A
+#define FXOS8700CQ_M_CTRL_REG1 0x5B
+#define FXOS8700CQ_M_CTRL_REG2 0x5C
+#define FXOS8700CQ_WHO_AM_I_VAL 0xC7
+#define FXOS8700CQ_READ_LEN 13
+
+#define PI 3.14159265359f
+#define RAD2DEG 57.2957795131f
+
+struct Data {
+    float ax;
+    float ay;
+    float az;
+    float mx;
+    float my;
+    float mz;
+    float pitch;
+    float roll;
+};
+
+class FXOS8700CQ
+{
+
+public:
+    FXOS8700CQ(PinName sda, PinName scl);
+    ~FXOS8700CQ();
+    void init();
+    Data get_values();
+    Data get_pitch();
+    Data get_roll();
+    
+  
+private:
+    I2C* i2c;
+
+    void send_byte_to_reg(char byte,char reg);
+    char read_byte_from_reg(char reg);
+    void read_bytes_from_reg(char reg,int number_of_bytes,char bytes[]);
+};
+
+#endif
\ No newline at end of file
diff -r d349e5d847cf -r 8d882354e387 Paddle.lib
--- a/Paddle.lib	Sun Mar 05 23:19:55 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/users/eencae/code/Paddle/#c054c5e52370
diff -r d349e5d847cf -r 8d882354e387 PongEngine.lib
--- a/PongEngine.lib	Sun Mar 05 23:19:55 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/users/eencae/code/PongEngine/#c0a8f5d54c7b
diff -r d349e5d847cf -r 8d882354e387 PongEngine/PongEngine.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PongEngine/PongEngine.cpp	Fri May 05 12:43:45 2017 +0000
@@ -0,0 +1,184 @@
+/*
+@author abdul rahman alhinai 200904758
+@brief the game angine where the collectian of the classes work 
+@date5/5/2017
+*/
+#include "PongEngine.h"
+
+PongEngine::PongEngine()
+{
+
+}
+
+PongEngine::~PongEngine()
+{
+
+}
+
+void PongEngine::init(int paddle_radius,int ball_size,int speed, FXOS8700CQ &device )
+{
+    // initialise the game parameters
+    _paddle_radius = paddle_radius;
+    _ball_size = ball_size;
+    _speed = speed;
+   
+
+    // puts paddles and ball in middle
+    _p1.init(_paddle_radius);
+    
+    _ball.init(_ball_size,_speed,device);
+}
+
+void PongEngine::read_input(Gamepad &pad,FXOS8700CQ &device)
+{
+    Data values = device.get_values();  //to find data for the roll and pitch angels
+    
+}
+
+void PongEngine::draw(N5110 &lcd)
+{
+    // draw the elements in the LCD buffer
+    // pitch
+    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
+    
+    //score
+    print_scores(lcd);
+    // paddles/basket
+    _p1.draw(lcd);
+    // ball
+    _ball.draw(lcd);
+}
+
+void PongEngine::update(int speed,Gamepad &pad,N5110 &lcd,FXOS8700CQ &device)
+{
+ 
+    _ball.update( _speed, device);
+    Data values = device.get_values();  //to find data for the roll and pitch angels
+       
+    check_wall_collision(pad, lcd,device); ///lose the game if it has
+    
+    check_paddle_collisions(pad,lcd,device);// get a score if it has
+}
+
+void PongEngine::check_wall_collision(Gamepad &pad,N5110 &lcd,FXOS8700CQ &device)
+{
+    // read current ball attributes
+    Vector2D ball_pos = _ball.get_pos();
+    Vector2D ball_velocity = _ball.get_velocity();
+   
+    // check p1 positian
+    Vector2D p1_pos = _p1.get_pos();
+   
+    
+    Data values = device.get_values();  //to find data for the roll and pitch angels
+
+    // check if hit top wall
+    if (ball_pos.y <= 1) {  //  1 due to 1 pixel boundary
+         lcd.printString("   gameover   ",0,1);//game over string
+         lcd.refresh();
+        // audio feedback
+        pad.tone(100.0,0.1);
+        //reset the score,lvl and ball pos
+        _p1.reset_score();
+        _ball.reset_pos();
+        _ball.reset_lvl();
+        //print to test (debug)
+    printf( "top wall \n");
+        wait(1);
+        
+    }
+    // check if hit bottom wall
+    else if (ball_pos.y  >= (HEIGHT-1) ) { // bottom pixel is 47
+         lcd.printString("    gameover   ",0,1);//game over string
+         lcd.refresh();
+        // audio feedback
+        pad.tone(100.0,0.1);
+        //reset the score,lvl and ball pos
+        _p1.reset_score();
+        _ball.reset_pos();
+        _ball.reset_lvl();
+        printf( "bottom wall \n");//debug
+        
+         wait(1);
+        }
+        else if (ball_pos.x  <= 1) {
+         // if hit right wall
+         lcd.printString("     gameover    ",0,1);//game over string
+         lcd.refresh();
+        // audio feedback
+        pad.tone(100.0,0.1);
+        //reset the score,lvl and ball pos
+        _p1.reset_score();
+        _ball.reset_pos();
+        _ball.reset_lvl();
+        printf( "right wall \n");//debug
+         wait(1);
+        }
+        else if (ball_pos.x  >= WIDTH-1) {
+             // if hit left wall
+         lcd.printString("   gameover   ",0,1);//game over string
+         lcd.refresh();
+        // audio feedback
+        pad.tone(100.0,0.1);
+        //reset the score,lvl and ball pos
+        _p1.reset_score();
+        _ball.reset_pos();
+        _ball.reset_lvl();
+        printf( "left wall \n");//debug
+         wait(1);
+   
+    }
+
+    // update ball parameters
+    
+    _ball.set_velocity(ball_velocity);
+    
+    device.get_values();  //to find data for the roll and pitch angels
+}
+
+void PongEngine::check_paddle_collisions(Gamepad &pad,N5110 &lcd,FXOS8700CQ &device)
+{
+    // read current ball attributes
+    Vector2D ball_pos = _ball.get_pos();
+    Vector2D ball_velocity = _ball.get_velocity();
+         
+    // check p1 positian
+    Vector2D p1_pos = _p1.get_pos();
+    
+    //get data for roll and pitch angel
+    Data values = device.get_values();
+
+    // see if ball has hit the paddle/basket by pos
+    if (
+        (ball_pos.y ==  p1_pos.y)&&  (ball_pos.x ==  p1_pos.x)
+    ) {
+        // if it has, add score and lvl
+        _p1.add_score();
+        _ball.add_lvl();
+        // reset ball pos in the middel
+        _ball.reset_pos();
+        wait(0.5);
+         // print  to test (debug)
+        printf( "lvl = %2d \n",_lvl); 
+        printf( "pitch angle = %f\n",values.pitch);
+        printf( "roll angle = %f\n",values.roll);
+        // audio feedback
+        pad.tone(1000.0,0.1);
+    }
+    }
+
+
+void PongEngine::print_scores(N5110 &lcd)
+{
+    // get scores from paddles
+    int p1_score = _p1.get_score();
+   int ball_lvl = _ball.get_lvl();
+    //print to test
+    //printf( "lvl = %2d \n",_lvl); 
+
+    // print to LCD i
+    char buffer1[14];
+    sprintf(buffer1,"%2d",p1_score);
+    lcd.printString(buffer1,2,2);  
+  
+}
\ No newline at end of file
diff -r d349e5d847cf -r 8d882354e387 PongEngine/PongEngine.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PongEngine/PongEngine.h	Fri May 05 12:43:45 2017 +0000
@@ -0,0 +1,49 @@
+#ifndef PONGENGINE_H
+#define PONGENGINE_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "FXOS8700CQ.h"
+#include "Gamepad.h"
+#include "Ball.h"
+#include "Paddle.h"
+
+// gap from edge of screen
+#define GAP 2
+
+class PongEngine
+{
+
+public:
+    PongEngine();
+    ~PongEngine();
+
+    void init(int paddle_radius,int ball_size,int speed,FXOS8700CQ &device);
+    void read_input(Gamepad &pad,FXOS8700CQ &device);
+    void update(int speed, Gamepad &pad,N5110 &lcd, FXOS8700CQ &device);
+    void draw(N5110 &lcd);
+    
+private:
+
+    void check_wall_collision(Gamepad &pad,N5110 &lcd ,FXOS8700CQ &device);
+    void check_paddle_collisions(Gamepad &pad,N5110 &lcd,FXOS8700CQ &device);// paddle is the goal
+    void print_scores(N5110 &lcd);
+    
+    Paddle _p1;
+    
+     
+    int _paddle_radius;
+    int _ball_size;
+    int _speed;
+    int _lvl;
+    
+    // x positions of the paddles
+    int _p1x;
+    
+    
+    Ball _ball;
+    
+
+};
+
+#endif
\ No newline at end of file
diff -r d349e5d847cf -r 8d882354e387 basket/Paddle.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/basket/Paddle.cpp	Fri May 05 12:43:45 2017 +0000
@@ -0,0 +1,50 @@
+/*
+@author abdul rahman alhinai 200904758
+@brief the game paddel class where the where the basket and score date is stoerd 
+@date may 2017
+*/
+#include "Paddle.h"
+
+Paddle::Paddle()
+{
+
+}
+
+Paddle::~Paddle()
+{
+
+}
+
+void Paddle::init(int radius)
+{
+    _x = WIDTH-radius-2;  // x middel of screen
+    _y = HEIGHT-radius-2 ;  // y middel of screen
+    _radius = radius;
+    _speed = 1;  // default speed
+    _score = 0;
+
+}
+
+void Paddle::draw(N5110 &lcd)
+{
+    // draw paddle in screen buffer. 
+    lcd.drawCircle(_x,_y,_radius,FILL_TRANSPARENT);
+}
+
+
+void Paddle::add_score()
+{
+    _score++;
+}
+int Paddle::get_score()
+{
+    return _score;
+}
+void Paddle::reset_score()
+{
+    _score=0;
+}
+Vector2D Paddle::get_pos() {
+    Vector2D p = {_x,_y};
+    return p;    
+}
\ No newline at end of file
diff -r d349e5d847cf -r 8d882354e387 basket/Paddle.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/basket/Paddle.h	Fri May 05 12:43:45 2017 +0000
@@ -0,0 +1,31 @@
+#ifndef PADDLE_H
+#define PADDLE_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+
+class Paddle
+{
+public:
+
+    Paddle();
+    ~Paddle();
+    void init(int radius);
+    void draw(N5110 &lcd);
+    //void update(Direction d,float mag);
+    void add_score();
+    void reset_score();
+    int get_score();
+    Vector2D get_pos();
+
+private:
+
+    int _radius;
+    int _x;
+    int _y;
+    int _speed;
+    int _score;
+
+};
+#endif
\ No newline at end of file
diff -r d349e5d847cf -r 8d882354e387 main.cpp
--- a/main.cpp	Sun Mar 05 23:19:55 2017 +0000
+++ b/main.cpp	Fri May 05 12:43:45 2017 +0000
@@ -1,13 +1,19 @@
+/*
+@author abdul rahman alhinai 200904758
+@brief main folder of the game (gravity) 
+@date may 2017
+*/
 ///////// pre-processor directives ////////
 #include "mbed.h"
 #include "Gamepad.h"
+#include "FXOS8700CQ.h"
 #include "N5110.h"
 #include "PongEngine.h"
 
-#define PADDLE_WIDTH 2
-#define PADDLE_HEIGHT 8
-#define BALL_SIZE 2
-#define BALL_SPEED 3
+#define PADDLE_RADIUS 4
+#define BALL_SIZE 3
+#define BALL_SPEED 2
+#define BALL_LVL 0
 
 /////////////// structs /////////////////
 struct UserInput {
@@ -15,6 +21,7 @@
     float mag;
 };
 /////////////// objects ///////////////
+FXOS8700CQ device(I2C_SDA,I2C_SCL);
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad pad;
 PongEngine pong;
@@ -27,17 +34,19 @@
 int main()
 {
     int fps = 8;  // frames per second
-
+ device.init();
     init();
     welcome();
-    
-    render();  // draw initial frame 
-    wait(1.0f/fps);  
+    Data values = device.get_values();
+
+
+    render();  // draw initial frame
+    wait(1.0f/fps);
 
     // game loop - read input, update the game state and render the display
     while (1) {
-        pong.read_input(pad);
-        pong.update(pad);
+        pong.read_input(pad,device);
+        pong.update(BALL_SPEED,pad,lcd,device);
         render();
         wait(1.0f/fps);
     }
@@ -45,35 +54,36 @@
 
 void init()
 {
-    // need to initialise LCD and Gamepad 
+    // need to initialise LCD and Gamepad
     lcd.init();
     pad.init();
-     
+ device.init();
     // initialise the game
-    pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED);
+    pong.init(PADDLE_RADIUS,BALL_SIZE,BALL_SPEED,device);
 
 }
 
 void render()
 {
     // clear screen, re-draw and refresh
-    lcd.clear();  
+    lcd.clear();
     pong.draw(lcd);
     lcd.refresh();
 }
 
-void welcome() {
-    
-    lcd.printString("     Pong!    ",0,1);  
+void welcome()
+{
+
+    lcd.printString("    gravity    ",0,1);
     lcd.printString("  Press Start ",0,4);
     lcd.refresh();
-     
-    // wait flashing LEDs until start button is pressed 
+
+    // wait flashing LEDs until start button is pressed
     while ( pad.check_event(Gamepad::START_PRESSED) == false) {
         pad.leds_on();
         wait(0.1);
         pad.leds_off();
         wait(0.1);
     }
- 
+
 }
\ No newline at end of file