Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Paddle/Paddle.cpp
- Committer:
- jamesheavey
- Date:
- 2019-05-06
- Revision:
- 75:d96b177585aa
- Parent:
- 74:dfb1d693d8e3
- Child:
- 89:806679ed11f2
File content as of revision 75:d96b177585aa:
#include "Paddle.h"
FXOS8700CQ acc(I2C_SDA,I2C_SCL);
// nothing doing in the constructor and destructor
Paddle::Paddle()
{
}
Paddle::~Paddle()
{
}
void Paddle::init(int y,int height,int width)
{
    _x = WIDTH/2 - width/2;  // x value on screen is fixed
    _y = y;  // y depends on height of screen and height of paddle
    _height = height;
    _width = width;
    _sens = 0.5;
    _speed = 4;  // default speed
    _score = 0;  // start score from zero
    _lives = 6;
    _tilt = false; //use to choose between motion methods
}
void Paddle::draw(N5110 &lcd)
{
    // draw paddle in screen buffer. 
    lcd.drawRect(_x,_y,_width,_height,FILL_BLACK);
}
void Paddle::update(Direction d,float mag)
{
    acc.init();
    
    if (_tilt == false) {
        _speed = int(mag*10.0f);  // scale is arbitrary, could be changed in future
    
        // update y value depending on direction of movement
        // North is decrement as origin is at the top-left so decreasing moves up
        if (d == W) {
            _x-=_speed * _sens;
        } else if (d == E) {
            _x+=_speed * _sens;
        }
    
        // check the y origin to ensure that the paddle doesn't go off screen
        if (_x < 1) {
            _x = 1;
        }
        if (_x > WIDTH - _width - 1) {
            _x = WIDTH - _width - 1;
        }
    }
    else if (_tilt == true) {
    
        Data values = acc.get_values();
        float roll_rad = atan2(values.ay,values.az);
        _speed = int((roll_rad*(360/3.14159265))*0.3f);  // scale is arbitrary, could be changed in future
    
        // update y value depending on direction of movement
        // North is decrement as origin is at the top-left so decreasing moves up
        _x -= _speed * _sens;
    }
    // check the y origin to ensure that the paddle doesn't go off screen
    if (_x < 1) {
        _x = 1;
    }
    if (_x > WIDTH - _width - 1) {
        _x = WIDTH - _width - 1;
    }
}
void Paddle::add_score()
{
    _score++;
}
int Paddle::get_score()
{
    return _score;
}
int Paddle::get_lives()
{
    return _lives;
}
void Paddle::lose_life()
{
    _lives--;
}
Vector2D Paddle::get_pos() {
    Vector2D p = {_x,_y};
    return p;    
}
void Paddle::set_tilt()
{
    _tilt = true;
}
void Paddle::set_joy()
{
    _tilt = false;
}
void Paddle::recentre() 
{
    _x = WIDTH/2 - 15/2;  // x value on screen is fixed
}
void Paddle::set_sens(float sens) 
{
    _sens = sens;
}