Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Ball/Ball.cpp

Committer:
kocemax
Date:
2019-03-26
Revision:
5:12c179da4788
Parent:
4:0e01cbb95434
Child:
6:39bda45efeed

File content as of revision 5:12c179da4788:

#include "Ball.h"
#include "PlayerControl.h"

PlayerControl pl;

int g_xBall = WIDTH/2; // draw ball in the middle of the columns initially
int g_yBall = HEIGHT - GAP - 2; // draw ball close to the bottom of the LCD

// Constructor
Ball::Ball()
{
    _counterx = 1;
    _countery = 1;
}

// Destructor
Ball::~Ball()
{

}

void Ball::drawBall(N5110 &lcd) 
{
    lcd.drawRect(g_xBall,g_yBall,1,1,FILL_BLACK);
}

void Ball::moveBall() 
{   
    
    g_xBall += _counterx;
    g_yBall -= _countery;
    
    if (g_xBall > 83) 
    {   
        _counterx = -1;
    }
    else if(g_xBall < 1)
    {
        _counterx = 1;
    }
    if (g_yBall < 1)
    {   
        _countery = -1;
    }
    else if (g_yBall > 47) 
    {
        _countery = 1;
    }
}

void Ball::hitPad(Gamepad &pad) 
{
    Vector2D posBall = get_ballPos(pad);
    Vector2D posPad = pl.get_padPos(pad);
    if (posBall.y == posPad.y - 1 && (posBall.x >= posPad.x && posBall.x <= posPad.x + 12))
    {
        _countery = !_countery;
        //printf("\nball x = %f, ball y = %f \n pad x = %f, pad y = %f ",posBall.x, posBall.y, posPad.x, posPad.y);
    }
    
    
}

Vector2D Ball::get_ballPos(Gamepad &pad)
{
    Vector2D posBall = {g_xBall,g_yBall};
    return posBall;
}

void Ball::endCondition(Gamepad &pad, N5110 &lcd)
{
    Vector2D posBall = get_ballPos(pad);
    Vector2D posPad = pl.get_padPos(pad);
    if (posBall.y > posPad.y) 
    {
        while (1) 
        {
            lcd.clear();
            lcd.printString("You Lost",17,2);
            lcd.printString("Press A",20,3);
            lcd.printString("to restart",12,4);
            wait(0.1);
            lcd.refresh();
            if (pad.check_event(Gamepad::A_PRESSED) == true)
            {
                resetGame();
                break;           
            }
        }
    }
}

void Ball::resetGame()
{
    g_xBall = WIDTH/2;
    g_yBall = HEIGHT - GAP - 2;
    _counterx = 1;
    _countery = 1;
    g_xBall += _counterx;
    g_yBall -= _countery; 
    pl.padReset();
}