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.
Diff: Ball/Ball.cpp
- Revision:
- 3:fe856d0890ee
- Parent:
- 2:006a2ddfabb6
- Child:
- 4:0e01cbb95434
--- a/Ball/Ball.cpp Wed Mar 20 20:58:36 2019 +0000 +++ b/Ball/Ball.cpp Sat Mar 23 11:17:33 2019 +0000 @@ -1,14 +1,15 @@ #include "Ball.h" #include "PlayerControl.h" +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() { - _xBall = WIDTH/2; // draw ball in the middle of the columns initially - _yBall = HEIGHT - GAP - 2; // draw ball close to the bottom of the LCD - _counterx = 1; - _countery = 1; + } // Destructor @@ -19,36 +20,50 @@ void Ball::drawBall(N5110 &lcd) { - lcd.drawRect(_xBall,_yBall,2,2,FILL_BLACK); + lcd.drawRect(g_xBall,g_yBall,2,2,FILL_BLACK); } void Ball::moveBall() { - _xBall += _counterx; - _yBall -= _countery; + g_xBall += g_counterx; + g_yBall -= g_countery; - if (_xBall > 82) + if (g_xBall > 82) { - _counterx = -1; + g_counterx = -1; + } + else if(g_xBall < 1) + { + g_counterx = 1; } - else if( _xBall < 1) + if (g_yBall < 1) + { + g_countery = -1; + } + else if (g_yBall > 46) { - _counterx = 1; + g_countery = 1; } - if (_yBall < 1) - { - _countery = -1; +} + +void Ball::hitPad(Gamepad &pad) +{ + PlayerControl pl; + Vector2D posBall = get_ballPos(pad); + Vector2D posPad = pl.get_padPos(pad); + if (posBall.y == posPad.y - 2 && (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); } - else if (_yBall > 46) - { - _countery = 1; - } + + } Vector2D Ball::get_ballPos(Gamepad &pad) { - Vector2D posBall = {_xBall,_yBall}; + Vector2D posBall = {g_xBall,g_yBall}; return posBall; } @@ -57,7 +72,7 @@ PlayerControl pl; Vector2D posBall = get_ballPos(pad); Vector2D posPad = pl.get_padPos(pad); - if (posBall.y < posPad.y) + if (posBall.y > posPad.y) { return true; }