James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Mon May 06 22:57:04 2019 +0000
Revision:
91:c01a736fb0d9
Parent:
89:806679ed11f2
Child:
111:4848c399fae0
paddle commented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 0:7d4d2023ed9c 1 #include "Paddle.h"
jamesheavey 0:7d4d2023ed9c 2
jamesheavey 91:c01a736fb0d9 3 FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
jamesheavey 91:c01a736fb0d9 4
jamesheavey 0:7d4d2023ed9c 5 Paddle::Paddle()
jamesheavey 0:7d4d2023ed9c 6 {
jamesheavey 0:7d4d2023ed9c 7
jamesheavey 0:7d4d2023ed9c 8 }
jamesheavey 0:7d4d2023ed9c 9
jamesheavey 0:7d4d2023ed9c 10 Paddle::~Paddle()
jamesheavey 0:7d4d2023ed9c 11 {
jamesheavey 0:7d4d2023ed9c 12
jamesheavey 0:7d4d2023ed9c 13 }
jamesheavey 0:7d4d2023ed9c 14
jamesheavey 1:e18615d46071 15 void Paddle::init(int y,int height,int width)
jamesheavey 0:7d4d2023ed9c 16 {
jamesheavey 91:c01a736fb0d9 17 _x = WIDTH/2 - width/2; // initialised in the centre of the screen
jamesheavey 91:c01a736fb0d9 18 _y = y; // fixed at the bottom of the screen, initialised by _paddley in engine
jamesheavey 89:806679ed11f2 19 _height = height; // paddle height, defined at init
jamesheavey 89:806679ed11f2 20 _width = width; // paddle width, defined at init
jamesheavey 89:806679ed11f2 21 _sens = 0.5; // default sensitivity of tilt/joystick
jamesheavey 89:806679ed11f2 22 _speed = 4; // default speed = _speed * _sens
jamesheavey 89:806679ed11f2 23 _lives = 6; // number of lives = number of LEDs
jamesheavey 89:806679ed11f2 24 _tilt = false; //used to choose between motion methods
jamesheavey 0:7d4d2023ed9c 25
jamesheavey 0:7d4d2023ed9c 26 }
jamesheavey 0:7d4d2023ed9c 27
jamesheavey 0:7d4d2023ed9c 28 void Paddle::draw(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 29 {
jamesheavey 91:c01a736fb0d9 30 lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); // draw paddle at specified coordinates
jamesheavey 0:7d4d2023ed9c 31 }
jamesheavey 0:7d4d2023ed9c 32
jamesheavey 91:c01a736fb0d9 33 void Paddle::update(Direction d,float mag) // update the paddle's properties
jamesheavey 0:7d4d2023ed9c 34 {
jamesheavey 16:1761dfe801af 35 if (_tilt == false) {
jamesheavey 91:c01a736fb0d9 36 _speed = int(mag*10.0f); // scale is arbitrary
jamesheavey 91:c01a736fb0d9 37
jamesheavey 91:c01a736fb0d9 38 // update x value depending on direction of movement
jamesheavey 91:c01a736fb0d9 39 // West is decrement as origin is at the top-left so decreasing moves left
jamesheavey 16:1761dfe801af 40 if (d == W) {
jamesheavey 91:c01a736fb0d9 41 _x-=_speed * _sens; // adjust the x coordinate accordingly. Sensitivity scales the speed of the paddle
jamesheavey 16:1761dfe801af 42 } else if (d == E) {
jamesheavey 72:7254d2a8a1cd 43 _x+=_speed * _sens;
jamesheavey 16:1761dfe801af 44 }
jamesheavey 91:c01a736fb0d9 45
jamesheavey 91:c01a736fb0d9 46 // censure that the paddle doesn't go off screen
jamesheavey 16:1761dfe801af 47 if (_x < 1) {
jamesheavey 16:1761dfe801af 48 _x = 1;
jamesheavey 16:1761dfe801af 49 }
jamesheavey 16:1761dfe801af 50 if (_x > WIDTH - _width - 1) {
jamesheavey 16:1761dfe801af 51 _x = WIDTH - _width - 1;
jamesheavey 16:1761dfe801af 52 }
jamesheavey 91:c01a736fb0d9 53 } else if (_tilt == true) {
jamesheavey 91:c01a736fb0d9 54 accelerometer.init(); // initialise the accelerometer
jamesheavey 91:c01a736fb0d9 55 Data values = accelerometer.get_values(); // retrieve a struct of values
jamesheavey 91:c01a736fb0d9 56 float roll_rad = atan2(values.ay,values.az); // use values to calculate the roll angle in radians
jamesheavey 91:c01a736fb0d9 57 _speed = int((roll_rad*(360/3.14159265))*0.3f); // convert to degrees and scale
jamesheavey 91:c01a736fb0d9 58 _x -= _speed * _sens; // adjust the x coordinate accordingly
jamesheavey 16:1761dfe801af 59 }
jamesheavey 0:7d4d2023ed9c 60
jamesheavey 91:c01a736fb0d9 61 // ensure that the paddle doesn't go off screen
jamesheavey 0:7d4d2023ed9c 62 if (_x < 1) {
jamesheavey 0:7d4d2023ed9c 63 _x = 1;
jamesheavey 0:7d4d2023ed9c 64 }
jamesheavey 0:7d4d2023ed9c 65 if (_x > WIDTH - _width - 1) {
jamesheavey 0:7d4d2023ed9c 66 _x = WIDTH - _width - 1;
jamesheavey 0:7d4d2023ed9c 67 }
jamesheavey 0:7d4d2023ed9c 68 }
jamesheavey 0:7d4d2023ed9c 69
jamesheavey 91:c01a736fb0d9 70 int Paddle::get_lives() // returns the integer _lives
jamesheavey 9:f6f0f39538c7 71 {
jamesheavey 9:f6f0f39538c7 72 return _lives;
jamesheavey 9:f6f0f39538c7 73 }
jamesheavey 9:f6f0f39538c7 74
jamesheavey 91:c01a736fb0d9 75 void Paddle::lose_life() // decrements the member variable _lives
jamesheavey 9:f6f0f39538c7 76 {
jamesheavey 9:f6f0f39538c7 77 _lives--;
jamesheavey 9:f6f0f39538c7 78 }
jamesheavey 9:f6f0f39538c7 79
jamesheavey 91:c01a736fb0d9 80 Vector2D Paddle::get_pos() // returns the vector of the paddle's current position
jamesheavey 91:c01a736fb0d9 81 {
jamesheavey 0:7d4d2023ed9c 82 Vector2D p = {_x,_y};
jamesheavey 91:c01a736fb0d9 83 return p;
jamesheavey 16:1761dfe801af 84 }
jamesheavey 16:1761dfe801af 85
jamesheavey 91:c01a736fb0d9 86 void Paddle::set_tilt() // sets the paddle motion option to tilt i.e. the member variable _tilt = true
jamesheavey 16:1761dfe801af 87 {
jamesheavey 16:1761dfe801af 88 _tilt = true;
jamesheavey 16:1761dfe801af 89 }
jamesheavey 16:1761dfe801af 90
jamesheavey 91:c01a736fb0d9 91 void Paddle::set_joy() // sets the paddle motion option to joystick
jamesheavey 16:1761dfe801af 92 {
jamesheavey 16:1761dfe801af 93 _tilt = false;
jamesheavey 16:1761dfe801af 94 }
jamesheavey 69:db1354676fde 95
jamesheavey 91:c01a736fb0d9 96 void Paddle::recentre() // moves the paddle back to the centre of the screen (used after victory screen to restart)
jamesheavey 69:db1354676fde 97 {
jamesheavey 91:c01a736fb0d9 98 _x = WIDTH/2 - PADDLE_WIDTH/2; // centre of the screen
jamesheavey 69:db1354676fde 99 }
jamesheavey 72:7254d2a8a1cd 100
jamesheavey 91:c01a736fb0d9 101 void Paddle::set_sens(float sens) // sets the member variable _sens to an input
jamesheavey 72:7254d2a8a1cd 102 {
jamesheavey 72:7254d2a8a1cd 103 _sens = sens;
jamesheavey 72:7254d2a8a1cd 104 }