Brickbreaker
Dependencies: NokiaLCD PS2 mbed
Fork of Pong by
ball.cpp@4:f3ac45fa2196, 2013-04-29 (annotated)
- Committer:
- ipapp3
- Date:
- Mon Apr 29 18:17:51 2013 +0000
- Revision:
- 4:f3ac45fa2196
- Parent:
- 3:e1328f84b107
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wjohnsto | 0:93dce1e528b9 | 1 | #include "mbed.h" |
wjohnsto | 0:93dce1e528b9 | 2 | #include "ball.h" |
wjohnsto | 0:93dce1e528b9 | 3 | |
wjohnsto | 0:93dce1e528b9 | 4 | Ball::Ball() { |
wjohnsto | 0:93dce1e528b9 | 5 | int x=y=width=height=color=xInc=yInc=0; |
wjohnsto | 0:93dce1e528b9 | 6 | } |
wjohnsto | 0:93dce1e528b9 | 7 | |
wjohnsto | 0:93dce1e528b9 | 8 | Ball::Ball(int x, int y, int w, int h, int c, int xi, int yi) |
wjohnsto | 0:93dce1e528b9 | 9 | : x(x), y(y), width(w), height(h), color(c), xInc(xi), yInc(yi) {} |
wjohnsto | 0:93dce1e528b9 | 10 | |
wjohnsto | 1:3cc8b1413557 | 11 | |
wjohnsto | 1:3cc8b1413557 | 12 | /* |
wjohnsto | 1:3cc8b1413557 | 13 | * Member Function move: |
wjohnsto | 1:3cc8b1413557 | 14 | * Description: Colors in the previous ball black |
wjohnsto | 1:3cc8b1413557 | 15 | * and moves ball to new position. |
wjohnsto | 1:3cc8b1413557 | 16 | */ |
wjohnsto | 0:93dce1e528b9 | 17 | void Ball::move(NokiaLCD &lcd) { |
wjohnsto | 1:3cc8b1413557 | 18 | draw(lcd, true); |
wjohnsto | 0:93dce1e528b9 | 19 | x += xInc; y += yInc; |
wjohnsto | 0:93dce1e528b9 | 20 | } |
wjohnsto | 0:93dce1e528b9 | 21 | |
wjohnsto | 1:3cc8b1413557 | 22 | /* |
wjohnsto | 1:3cc8b1413557 | 23 | * Member Function draw: |
wjohnsto | 1:3cc8b1413557 | 24 | * Description: Draws object on screen |
wjohnsto | 1:3cc8b1413557 | 25 | * if isBlack, color in black. |
wjohnsto | 1:3cc8b1413557 | 26 | */ |
wjohnsto | 0:93dce1e528b9 | 27 | void Ball::draw(NokiaLCD &lcd, bool isBlack) const { |
wjohnsto | 0:93dce1e528b9 | 28 | lcd.fill(x, y, width, height, (isBlack) ? 0x000000 : color); |
wjohnsto | 0:93dce1e528b9 | 29 | } |
wjohnsto | 0:93dce1e528b9 | 30 | |
wjohnsto | 0:93dce1e528b9 | 31 | int Ball::size() const { |
wjohnsto | 0:93dce1e528b9 | 32 | return width*height; |
wjohnsto | 0:93dce1e528b9 | 33 | } |
wjohnsto | 0:93dce1e528b9 | 34 | |
wjohnsto | 0:93dce1e528b9 | 35 | int Ball::getX() const { |
wjohnsto | 0:93dce1e528b9 | 36 | return x; |
wjohnsto | 0:93dce1e528b9 | 37 | } |
wjohnsto | 0:93dce1e528b9 | 38 | |
wjohnsto | 0:93dce1e528b9 | 39 | int Ball::getY() const { |
wjohnsto | 0:93dce1e528b9 | 40 | return y; |
wjohnsto | 0:93dce1e528b9 | 41 | } |
wjohnsto | 0:93dce1e528b9 | 42 | |
wjohnsto | 0:93dce1e528b9 | 43 | bool Ball::hitX() { |
wjohnsto | 0:93dce1e528b9 | 44 | return (x+width<=width) || (x+width>=132); |
wjohnsto | 0:93dce1e528b9 | 45 | } |
wjohnsto | 0:93dce1e528b9 | 46 | |
wjohnsto | 0:93dce1e528b9 | 47 | bool Ball::hitY() { |
wjohnsto | 0:93dce1e528b9 | 48 | return (y<=0) || (y+height>=130); |
wjohnsto | 0:93dce1e528b9 | 49 | } |
wjohnsto | 1:3cc8b1413557 | 50 | |
wjohnsto | 1:3cc8b1413557 | 51 | /* |
wjohnsto | 1:3cc8b1413557 | 52 | * Member Function hitP1: |
wjohnsto | 1:3cc8b1413557 | 53 | * Description: Checks to see if there is |
wjohnsto | 1:3cc8b1413557 | 54 | * a collision between paddle1 and the ball. |
wjohnsto | 1:3cc8b1413557 | 55 | * Has special functionality for changing |
wjohnsto | 1:3cc8b1413557 | 56 | * y-incline based on collision point. |
wjohnsto | 1:3cc8b1413557 | 57 | */ |
wjohnsto | 0:93dce1e528b9 | 58 | bool Ball::hitP1(int _x, int _y, int _height) { |
wjohnsto | 0:93dce1e528b9 | 59 | bool hit = ((_x>=x) && (xInc<0)) && |
wjohnsto | 0:93dce1e528b9 | 60 | (((_y<=y) && (_y+_height>=y+height)) || |
wjohnsto | 0:93dce1e528b9 | 61 | ((_y>=y) && (_y<=y+height)) || |
wjohnsto | 0:93dce1e528b9 | 62 | ((_y+_height>=y) && (_y+_height<=y+height)) |
wjohnsto | 0:93dce1e528b9 | 63 | ); |
wjohnsto | 0:93dce1e528b9 | 64 | if(hit) { |
wjohnsto | 0:93dce1e528b9 | 65 | if(_y+_height-y < 4 && yInc>0) yInc = 2; |
wjohnsto | 0:93dce1e528b9 | 66 | if(y-_y < 4 && yInc<0) yInc = -2; |
wjohnsto | 0:93dce1e528b9 | 67 | else yInc = (yInc>0) ? 1 : -1; |
wjohnsto | 0:93dce1e528b9 | 68 | } |
wjohnsto | 0:93dce1e528b9 | 69 | return hit; |
wjohnsto | 0:93dce1e528b9 | 70 | } |
wjohnsto | 0:93dce1e528b9 | 71 | |
wjohnsto | 1:3cc8b1413557 | 72 | |
wjohnsto | 1:3cc8b1413557 | 73 | /* |
ipapp3 | 3:e1328f84b107 | 74 | * Member Function hitB2: |
wjohnsto | 1:3cc8b1413557 | 75 | * Description: Checks to see if there is |
ipapp3 | 3:e1328f84b107 | 76 | * a collision between a block and the ball. |
wjohnsto | 1:3cc8b1413557 | 77 | * Has special functionality for changing |
wjohnsto | 1:3cc8b1413557 | 78 | * y-incline based on collision point. |
wjohnsto | 1:3cc8b1413557 | 79 | */ |
ipapp3 | 3:e1328f84b107 | 80 | bool Ball::hitB1(int _x, int _y, int _height) { |
wjohnsto | 0:93dce1e528b9 | 81 | bool hit = ((_x<=x+width) && (xInc>0)) && |
wjohnsto | 0:93dce1e528b9 | 82 | (((_y<=y) && (_y+_height>=y+height)) || |
wjohnsto | 0:93dce1e528b9 | 83 | ((_y>=y) && (_y<=y+height)) || |
wjohnsto | 0:93dce1e528b9 | 84 | ((_y+_height>=y) && (_y+_height<=y+height)) |
wjohnsto | 0:93dce1e528b9 | 85 | ); |
wjohnsto | 0:93dce1e528b9 | 86 | if(hit) { |
wjohnsto | 0:93dce1e528b9 | 87 | if(_y+_height-y < 4 && yInc>0) yInc = 2; |
wjohnsto | 0:93dce1e528b9 | 88 | if(y-_y < 4 && yInc<0) yInc = -2; |
wjohnsto | 0:93dce1e528b9 | 89 | else yInc = (yInc>0) ? 1 : -1; |
wjohnsto | 0:93dce1e528b9 | 90 | } |
wjohnsto | 0:93dce1e528b9 | 91 | return hit; |
wjohnsto | 0:93dce1e528b9 | 92 | } |
wjohnsto | 0:93dce1e528b9 | 93 | |
wjohnsto | 0:93dce1e528b9 | 94 | void Ball::reverseX() { |
wjohnsto | 0:93dce1e528b9 | 95 | xInc *= -1; |
wjohnsto | 0:93dce1e528b9 | 96 | } |
wjohnsto | 0:93dce1e528b9 | 97 | |
wjohnsto | 0:93dce1e528b9 | 98 | void Ball::reverseY() { |
wjohnsto | 0:93dce1e528b9 | 99 | yInc *= -1; |
wjohnsto | 0:93dce1e528b9 | 100 | } |