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:
- 7:cd3cafda3dd4
- Parent:
- 6:39bda45efeed
- Child:
- 8:9b77eea95088
--- a/Ball/Ball.cpp Mon Apr 08 09:14:33 2019 +0000 +++ b/Ball/Ball.cpp Wed Apr 10 09:18:25 2019 +0000 @@ -4,12 +4,7 @@ // Constructor Ball::Ball() { - ballpos.x = WIDTH/2; - ballpos.y = HEIGHT - GAP - 2; - velocity.x = randomize(); - velocity.y = -1; - w = 1; - h = 1; + reset(); } // Destructor @@ -19,112 +14,53 @@ } //Ball screen edge detection -void Ball::move() -{ +void Ball::move() +{ GameObject::move(); - - if (ballpos.x > WIDTH-1) - { + + if (pos.x > WIDTH-1) { velocity.x = -1; - } - else if(ballpos.x < 1) - { + } else if(pos.x < 1) { velocity.x = 1; } - if (ballpos.y < 1) - { + if (pos.y < 1) { velocity.y = 1; - } - else if (ballpos.y > HEIGHT-1) - { + } else if (pos.y > HEIGHT-1) { velocity.y = -1; } } //Pad and Ball collision detection -void Ball::hitPad(Gamepad &pad, PlayerControl &pl) +void Ball::hitPad(PlayerControl &pl) { - Vector2D posPad = pl.get_padPos(pad); - if (ballpos.y == posPad.y - 1 && (ballpos.x >= posPad.x && ballpos.x <= posPad.x + 12)) - { + Vector2D& posPad = pl.get_padPos(); + if (pos.y == posPad.y - 1 && (pos.x >= posPad.x && pos.x <= posPad.x + 12)) { velocity.y = -1; } - - -} -void Ball::endCondition(Gamepad &pad, N5110 &lcd, PlayerControl &pl) -{ - Vector2D posPad = pl.get_padPos(pad); - if (ballpos.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(pl); - break; - } - } - } -} -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() { AnalogIn noisy(PTB0); + srand(1000000*noisy.read()); + int direction = rand() % 2; // randomise initial direction. int movement; - srand(1000000*noisy.read()); - int direction = rand() % 2; // randomise initial direction. - if (direction == 0) - { + if (direction == 0) { movement = -1; - } - else if (direction == 1) - { + } else if (direction == 1) { movement = 1; } - return movement; + return movement; } -void Ball::hitBrick(Gamepad &pad, N5110 &lcd) +void Ball::reset() { - /* - 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; - } - */ + pos.x = WIDTH/2; + pos.y = HEIGHT - GAP - 2; + velocity.x = randomize(); + velocity.y = -1; + w = 1; + h = 1; } \ No newline at end of file