ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Ball/Ball.cpp

Committer:
shahidsajid
Date:
2019-04-16
Revision:
5:915bcac4e9b9
Parent:
4:55a0509c4874
Child:
6:3e50f2cf4366

File content as of revision 5:915bcac4e9b9:

#include "Ball.h"


Ball::Ball()
{

}

Ball::~Ball()
{

}

void Ball::init(int size,int speed)
{
    _size = size;

    _x = 42;
    _y = 15;

    srand(time(NULL));
    int direction = rand() % 4; // randomise initial direction. 

    // 4 possibilities. Get random modulo and set velocities accordingly
    if (direction == 0) {
        _velocity.x = speed;
        _velocity.y = speed;
    } else if (direction == 1) {
        _velocity.x = speed;
        _velocity.y = -speed;
    } else if (direction == 2) {
        _velocity.x = speed;
        _velocity.y = speed;
    } else {
        _velocity.x = -speed;
        _velocity.y = -speed;
    }
}

void Ball::start(N5110 &lcd){
    if (_y!=36){
        _y+=1;
    }
}

void Ball::draw(N5110 &lcd)
{
    lcd.drawCircle(_x,_y,1,FILL_BLACK);
    //lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
}

void Ball::update()
{
    _x += _velocity.x;
    _y += _velocity.y;
}

void Ball::set_velocity(Vector2D v)
{
    _velocity.x = v.x;
    _velocity.y = v.y;
}

Vector2D Ball::get_velocity()
{
    Vector2D v = {_velocity.x,_velocity.y};
    return v;
}

Vector2D Ball::get_pos()
{
    Vector2D p = {_x,_y};
    return p;
}

void Ball::set_pos(Vector2D p)
{
    _x = p.x;
    _y = p.y;
}
void Ball::get_direction(Gamepad &pad){
    _d = pad.get_direction();
}
void Ball::set_field(N5110 &lcd){
    lcd.drawLine(40,11,40,14,1);
    lcd.drawLine(42,11,42,14,1);
    lcd.drawLine(44,11,44,14,1);
    lcd.drawLine(40,14,44,14,2);
    /*
    lcd.drawCircle(25,2,2,FILL_BLACK); //NE
    lcd.drawCircle(65,2,2,FILL_BLACK);  //NW
    lcd.drawCircle(65,40,2,FILL_BLACK);  //SE
    lcd.drawCircle(25,40,2,FILL_BLACK); //SW
    lcd.drawCircle(3,20,2,FILL_BLACK); //E
    lcd.drawCircle(80,20,2,FILL_BLACK);
    */
    int d=0;
    srand(time(NULL));
    for(int i=0;i<3;i++){
        d = 1+ rand() % 6;
        //printf("Direction %i \n",d);  
        //while (d==N||d==S){
            //get_direction(pad);
        //}
        if (d==1){ //NE
            field[i].x=25;
            field[i].y=2;
            field[i].position=1;
        }
        if (d==2){ //NW
            field[i].x=65;
            field[i].y=2;
            field[i].position=1;
        }
        if (d==3){ //SW
            field[i].x=25;
            field[i].y=40;
            field[i].position=1;
        }
        if (d==4){ //SE
            field[i].x=65;
            field[i].y=40;
            field[i].position=1;
        }
        if (d==5){ //E
            field[i].x=3;
            field[i].y=20;
            field[i].position=1;
        }
        if (d==3){ //W
            field[i].x=80;
            field[i].y=20;
            field[i].position=1;
        }
  }
  lcd.drawCircle(field[0].x,field[0].y,2,FILL_BLACK);
  lcd.drawCircle(field[1].x,field[1].y,2,FILL_BLACK);
  lcd.drawCircle(field[2].x,field[2].y,2,FILL_BLACK);
  lcd.drawCircle(field[3].x,field[3].y,2,FILL_BLACK);
  
}