Simple Pong game on NokiaLCD with PS2
Dependencies: mbed PS2 NokiaLCD
ball.cpp@0:93dce1e528b9, 2011-02-27 (annotated)
- Committer:
- wjohnsto
- Date:
- Sun Feb 27 23:35:17 2011 +0000
- Revision:
- 0:93dce1e528b9
- Child:
- 1:3cc8b1413557
Version 0.0
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 | 0:93dce1e528b9 | 11 | void Ball::move(NokiaLCD &lcd) { |
wjohnsto | 0:93dce1e528b9 | 12 | lcd.fill(x,y,width,height,0x000000); |
wjohnsto | 0:93dce1e528b9 | 13 | x += xInc; y += yInc; |
wjohnsto | 0:93dce1e528b9 | 14 | } |
wjohnsto | 0:93dce1e528b9 | 15 | |
wjohnsto | 0:93dce1e528b9 | 16 | void Ball::draw(NokiaLCD &lcd, bool isBlack) const { |
wjohnsto | 0:93dce1e528b9 | 17 | lcd.fill(x, y, width, height, (isBlack) ? 0x000000 : color); |
wjohnsto | 0:93dce1e528b9 | 18 | } |
wjohnsto | 0:93dce1e528b9 | 19 | |
wjohnsto | 0:93dce1e528b9 | 20 | int Ball::size() const { |
wjohnsto | 0:93dce1e528b9 | 21 | return width*height; |
wjohnsto | 0:93dce1e528b9 | 22 | } |
wjohnsto | 0:93dce1e528b9 | 23 | |
wjohnsto | 0:93dce1e528b9 | 24 | int Ball::getX() const { |
wjohnsto | 0:93dce1e528b9 | 25 | return x; |
wjohnsto | 0:93dce1e528b9 | 26 | } |
wjohnsto | 0:93dce1e528b9 | 27 | |
wjohnsto | 0:93dce1e528b9 | 28 | int Ball::getY() const { |
wjohnsto | 0:93dce1e528b9 | 29 | return y; |
wjohnsto | 0:93dce1e528b9 | 30 | } |
wjohnsto | 0:93dce1e528b9 | 31 | |
wjohnsto | 0:93dce1e528b9 | 32 | bool Ball::hitX() { |
wjohnsto | 0:93dce1e528b9 | 33 | return (x+width<=width) || (x+width>=132); |
wjohnsto | 0:93dce1e528b9 | 34 | } |
wjohnsto | 0:93dce1e528b9 | 35 | |
wjohnsto | 0:93dce1e528b9 | 36 | bool Ball::hitY() { |
wjohnsto | 0:93dce1e528b9 | 37 | return (y<=0) || (y+height>=130); |
wjohnsto | 0:93dce1e528b9 | 38 | } |
wjohnsto | 0:93dce1e528b9 | 39 | |
wjohnsto | 0:93dce1e528b9 | 40 | bool Ball::hitP1(int _x, int _y, int _height) { |
wjohnsto | 0:93dce1e528b9 | 41 | bool hit = ((_x>=x) && (xInc<0)) && |
wjohnsto | 0:93dce1e528b9 | 42 | (((_y<=y) && (_y+_height>=y+height)) || |
wjohnsto | 0:93dce1e528b9 | 43 | ((_y>=y) && (_y<=y+height)) || |
wjohnsto | 0:93dce1e528b9 | 44 | ((_y+_height>=y) && (_y+_height<=y+height)) |
wjohnsto | 0:93dce1e528b9 | 45 | ); |
wjohnsto | 0:93dce1e528b9 | 46 | if(hit) { |
wjohnsto | 0:93dce1e528b9 | 47 | if(_y+_height-y < 4 && yInc>0) yInc = 2; |
wjohnsto | 0:93dce1e528b9 | 48 | if(y-_y < 4 && yInc<0) yInc = -2; |
wjohnsto | 0:93dce1e528b9 | 49 | else yInc = (yInc>0) ? 1 : -1; |
wjohnsto | 0:93dce1e528b9 | 50 | } |
wjohnsto | 0:93dce1e528b9 | 51 | return hit; |
wjohnsto | 0:93dce1e528b9 | 52 | } |
wjohnsto | 0:93dce1e528b9 | 53 | |
wjohnsto | 0:93dce1e528b9 | 54 | bool Ball::hitP2(int _x, int _y, int _height) { |
wjohnsto | 0:93dce1e528b9 | 55 | bool hit = ((_x<=x+width) && (xInc>0)) && |
wjohnsto | 0:93dce1e528b9 | 56 | (((_y<=y) && (_y+_height>=y+height)) || |
wjohnsto | 0:93dce1e528b9 | 57 | ((_y>=y) && (_y<=y+height)) || |
wjohnsto | 0:93dce1e528b9 | 58 | ((_y+_height>=y) && (_y+_height<=y+height)) |
wjohnsto | 0:93dce1e528b9 | 59 | ); |
wjohnsto | 0:93dce1e528b9 | 60 | if(hit) { |
wjohnsto | 0:93dce1e528b9 | 61 | if(_y+_height-y < 4 && yInc>0) yInc = 2; |
wjohnsto | 0:93dce1e528b9 | 62 | if(y-_y < 4 && yInc<0) yInc = -2; |
wjohnsto | 0:93dce1e528b9 | 63 | else yInc = (yInc>0) ? 1 : -1; |
wjohnsto | 0:93dce1e528b9 | 64 | } |
wjohnsto | 0:93dce1e528b9 | 65 | return hit; |
wjohnsto | 0:93dce1e528b9 | 66 | } |
wjohnsto | 0:93dce1e528b9 | 67 | |
wjohnsto | 0:93dce1e528b9 | 68 | void Ball::reverseX() { |
wjohnsto | 0:93dce1e528b9 | 69 | xInc *= -1; |
wjohnsto | 0:93dce1e528b9 | 70 | } |
wjohnsto | 0:93dce1e528b9 | 71 | |
wjohnsto | 0:93dce1e528b9 | 72 | void Ball::reverseY() { |
wjohnsto | 0:93dce1e528b9 | 73 | yInc *= -1; |
wjohnsto | 0:93dce1e528b9 | 74 | } |