James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Apr 23 17:09:47 2019 +0000
Revision:
15:b867a6620f96
Parent:
9:f6f0f39538c7
Child:
16:1761dfe801af
tilt works, need to add a motion control func and laser class

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 15:b867a6620f96 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 15:b867a6620f96 37 // //if (_tilt == false) {
jamesheavey 15:b867a6620f96 38 // _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future
jamesheavey 15:b867a6620f96 39
jamesheavey 15:b867a6620f96 40 // // update y value depending on direction of movement
jamesheavey 15:b867a6620f96 41 // // North is decrement as origin is at the top-left so decreasing moves up
jamesheavey 15:b867a6620f96 42 // if (d == W) {
jamesheavey 15:b867a6620f96 43 // _x-=_speed;
jamesheavey 15:b867a6620f96 44 // } else if (d == E) {
jamesheavey 15:b867a6620f96 45 // _x+=_speed;
jamesheavey 15:b867a6620f96 46 // }
jamesheavey 15:b867a6620f96 47
jamesheavey 15:b867a6620f96 48 // // check the y origin to ensure that the paddle doesn't go off screen
jamesheavey 15:b867a6620f96 49 // if (_x < 1) {
jamesheavey 15:b867a6620f96 50 // _x = 1;
jamesheavey 15:b867a6620f96 51 // }
jamesheavey 15:b867a6620f96 52 // if (_x > WIDTH - _width - 1) {
jamesheavey 15:b867a6620f96 53 // _x = WIDTH - _width - 1;
jamesheavey 15:b867a6620f96 54 // }
jamesheavey 15:b867a6620f96 55
jamesheavey 15:b867a6620f96 56 //else if (_tilt =+ true) {
jamesheavey 15:b867a6620f96 57
jamesheavey 15:b867a6620f96 58 Data values = acc.get_values();
jamesheavey 15:b867a6620f96 59 float roll_rad = atan2(values.ay,values.az);
jamesheavey 15:b867a6620f96 60 _speed = int((roll_rad*(360/3.14159265))*0.3f); // scale is arbitrary, could be changed in future
jamesheavey 0:7d4d2023ed9c 61
jamesheavey 0:7d4d2023ed9c 62 // update y value depending on direction of movement
jamesheavey 0:7d4d2023ed9c 63 // North is decrement as origin is at the top-left so decreasing moves up
jamesheavey 15:b867a6620f96 64 _x -= _speed;
jamesheavey 0:7d4d2023ed9c 65
jamesheavey 0:7d4d2023ed9c 66 // check the y origin to ensure that the paddle doesn't go off screen
jamesheavey 0:7d4d2023ed9c 67 if (_x < 1) {
jamesheavey 0:7d4d2023ed9c 68 _x = 1;
jamesheavey 0:7d4d2023ed9c 69 }
jamesheavey 0:7d4d2023ed9c 70 if (_x > WIDTH - _width - 1) {
jamesheavey 0:7d4d2023ed9c 71 _x = WIDTH - _width - 1;
jamesheavey 0:7d4d2023ed9c 72 }
jamesheavey 0:7d4d2023ed9c 73 }
jamesheavey 0:7d4d2023ed9c 74
jamesheavey 0:7d4d2023ed9c 75 void Paddle::add_score()
jamesheavey 0:7d4d2023ed9c 76 {
jamesheavey 0:7d4d2023ed9c 77 _score++;
jamesheavey 0:7d4d2023ed9c 78 }
jamesheavey 0:7d4d2023ed9c 79 int Paddle::get_score()
jamesheavey 0:7d4d2023ed9c 80 {
jamesheavey 0:7d4d2023ed9c 81 return _score;
jamesheavey 0:7d4d2023ed9c 82 }
jamesheavey 0:7d4d2023ed9c 83
jamesheavey 9:f6f0f39538c7 84 int Paddle::get_lives()
jamesheavey 9:f6f0f39538c7 85 {
jamesheavey 9:f6f0f39538c7 86 return _lives;
jamesheavey 9:f6f0f39538c7 87 }
jamesheavey 9:f6f0f39538c7 88
jamesheavey 9:f6f0f39538c7 89 void Paddle::lose_life()
jamesheavey 9:f6f0f39538c7 90 {
jamesheavey 9:f6f0f39538c7 91 _lives--;
jamesheavey 9:f6f0f39538c7 92 }
jamesheavey 9:f6f0f39538c7 93
jamesheavey 0:7d4d2023ed9c 94 Vector2D Paddle::get_pos() {
jamesheavey 0:7d4d2023ed9c 95 Vector2D p = {_x,_y};
jamesheavey 0:7d4d2023ed9c 96 return p;
jamesheavey 0:7d4d2023ed9c 97 }