James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Apr 23 19:53:23 2019 +0000
Revision:
22:fa2e0b58043a
Parent:
19:06f4d39b9290
Child:
38:3a077f0259df
could also add a scoring mechanism each time a brick is destoyed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 0:7d4d2023ed9c 1 #include "Paddle.h"
jamesheavey 0:7d4d2023ed9c 2
jamesheavey 15:b867a6620f96 3 FXOS8700CQ acc(I2C_SDA,I2C_SCL);
jamesheavey 15:b867a6620f96 4
jamesheavey 0:7d4d2023ed9c 5 // nothing doing in the constructor and destructor
jamesheavey 0:7d4d2023ed9c 6 Paddle::Paddle()
jamesheavey 0:7d4d2023ed9c 7 {
jamesheavey 0:7d4d2023ed9c 8
jamesheavey 0:7d4d2023ed9c 9 }
jamesheavey 0:7d4d2023ed9c 10
jamesheavey 0:7d4d2023ed9c 11 Paddle::~Paddle()
jamesheavey 0:7d4d2023ed9c 12 {
jamesheavey 0:7d4d2023ed9c 13
jamesheavey 0:7d4d2023ed9c 14 }
jamesheavey 0:7d4d2023ed9c 15
jamesheavey 1:e18615d46071 16 void Paddle::init(int y,int height,int width)
jamesheavey 0:7d4d2023ed9c 17 {
jamesheavey 1:e18615d46071 18 _x = WIDTH/2 - width/2; // x value on screen is fixed
jamesheavey 1:e18615d46071 19 _y = y; // y depends on height of screen and height of paddle
jamesheavey 0:7d4d2023ed9c 20 _height = height;
jamesheavey 0:7d4d2023ed9c 21 _width = width;
jamesheavey 0:7d4d2023ed9c 22 _speed = 1; // default speed
jamesheavey 0:7d4d2023ed9c 23 _score = 0; // start score from zero
jamesheavey 9:f6f0f39538c7 24 _lives = 6;
jamesheavey 16:1761dfe801af 25 _tilt = false; //use to choose between motion methods
jamesheavey 0:7d4d2023ed9c 26
jamesheavey 0:7d4d2023ed9c 27 }
jamesheavey 0:7d4d2023ed9c 28
jamesheavey 0:7d4d2023ed9c 29 void Paddle::draw(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 30 {
jamesheavey 0:7d4d2023ed9c 31 // draw paddle in screen buffer.
jamesheavey 0:7d4d2023ed9c 32 lcd.drawRect(_x,_y,_width,_height,FILL_BLACK);
jamesheavey 0:7d4d2023ed9c 33 }
jamesheavey 0:7d4d2023ed9c 34
jamesheavey 0:7d4d2023ed9c 35 void Paddle::update(Direction d,float mag)
jamesheavey 0:7d4d2023ed9c 36 {
jamesheavey 19:06f4d39b9290 37 acc.init();
jamesheavey 19:06f4d39b9290 38
jamesheavey 16:1761dfe801af 39 if (_tilt == false) {
jamesheavey 16:1761dfe801af 40 _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future
jamesheavey 16:1761dfe801af 41
jamesheavey 16:1761dfe801af 42 // update y value depending on direction of movement
jamesheavey 16:1761dfe801af 43 // North is decrement as origin is at the top-left so decreasing moves up
jamesheavey 16:1761dfe801af 44 if (d == W) {
jamesheavey 16:1761dfe801af 45 _x-=_speed;
jamesheavey 16:1761dfe801af 46 } else if (d == E) {
jamesheavey 16:1761dfe801af 47 _x+=_speed;
jamesheavey 16:1761dfe801af 48 }
jamesheavey 15:b867a6620f96 49
jamesheavey 16:1761dfe801af 50 // check the y origin to ensure that the paddle doesn't go off screen
jamesheavey 16:1761dfe801af 51 if (_x < 1) {
jamesheavey 16:1761dfe801af 52 _x = 1;
jamesheavey 16:1761dfe801af 53 }
jamesheavey 16:1761dfe801af 54 if (_x > WIDTH - _width - 1) {
jamesheavey 16:1761dfe801af 55 _x = WIDTH - _width - 1;
jamesheavey 16:1761dfe801af 56 }
jamesheavey 16:1761dfe801af 57 }
jamesheavey 16:1761dfe801af 58 else if (_tilt == true) {
jamesheavey 15:b867a6620f96 59
jamesheavey 16:1761dfe801af 60 Data values = acc.get_values();
jamesheavey 22:fa2e0b58043a 61 float roll_rad = atan2(values.ay,values.az) + 0.3f ;
jamesheavey 19:06f4d39b9290 62 _speed = int((roll_rad*(360/3.14159265))*0.3f); // scale is arbitrary, could be changed in future
jamesheavey 16:1761dfe801af 63
jamesheavey 16:1761dfe801af 64 // update y value depending on direction of movement
jamesheavey 16:1761dfe801af 65 // North is decrement as origin is at the top-left so decreasing moves up
jamesheavey 16:1761dfe801af 66 _x -= _speed;
jamesheavey 16:1761dfe801af 67 }
jamesheavey 0:7d4d2023ed9c 68
jamesheavey 0:7d4d2023ed9c 69 // check the y origin to ensure that the paddle doesn't go off screen
jamesheavey 0:7d4d2023ed9c 70 if (_x < 1) {
jamesheavey 0:7d4d2023ed9c 71 _x = 1;
jamesheavey 0:7d4d2023ed9c 72 }
jamesheavey 0:7d4d2023ed9c 73 if (_x > WIDTH - _width - 1) {
jamesheavey 0:7d4d2023ed9c 74 _x = WIDTH - _width - 1;
jamesheavey 0:7d4d2023ed9c 75 }
jamesheavey 0:7d4d2023ed9c 76 }
jamesheavey 0:7d4d2023ed9c 77
jamesheavey 0:7d4d2023ed9c 78 void Paddle::add_score()
jamesheavey 0:7d4d2023ed9c 79 {
jamesheavey 0:7d4d2023ed9c 80 _score++;
jamesheavey 0:7d4d2023ed9c 81 }
jamesheavey 0:7d4d2023ed9c 82 int Paddle::get_score()
jamesheavey 0:7d4d2023ed9c 83 {
jamesheavey 0:7d4d2023ed9c 84 return _score;
jamesheavey 0:7d4d2023ed9c 85 }
jamesheavey 0:7d4d2023ed9c 86
jamesheavey 9:f6f0f39538c7 87 int Paddle::get_lives()
jamesheavey 9:f6f0f39538c7 88 {
jamesheavey 9:f6f0f39538c7 89 return _lives;
jamesheavey 9:f6f0f39538c7 90 }
jamesheavey 9:f6f0f39538c7 91
jamesheavey 9:f6f0f39538c7 92 void Paddle::lose_life()
jamesheavey 9:f6f0f39538c7 93 {
jamesheavey 9:f6f0f39538c7 94 _lives--;
jamesheavey 9:f6f0f39538c7 95 }
jamesheavey 9:f6f0f39538c7 96
jamesheavey 0:7d4d2023ed9c 97 Vector2D Paddle::get_pos() {
jamesheavey 0:7d4d2023ed9c 98 Vector2D p = {_x,_y};
jamesheavey 0:7d4d2023ed9c 99 return p;
jamesheavey 16:1761dfe801af 100 }
jamesheavey 16:1761dfe801af 101
jamesheavey 16:1761dfe801af 102 void Paddle::set_tilt()
jamesheavey 16:1761dfe801af 103 {
jamesheavey 16:1761dfe801af 104 _tilt = true;
jamesheavey 16:1761dfe801af 105 }
jamesheavey 16:1761dfe801af 106
jamesheavey 16:1761dfe801af 107 void Paddle::set_joy()
jamesheavey 16:1761dfe801af 108 {
jamesheavey 16:1761dfe801af 109 _tilt = false;
jamesheavey 16:1761dfe801af 110 }