Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Ball/Ball.cpp

Committer:
kocemax
Date:
2019-03-23
Revision:
4:0e01cbb95434
Parent:
3:fe856d0890ee
Child:
5:12c179da4788

File content as of revision 4:0e01cbb95434:

#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
int g_counterx = 1;
int g_countery = 1;

// Constructor
Ball::Ball()
{

}

// Destructor
Ball::~Ball()
{

}

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

void Ball::moveBall() 
{   
    
    g_xBall += g_counterx;
    g_yBall -= g_countery;
    
    if (g_xBall > 83) 
    {   
        g_counterx = -1;
    }
    else if(g_xBall < 1)
    {
        g_counterx = 1;
    }
    if (g_yBall < 1)
    {   
        g_countery = -1;
    }
    else if (g_yBall > 47) 
    {
        g_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))
    {
        g_countery = !g_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;
    g_counterx = 1;
    g_countery = 1;
    g_xBall += g_counterx;
    g_yBall -= g_countery; 
    pl.padReset();
}