James Heavey / Mbed 2 deprecated 2665-Breakout-Game

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Jan 05 01:14:11 2021 +0000
Revision:
0:92b180c8d407
test

Who changed what in which revision?

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