James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 09:52:35 2019 +0000
Revision:
136:04a2724f90cf
Parent:
114:280903dd7e06
small white space edit

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 136:04a2724f90cf 28
jamesheavey 0:7d4d2023ed9c 29 void Paddle::draw(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 30 {
jamesheavey 91:c01a736fb0d9 31 lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); // draw paddle at specified coordinates
jamesheavey 0:7d4d2023ed9c 32 }
jamesheavey 0:7d4d2023ed9c 33
jamesheavey 136:04a2724f90cf 34
jamesheavey 91:c01a736fb0d9 35 void Paddle::update(Direction d,float mag) // update the paddle's properties
jamesheavey 0:7d4d2023ed9c 36 {
jamesheavey 16:1761dfe801af 37 if (_tilt == false) {
jamesheavey 91:c01a736fb0d9 38 _speed = int(mag*10.0f); // scale is arbitrary
jamesheavey 91:c01a736fb0d9 39
jamesheavey 91:c01a736fb0d9 40 // update x value depending on direction of movement
jamesheavey 91:c01a736fb0d9 41 // West is decrement as origin is at the top-left so decreasing moves left
jamesheavey 16:1761dfe801af 42 if (d == W) {
jamesheavey 91:c01a736fb0d9 43 _x-=_speed * _sens; // adjust the x coordinate accordingly. Sensitivity scales the speed of the paddle
jamesheavey 16:1761dfe801af 44 } else if (d == E) {
jamesheavey 72:7254d2a8a1cd 45 _x+=_speed * _sens;
jamesheavey 16:1761dfe801af 46 }
jamesheavey 91:c01a736fb0d9 47
jamesheavey 91:c01a736fb0d9 48 // censure that the paddle doesn't go off screen
jamesheavey 16:1761dfe801af 49 if (_x < 1) {
jamesheavey 16:1761dfe801af 50 _x = 1;
jamesheavey 16:1761dfe801af 51 }
jamesheavey 16:1761dfe801af 52 if (_x > WIDTH - _width - 1) {
jamesheavey 16:1761dfe801af 53 _x = WIDTH - _width - 1;
jamesheavey 16:1761dfe801af 54 }
jamesheavey 91:c01a736fb0d9 55 } else if (_tilt == true) {
jamesheavey 91:c01a736fb0d9 56 accelerometer.init(); // initialise the accelerometer
jamesheavey 91:c01a736fb0d9 57 Data values = accelerometer.get_values(); // retrieve a struct of values
jamesheavey 91:c01a736fb0d9 58 float roll_rad = atan2(values.ay,values.az); // use values to calculate the roll angle in radians
jamesheavey 91:c01a736fb0d9 59 _speed = int((roll_rad*(360/3.14159265))*0.3f); // convert to degrees and scale
jamesheavey 91:c01a736fb0d9 60 _x -= _speed * _sens; // adjust the x coordinate accordingly
jamesheavey 16:1761dfe801af 61 }
jamesheavey 0:7d4d2023ed9c 62
jamesheavey 91:c01a736fb0d9 63 // ensure that the paddle doesn't go off screen
jamesheavey 0:7d4d2023ed9c 64 if (_x < 1) {
jamesheavey 0:7d4d2023ed9c 65 _x = 1;
jamesheavey 0:7d4d2023ed9c 66 }
jamesheavey 0:7d4d2023ed9c 67 if (_x > WIDTH - _width - 1) {
jamesheavey 0:7d4d2023ed9c 68 _x = WIDTH - _width - 1;
jamesheavey 0:7d4d2023ed9c 69 }
jamesheavey 0:7d4d2023ed9c 70 }
jamesheavey 0:7d4d2023ed9c 71
jamesheavey 136:04a2724f90cf 72
jamesheavey 91:c01a736fb0d9 73 int Paddle::get_lives() // returns the integer _lives
jamesheavey 9:f6f0f39538c7 74 {
jamesheavey 9:f6f0f39538c7 75 return _lives;
jamesheavey 9:f6f0f39538c7 76 }
jamesheavey 9:f6f0f39538c7 77
jamesheavey 136:04a2724f90cf 78
jamesheavey 91:c01a736fb0d9 79 void Paddle::lose_life() // decrements the member variable _lives
jamesheavey 9:f6f0f39538c7 80 {
jamesheavey 9:f6f0f39538c7 81 _lives--;
jamesheavey 9:f6f0f39538c7 82 }
jamesheavey 9:f6f0f39538c7 83
jamesheavey 136:04a2724f90cf 84
jamesheavey 114:280903dd7e06 85 void Paddle::inc_life() // increment the member variable _lives
jamesheavey 114:280903dd7e06 86 {
jamesheavey 114:280903dd7e06 87 _lives++;
jamesheavey 114:280903dd7e06 88 }
jamesheavey 114:280903dd7e06 89
jamesheavey 136:04a2724f90cf 90
jamesheavey 111:4848c399fae0 91 void Paddle::reset_lives()
jamesheavey 111:4848c399fae0 92 {
jamesheavey 111:4848c399fae0 93 _lives = 6;
jamesheavey 111:4848c399fae0 94 }
jamesheavey 111:4848c399fae0 95
jamesheavey 136:04a2724f90cf 96
jamesheavey 91:c01a736fb0d9 97 Vector2D Paddle::get_pos() // returns the vector of the paddle's current position
jamesheavey 91:c01a736fb0d9 98 {
jamesheavey 0:7d4d2023ed9c 99 Vector2D p = {_x,_y};
jamesheavey 91:c01a736fb0d9 100 return p;
jamesheavey 16:1761dfe801af 101 }
jamesheavey 16:1761dfe801af 102
jamesheavey 136:04a2724f90cf 103
jamesheavey 91:c01a736fb0d9 104 void Paddle::set_tilt() // sets the paddle motion option to tilt i.e. the member variable _tilt = true
jamesheavey 16:1761dfe801af 105 {
jamesheavey 16:1761dfe801af 106 _tilt = true;
jamesheavey 16:1761dfe801af 107 }
jamesheavey 16:1761dfe801af 108
jamesheavey 136:04a2724f90cf 109
jamesheavey 91:c01a736fb0d9 110 void Paddle::set_joy() // sets the paddle motion option to joystick
jamesheavey 16:1761dfe801af 111 {
jamesheavey 16:1761dfe801af 112 _tilt = false;
jamesheavey 16:1761dfe801af 113 }
jamesheavey 69:db1354676fde 114
jamesheavey 136:04a2724f90cf 115
jamesheavey 91:c01a736fb0d9 116 void Paddle::recentre() // moves the paddle back to the centre of the screen (used after victory screen to restart)
jamesheavey 69:db1354676fde 117 {
jamesheavey 91:c01a736fb0d9 118 _x = WIDTH/2 - PADDLE_WIDTH/2; // centre of the screen
jamesheavey 69:db1354676fde 119 }
jamesheavey 72:7254d2a8a1cd 120
jamesheavey 136:04a2724f90cf 121
jamesheavey 91:c01a736fb0d9 122 void Paddle::set_sens(float sens) // sets the member variable _sens to an input
jamesheavey 72:7254d2a8a1cd 123 {
jamesheavey 72:7254d2a8a1cd 124 _sens = sens;
jamesheavey 72:7254d2a8a1cd 125 }