ST7735 Pong by Jonne Valola, derived work from William Johnston\'s mbed Pong for NokiaLCD / PS2 keyboard This pong uses a rotary encoder hooked to pins 21 and 22, ST7735_TFT library by me, RotaryEncoder library by Shinichiro Nakamura. All copyrights of respective owners. Use on your own risk and discretion.
Dependencies: mbed ST7735_TFT RotaryEncoder TFT_fonts_old
ball.cpp@0:2353da390056, 2011-12-12 (annotated)
- Committer:
- smultron1977
- Date:
- Mon Dec 12 21:35:42 2011 +0000
- Revision:
- 0:2353da390056
Original 2 hour hack
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
smultron1977 | 0:2353da390056 | 1 | #include "mbed.h" |
smultron1977 | 0:2353da390056 | 2 | #include "ball.h" |
smultron1977 | 0:2353da390056 | 3 | |
smultron1977 | 0:2353da390056 | 4 | Ball::Ball() { |
smultron1977 | 0:2353da390056 | 5 | int x=y=width=height=color=xInc=yInc=0; |
smultron1977 | 0:2353da390056 | 6 | } |
smultron1977 | 0:2353da390056 | 7 | |
smultron1977 | 0:2353da390056 | 8 | Ball::Ball(int x, int y, int w, int h, int c, int xi, int yi) |
smultron1977 | 0:2353da390056 | 9 | : x(x), y(y), width(w), height(h), color(c), xInc(xi), yInc(yi) {} |
smultron1977 | 0:2353da390056 | 10 | |
smultron1977 | 0:2353da390056 | 11 | |
smultron1977 | 0:2353da390056 | 12 | /* |
smultron1977 | 0:2353da390056 | 13 | * Member Function move: |
smultron1977 | 0:2353da390056 | 14 | * Description: Colors in the previous ball black |
smultron1977 | 0:2353da390056 | 15 | * and moves ball to new position. |
smultron1977 | 0:2353da390056 | 16 | */ |
smultron1977 | 0:2353da390056 | 17 | void Ball::move(ST7735_TFT &lcd) { |
smultron1977 | 0:2353da390056 | 18 | draw(lcd, true); |
smultron1977 | 0:2353da390056 | 19 | x += xInc; y += yInc; |
smultron1977 | 0:2353da390056 | 20 | } |
smultron1977 | 0:2353da390056 | 21 | |
smultron1977 | 0:2353da390056 | 22 | /* |
smultron1977 | 0:2353da390056 | 23 | * Member Function draw: |
smultron1977 | 0:2353da390056 | 24 | * Description: Draws object on screen |
smultron1977 | 0:2353da390056 | 25 | * if isBlack, color in black. |
smultron1977 | 0:2353da390056 | 26 | */ |
smultron1977 | 0:2353da390056 | 27 | void Ball::draw(ST7735_TFT &lcd, bool isBlack) const { |
smultron1977 | 0:2353da390056 | 28 | lcd.fillrect(x, y, x+width, y+height, (isBlack) ? 0x000000 : color); |
smultron1977 | 0:2353da390056 | 29 | } |
smultron1977 | 0:2353da390056 | 30 | |
smultron1977 | 0:2353da390056 | 31 | int Ball::size() const { |
smultron1977 | 0:2353da390056 | 32 | return width*height; |
smultron1977 | 0:2353da390056 | 33 | } |
smultron1977 | 0:2353da390056 | 34 | |
smultron1977 | 0:2353da390056 | 35 | int Ball::getX() const { |
smultron1977 | 0:2353da390056 | 36 | return x; |
smultron1977 | 0:2353da390056 | 37 | } |
smultron1977 | 0:2353da390056 | 38 | |
smultron1977 | 0:2353da390056 | 39 | int Ball::getY() const { |
smultron1977 | 0:2353da390056 | 40 | return y; |
smultron1977 | 0:2353da390056 | 41 | } |
smultron1977 | 0:2353da390056 | 42 | |
smultron1977 | 0:2353da390056 | 43 | bool Ball::hitX() { |
smultron1977 | 0:2353da390056 | 44 | return (x+width<=width) || (x+width>=160); |
smultron1977 | 0:2353da390056 | 45 | } |
smultron1977 | 0:2353da390056 | 46 | |
smultron1977 | 0:2353da390056 | 47 | bool Ball::hitY() { |
smultron1977 | 0:2353da390056 | 48 | return (y<=0) || (y+height>=128); |
smultron1977 | 0:2353da390056 | 49 | } |
smultron1977 | 0:2353da390056 | 50 | |
smultron1977 | 0:2353da390056 | 51 | /* |
smultron1977 | 0:2353da390056 | 52 | * Member Function hitP1: |
smultron1977 | 0:2353da390056 | 53 | * Description: Checks to see if there is |
smultron1977 | 0:2353da390056 | 54 | * a collision between paddle1 and the ball. |
smultron1977 | 0:2353da390056 | 55 | * Has special functionality for changing |
smultron1977 | 0:2353da390056 | 56 | * y-incline based on collision point. |
smultron1977 | 0:2353da390056 | 57 | */ |
smultron1977 | 0:2353da390056 | 58 | bool Ball::hitP1(int _x, int _y, int _height) { |
smultron1977 | 0:2353da390056 | 59 | bool hit = ((_x>=x) && (xInc<0)) && |
smultron1977 | 0:2353da390056 | 60 | (((_y<=y) && (_y+_height>=y+height)) || |
smultron1977 | 0:2353da390056 | 61 | ((_y>=y) && (_y<=y+height)) || |
smultron1977 | 0:2353da390056 | 62 | ((_y+_height>=y) && (_y+_height<=y+height)) |
smultron1977 | 0:2353da390056 | 63 | ); |
smultron1977 | 0:2353da390056 | 64 | if(hit) { |
smultron1977 | 0:2353da390056 | 65 | if(_y+_height-y < 4 && yInc>0) yInc = 2; |
smultron1977 | 0:2353da390056 | 66 | if(y-_y < 4 && yInc<0) yInc = -2; |
smultron1977 | 0:2353da390056 | 67 | else yInc = (yInc>0) ? 1 : -1; |
smultron1977 | 0:2353da390056 | 68 | } |
smultron1977 | 0:2353da390056 | 69 | return hit; |
smultron1977 | 0:2353da390056 | 70 | } |
smultron1977 | 0:2353da390056 | 71 | |
smultron1977 | 0:2353da390056 | 72 | |
smultron1977 | 0:2353da390056 | 73 | /* |
smultron1977 | 0:2353da390056 | 74 | * Member Function hitP2: |
smultron1977 | 0:2353da390056 | 75 | * Description: Checks to see if there is |
smultron1977 | 0:2353da390056 | 76 | * a collision between paddle2 and the ball. |
smultron1977 | 0:2353da390056 | 77 | * Has special functionality for changing |
smultron1977 | 0:2353da390056 | 78 | * y-incline based on collision point. |
smultron1977 | 0:2353da390056 | 79 | */ |
smultron1977 | 0:2353da390056 | 80 | bool Ball::hitP2(int _x, int _y, int _height) { |
smultron1977 | 0:2353da390056 | 81 | bool hit = ((_x<=x+width) && (xInc>0)) && |
smultron1977 | 0:2353da390056 | 82 | (((_y<=y) && (_y+_height>=y+height)) || |
smultron1977 | 0:2353da390056 | 83 | ((_y>=y) && (_y<=y+height)) || |
smultron1977 | 0:2353da390056 | 84 | ((_y+_height>=y) && (_y+_height<=y+height)) |
smultron1977 | 0:2353da390056 | 85 | ); |
smultron1977 | 0:2353da390056 | 86 | if(hit) { |
smultron1977 | 0:2353da390056 | 87 | if(_y+_height-y < 4 && yInc>0) yInc = 2; |
smultron1977 | 0:2353da390056 | 88 | if(y-_y < 4 && yInc<0) yInc = -2; |
smultron1977 | 0:2353da390056 | 89 | else yInc = (yInc>0) ? 1 : -1; |
smultron1977 | 0:2353da390056 | 90 | } |
smultron1977 | 0:2353da390056 | 91 | return hit; |
smultron1977 | 0:2353da390056 | 92 | } |
smultron1977 | 0:2353da390056 | 93 | |
smultron1977 | 0:2353da390056 | 94 | void Ball::reverseX() { |
smultron1977 | 0:2353da390056 | 95 | xInc *= -1; |
smultron1977 | 0:2353da390056 | 96 | } |
smultron1977 | 0:2353da390056 | 97 | |
smultron1977 | 0:2353da390056 | 98 | void Ball::reverseY() { |
smultron1977 | 0:2353da390056 | 99 | yInc *= -1; |
smultron1977 | 0:2353da390056 | 100 | } |