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:
- 6:39bda45efeed
- Parent:
- 5:12c179da4788
- Child:
- 7:cd3cafda3dd4
--- a/Ball/Ball.cpp Tue Mar 26 09:16:35 2019 +0000 +++ b/Ball/Ball.cpp Mon Apr 08 09:14:33 2019 +0000 @@ -1,16 +1,15 @@ #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; + ballpos.x = WIDTH/2; + ballpos.y = HEIGHT - GAP - 2; + velocity.x = randomize(); + velocity.y = -1; + w = 1; + h = 1; } // Destructor @@ -19,59 +18,45 @@ } -void Ball::drawBall(N5110 &lcd) -{ - lcd.drawRect(g_xBall,g_yBall,1,1,FILL_BLACK); -} - -void Ball::moveBall() +//Ball screen edge detection +void Ball::move() { + GameObject::move(); - g_xBall += _counterx; - g_yBall -= _countery; - - if (g_xBall > 83) + if (ballpos.x > WIDTH-1) { - _counterx = -1; + velocity.x = -1; } - else if(g_xBall < 1) + else if(ballpos.x < 1) { - _counterx = 1; + velocity.x = 1; } - if (g_yBall < 1) + if (ballpos.y < 1) { - _countery = -1; + velocity.y = 1; } - else if (g_yBall > 47) + else if (ballpos.y > HEIGHT-1) { - _countery = 1; + velocity.y = -1; } } -void Ball::hitPad(Gamepad &pad) +//Pad and Ball collision detection +void Ball::hitPad(Gamepad &pad, PlayerControl &pl) { - 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)) + if (ballpos.y == posPad.y - 1 && (ballpos.x >= posPad.x && ballpos.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); + velocity.y = -1; } } -Vector2D Ball::get_ballPos(Gamepad &pad) +void Ball::endCondition(Gamepad &pad, N5110 &lcd, PlayerControl &pl) { - 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) + if (ballpos.y > posPad.y) { while (1) { @@ -83,20 +68,63 @@ lcd.refresh(); if (pad.check_event(Gamepad::A_PRESSED) == true) { - resetGame(); + resetGame(pl); break; } } } } -void Ball::resetGame() +void Ball::resetGame(PlayerControl &pl) +{ + ballpos.x = WIDTH/2; + ballpos.y = HEIGHT - GAP - 2; + velocity.x = randomize(); + velocity.y = -1; + pl.padReset(); +} + +int Ball::randomize() { - g_xBall = WIDTH/2; - g_yBall = HEIGHT - GAP - 2; - _counterx = 1; - _countery = 1; - g_xBall += _counterx; - g_yBall -= _countery; - pl.padReset(); + AnalogIn noisy(PTB0); + int movement; + srand(1000000*noisy.read()); + int direction = rand() % 2; // randomise initial direction. + if (direction == 0) + { + movement = -1; + } + else if (direction == 1) + { + movement = 1; + } + return movement; +} + +void Ball::hitBrick(Gamepad &pad, N5110 &lcd) +{ + /* + Vector2D posBall = get_ballPos(pad); + int pixel_to_test1 = lcd.getPixel(posBall.x,posBall.y-2); + int pixel_to_test2 = lcd.getPixel(posBall.x,posBall.y+2); + int pixel_to_test3 = lcd.getPixel(posBall.x+2,posBall.y); + int pixel_to_test4 = lcd.getPixel(posBall.x-2,posBall.y); + + if ( pixel_to_test1 ) + { + velocity.y = -1; + } + if ( pixel_to_test2 ) + { + velocity.y = 1; + } + if ( pixel_to_test3 ) + { + velocity.x = -1; + } + if ( pixel_to_test4 ) + { + velocity.x = 1; + } + */ } \ No newline at end of file