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.
Ball/Ball.cpp@6:39bda45efeed, 2019-04-08 (annotated)
- Committer:
- kocemax
- Date:
- Mon Apr 08 09:14:33 2019 +0000
- Revision:
- 6:39bda45efeed
- Parent:
- 5:12c179da4788
- Child:
- 7:cd3cafda3dd4
Finally had more time during the holiday, reedited lots of the code to make it simpler, more readable, more robust, and better structured.Also added collision detection with the bricks, but no deflection yet.(Took the algorithm from an online forum).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kocemax | 2:006a2ddfabb6 | 1 | #include "Ball.h" |
kocemax | 2:006a2ddfabb6 | 2 | #include "PlayerControl.h" |
kocemax | 2:006a2ddfabb6 | 3 | |
kocemax | 2:006a2ddfabb6 | 4 | // Constructor |
kocemax | 2:006a2ddfabb6 | 5 | Ball::Ball() |
kocemax | 2:006a2ddfabb6 | 6 | { |
kocemax | 6:39bda45efeed | 7 | ballpos.x = WIDTH/2; |
kocemax | 6:39bda45efeed | 8 | ballpos.y = HEIGHT - GAP - 2; |
kocemax | 6:39bda45efeed | 9 | velocity.x = randomize(); |
kocemax | 6:39bda45efeed | 10 | velocity.y = -1; |
kocemax | 6:39bda45efeed | 11 | w = 1; |
kocemax | 6:39bda45efeed | 12 | h = 1; |
kocemax | 2:006a2ddfabb6 | 13 | } |
kocemax | 2:006a2ddfabb6 | 14 | |
kocemax | 2:006a2ddfabb6 | 15 | // Destructor |
kocemax | 2:006a2ddfabb6 | 16 | Ball::~Ball() |
kocemax | 2:006a2ddfabb6 | 17 | { |
kocemax | 2:006a2ddfabb6 | 18 | |
kocemax | 2:006a2ddfabb6 | 19 | } |
kocemax | 2:006a2ddfabb6 | 20 | |
kocemax | 6:39bda45efeed | 21 | //Ball screen edge detection |
kocemax | 6:39bda45efeed | 22 | void Ball::move() |
kocemax | 2:006a2ddfabb6 | 23 | { |
kocemax | 6:39bda45efeed | 24 | GameObject::move(); |
kocemax | 2:006a2ddfabb6 | 25 | |
kocemax | 6:39bda45efeed | 26 | if (ballpos.x > WIDTH-1) |
kocemax | 2:006a2ddfabb6 | 27 | { |
kocemax | 6:39bda45efeed | 28 | velocity.x = -1; |
kocemax | 3:fe856d0890ee | 29 | } |
kocemax | 6:39bda45efeed | 30 | else if(ballpos.x < 1) |
kocemax | 3:fe856d0890ee | 31 | { |
kocemax | 6:39bda45efeed | 32 | velocity.x = 1; |
kocemax | 2:006a2ddfabb6 | 33 | } |
kocemax | 6:39bda45efeed | 34 | if (ballpos.y < 1) |
kocemax | 3:fe856d0890ee | 35 | { |
kocemax | 6:39bda45efeed | 36 | velocity.y = 1; |
kocemax | 3:fe856d0890ee | 37 | } |
kocemax | 6:39bda45efeed | 38 | else if (ballpos.y > HEIGHT-1) |
kocemax | 2:006a2ddfabb6 | 39 | { |
kocemax | 6:39bda45efeed | 40 | velocity.y = -1; |
kocemax | 2:006a2ddfabb6 | 41 | } |
kocemax | 3:fe856d0890ee | 42 | } |
kocemax | 3:fe856d0890ee | 43 | |
kocemax | 6:39bda45efeed | 44 | //Pad and Ball collision detection |
kocemax | 6:39bda45efeed | 45 | void Ball::hitPad(Gamepad &pad, PlayerControl &pl) |
kocemax | 3:fe856d0890ee | 46 | { |
kocemax | 3:fe856d0890ee | 47 | Vector2D posPad = pl.get_padPos(pad); |
kocemax | 6:39bda45efeed | 48 | if (ballpos.y == posPad.y - 1 && (ballpos.x >= posPad.x && ballpos.x <= posPad.x + 12)) |
kocemax | 3:fe856d0890ee | 49 | { |
kocemax | 6:39bda45efeed | 50 | velocity.y = -1; |
kocemax | 2:006a2ddfabb6 | 51 | } |
kocemax | 3:fe856d0890ee | 52 | |
kocemax | 3:fe856d0890ee | 53 | |
kocemax | 2:006a2ddfabb6 | 54 | } |
kocemax | 2:006a2ddfabb6 | 55 | |
kocemax | 6:39bda45efeed | 56 | void Ball::endCondition(Gamepad &pad, N5110 &lcd, PlayerControl &pl) |
kocemax | 2:006a2ddfabb6 | 57 | { |
kocemax | 2:006a2ddfabb6 | 58 | Vector2D posPad = pl.get_padPos(pad); |
kocemax | 6:39bda45efeed | 59 | if (ballpos.y > posPad.y) |
kocemax | 2:006a2ddfabb6 | 60 | { |
kocemax | 4:0e01cbb95434 | 61 | while (1) |
kocemax | 4:0e01cbb95434 | 62 | { |
kocemax | 4:0e01cbb95434 | 63 | lcd.clear(); |
kocemax | 4:0e01cbb95434 | 64 | lcd.printString("You Lost",17,2); |
kocemax | 4:0e01cbb95434 | 65 | lcd.printString("Press A",20,3); |
kocemax | 4:0e01cbb95434 | 66 | lcd.printString("to restart",12,4); |
kocemax | 4:0e01cbb95434 | 67 | wait(0.1); |
kocemax | 4:0e01cbb95434 | 68 | lcd.refresh(); |
kocemax | 4:0e01cbb95434 | 69 | if (pad.check_event(Gamepad::A_PRESSED) == true) |
kocemax | 4:0e01cbb95434 | 70 | { |
kocemax | 6:39bda45efeed | 71 | resetGame(pl); |
kocemax | 4:0e01cbb95434 | 72 | break; |
kocemax | 4:0e01cbb95434 | 73 | } |
kocemax | 4:0e01cbb95434 | 74 | } |
kocemax | 2:006a2ddfabb6 | 75 | } |
kocemax | 4:0e01cbb95434 | 76 | } |
kocemax | 4:0e01cbb95434 | 77 | |
kocemax | 6:39bda45efeed | 78 | void Ball::resetGame(PlayerControl &pl) |
kocemax | 6:39bda45efeed | 79 | { |
kocemax | 6:39bda45efeed | 80 | ballpos.x = WIDTH/2; |
kocemax | 6:39bda45efeed | 81 | ballpos.y = HEIGHT - GAP - 2; |
kocemax | 6:39bda45efeed | 82 | velocity.x = randomize(); |
kocemax | 6:39bda45efeed | 83 | velocity.y = -1; |
kocemax | 6:39bda45efeed | 84 | pl.padReset(); |
kocemax | 6:39bda45efeed | 85 | } |
kocemax | 6:39bda45efeed | 86 | |
kocemax | 6:39bda45efeed | 87 | int Ball::randomize() |
kocemax | 4:0e01cbb95434 | 88 | { |
kocemax | 6:39bda45efeed | 89 | AnalogIn noisy(PTB0); |
kocemax | 6:39bda45efeed | 90 | int movement; |
kocemax | 6:39bda45efeed | 91 | srand(1000000*noisy.read()); |
kocemax | 6:39bda45efeed | 92 | int direction = rand() % 2; // randomise initial direction. |
kocemax | 6:39bda45efeed | 93 | if (direction == 0) |
kocemax | 6:39bda45efeed | 94 | { |
kocemax | 6:39bda45efeed | 95 | movement = -1; |
kocemax | 6:39bda45efeed | 96 | } |
kocemax | 6:39bda45efeed | 97 | else if (direction == 1) |
kocemax | 6:39bda45efeed | 98 | { |
kocemax | 6:39bda45efeed | 99 | movement = 1; |
kocemax | 6:39bda45efeed | 100 | } |
kocemax | 6:39bda45efeed | 101 | return movement; |
kocemax | 6:39bda45efeed | 102 | } |
kocemax | 6:39bda45efeed | 103 | |
kocemax | 6:39bda45efeed | 104 | void Ball::hitBrick(Gamepad &pad, N5110 &lcd) |
kocemax | 6:39bda45efeed | 105 | { |
kocemax | 6:39bda45efeed | 106 | /* |
kocemax | 6:39bda45efeed | 107 | Vector2D posBall = get_ballPos(pad); |
kocemax | 6:39bda45efeed | 108 | int pixel_to_test1 = lcd.getPixel(posBall.x,posBall.y-2); |
kocemax | 6:39bda45efeed | 109 | int pixel_to_test2 = lcd.getPixel(posBall.x,posBall.y+2); |
kocemax | 6:39bda45efeed | 110 | int pixel_to_test3 = lcd.getPixel(posBall.x+2,posBall.y); |
kocemax | 6:39bda45efeed | 111 | int pixel_to_test4 = lcd.getPixel(posBall.x-2,posBall.y); |
kocemax | 6:39bda45efeed | 112 | |
kocemax | 6:39bda45efeed | 113 | if ( pixel_to_test1 ) |
kocemax | 6:39bda45efeed | 114 | { |
kocemax | 6:39bda45efeed | 115 | velocity.y = -1; |
kocemax | 6:39bda45efeed | 116 | } |
kocemax | 6:39bda45efeed | 117 | if ( pixel_to_test2 ) |
kocemax | 6:39bda45efeed | 118 | { |
kocemax | 6:39bda45efeed | 119 | velocity.y = 1; |
kocemax | 6:39bda45efeed | 120 | } |
kocemax | 6:39bda45efeed | 121 | if ( pixel_to_test3 ) |
kocemax | 6:39bda45efeed | 122 | { |
kocemax | 6:39bda45efeed | 123 | velocity.x = -1; |
kocemax | 6:39bda45efeed | 124 | } |
kocemax | 6:39bda45efeed | 125 | if ( pixel_to_test4 ) |
kocemax | 6:39bda45efeed | 126 | { |
kocemax | 6:39bda45efeed | 127 | velocity.x = 1; |
kocemax | 6:39bda45efeed | 128 | } |
kocemax | 6:39bda45efeed | 129 | */ |
kocemax | 2:006a2ddfabb6 | 130 | } |